基于JavaFX開發(fā)的查找客戶端可用串口列表
一、開發(fā)目的
基于B/S結(jié)構(gòu),在客戶端查找可用的串口列表。
二、開發(fā)環(huán)境
NetBeans IDE 6.5 for JavaFX 1.0,tomcat5,rxtx 2.1-7r2
三、相關(guān)資源
1、java串口操作框架,下載 rxtx 2.1-7r2。
2、了解串口操作,參考http://rxtx.qbang.org/wiki/index.php/Examples
3、開發(fā)工具,下載 NetBeans IDE 6.5 for JavaFX 1.0
4、了解JavaFX,參考http://developers.sun.com.cn/JavaFX/docs/tutorials/
其他資源略
四、編碼
1、首先開發(fā)串口操作程序DiscoveringAvailablePorts.java
- public static List getAvailableSerialPortsName() {
- List result = new ArrayList();
- Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
- while (thePorts.hasMoreElements()) {
- CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();
- switch (com.getPortType()) {
- case CommPortIdentifier.PORT_SERIAL:
- try {
- CommPort thePort = com.open("CommUtil", 50);
- thePort.close();
- result.add(com.getName());
- } catch (PortInUseException e) {
- System.out.println("Port, " + com.getName() + ", is in use.");
- } catch (Exception e) {
- System.err.println("Failed to open port " + com.getName());
- e.printStackTrace();
- }
- }
- }
- return result;
- }
注:RXTXcomm.jar 拷貝到: %JAVA_HOME%"jre"lib"ext
rxtxSerial.dll 拷貝到: %JAVA_HOME%"jre"bin
并在開發(fā)的工程中引入RXTXcomm.jar
2、然后開發(fā)界面程序SerialConfig.fx
- var portNames =
- new DiscoveringAvailablePorts().getAvailableSerialPortsName();
- var label = SwingLabel{text: "serial port: "};
- var combobox = SwingComboBox{
- width: 100
- items:[
- SwingComboBoxItem{
- text: "Please select" },
- for (idx in portNames) {
- SwingComboBoxItem{
- text:idx.toString()}
- }
- ]
- selectedIndex: 0};
- var button = SwingButton{text: "Save"};
- Stage {
- title: "Components"
- width: 344
- height: 240
- visible: true
- scene: Scene{
- fill: Color.CORNSILK
- content: VBox{
- translateX: 10
- translateY: 30
- spacing: 20
- content:[
- HBox{
- spacing: 10
- content:[label,combobox,HBox{ translateX:40 content:[button]}]
- }
- ]
- }
- }
- }
注:開發(fā)上面的程序時候,在NB中建立JavaFX項目,名稱JavaFXTutorial。
3、***建立一個最簡單的web工程,并編寫頁面程序index.jsp。
- <body>
- This is my JSP page. <br>
- <script src="http://dl.JavaFX.com/dtfx.js"></script>
- <script>
- JavaFX(
- {
- archive: "<%=path%>/JavaFXTutorial.jar",
- width: 400,
- height: 200,
- code: "com.howelltech.SerialConfig",
- name: "JavaFXTutorial"
- }
- );
- </script>
- </body>
五、部署
1、NB項目屬性中,在Run配置中選擇Run in Browser;在Application配置中選擇Self Signed Jar,其他可選。
2、 NB項目目錄中,在dist目錄中,存在JavaFXTutorial.html、JavaFXTutorial.jar、 JavaFXTutorial_browser.jnlp三個文件和lib目錄包括RXTXcomm.jar一個文件。把此目錄中所有文件拷貝到剛才建立好的web工程中,與index.jsp同路徑。
六、測試
1、在服務(wù)器端,如果不啟動tomcat可以直接執(zhí)行JavaFXTutorial.html既能看到效果。如果啟動tomcat,那么必須修改JavaFXTutorial_browser.jnlp中的端口號,要與tomcat使用的一致。
2、在客戶端,必須拷貝rxtxSerial.dll到 %JRE_HOME%"bin下,然后直接訪問服務(wù)器鏈接就可以。
開發(fā)完成后,總體感覺JavaFX比applet要方便,無論是編碼還是部署。但是開發(fā)JavaFX的時候也有很多不方便的地方,比如目前的IDE不支持圖形化開發(fā),雖然支持一些控件的托拽,但終歸是代碼不夠直觀;另外,JavaFX程序發(fā)布后在客戶端運行得還是有些慢。終歸是個新技術(shù),還需要慢慢適應(yīng)啊。
另外,遺留下來兩個問題,看看大家有什么好辦法。
1、關(guān)于客戶端需要拷貝rxtxSerial.dll問題,有沒有更加方便的方法。
2、JavaFX項目打包問題,有沒有更好的方法,比如打個包,其他頁面就可以直接調(diào)用。
【編輯推薦】