Windows和linux下的Java串口开发( javacomm20/RXTX)

来源:互联网 发布:程序员都有什么app 编辑:程序博客网 时间:2024/05/20 05:47

一、        使用javacomm20-win32

Jar下载地址: http://www.oreilly.com.tw/bookcode/java_io/javacomm20-win32.zip

Win32环境配置:

1、jdk配置

                            将文件Win32com.dll复制到%JAVA_HOME%\jre\bin目录下,然后将javax.com.properties文件复制到%JAVA_HOME%\jre\lib目录下;

2、项目配置

                            将Comm.jar引入项目的lib目录并Build Path– add to Build Path;



一、        使用RXTX

Jar下载地址: http://rxtx.qbang.org/wiki/index.php/Main_Page

Win32/64环境配置:

1、jdk配置

                            将文件rxtxSerial.dll复制到%JAVA_HOME%\jre\bin目录下 , 其它java.library.path也可;

 2、项目配置

                            将RXTXcomm.jar引入项目lib目录并Build Path– add to Build Path;

 

Linux环境配置

1、jdk配置

                            将文件librxtxSerail.so复制到%JAVA_HOME%\jre\lib\i386目录下, 其它java.library.path也可;

2、项目配置

                            将RxtxComm.jar引入lib目录并Build Path– add to Build Path,然后运行下列代码。

NOTE: For a JDK installation onarchitecture=x86_64, just change the i386 to x86_64above.

 

注意:运行eclipse的时候加上sudo,以排除eclipse因权限问题而无法正常的显示串口



import javax.comm.CommPortIdentifier;import java.util.Enumeration;/** * @author Jacarri *  */public class PrintPort {public static void main(String[] args) {System.out.println("请确认win32com.dll文件在如下位置:" + System.getProperty("java.library.path"));System.out.println("将javax.comm.properties复制到<JRE_HOME>/lib目录");Enumeration portList = CommPortIdentifier.getPortIdentifiers();System.out.println("-----------------------");while (portList.hasMoreElements()) {CommPortIdentifier ci = (CommPortIdentifier) portList.nextElement();System.out.println(ci.getName() + " ][" + ci.getPortType() + "][ " + ci.getCurrentOwner());System.out.println("-----------------------");}}}



import gnu.io.CommPortIdentifier;import java.io.File;import java.util.Enumeration;public class CommInfo {public static void main(String[] args) {  if (File.separator.equalsIgnoreCase("\\")) {System.out.println("请确认rxtxSerial.dll文件在如下位置:"+ System.getProperty("java.library.path"));}if (File.separator.equalsIgnoreCase("/")) {System.out.println("请确认librxtxSerial.so文件在如下位置:"+ System.getProperty("java.library.path"));}    Enumeration portList = CommPortIdentifier.getPortIdentifiers();      System.out.println("-----------------------");      while (portList.hasMoreElements()) {          CommPortIdentifier ci = (CommPortIdentifier) portList.nextElement();          System.out.println(ci.getName() + " ][" + ci.getPortType() + "][ " + ci.getCurrentOwner());          System.out.println("-----------------------");      }  }}




针对第一种,可以看下会有如下信息:

请确认win32com.dll文件在如下位置:D:\Java\32\jdk1.6.0_13\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\Microsoft Office\Office14;D:\Java\jdk1.7.0_06\bin;D:\application\mysql-5.5.21-winx64\bin;D:\application\android-sdk-windows\tools;D:\application\android-sdk-windows\platform-tools;D:\application\maven3\bin;D:\Program Files\TortoiseSVN\bin;D:\application\apache-tomcat-7.0.29\bin;D:\application\apache-ant-1.8.4\bin;D:\application\Sqliteman-1.2.2;D:\application\grails-2.2.2\bin;D:\Program Files\TortoiseGit\bin;D:\application\gradle-1.2\bin;D:\application\Python27;D:\Program Files (x86)\nodejs\;D:\application\Google\google_appengine\;C:\Users\Jacarri\AppData\Roaming\npm将javax.comm.properties复制到<JRE_HOME>/lib目录-----------------------COM3 ][1][ Port currently not owned-----------------------COM4 ][1][ Port currently not owned-----------------------COM10 ][1][ Port currently not owned-----------------------LPT1 ][2][ Port currently not owned-----------------------LPT2 ][2][ Port currently not owned-----------------------

如果提示[javax.comm.NoSuchPortException at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105)],一般都是javax.comm.properties文件放错地方了



针对第二种,在linux下 运行时可以看下如下信息:

  请确认librxtxSerial.so文件在如下位置:/usr/lib/jdk1.7.0_17/jre/lib/i386/client:/usr/lib/jdk1.7.0_17/jre/lib/i386::/usr/java/packages/lib/i386:/lib:/usr/libWARNING:  RXTX Version mismatchJar version = RXTX-2.2pre1native lib Version = RXTX-2.1-7pre20-----------------------/dev/ttyS1 ][1][ null-----------------------/dev/ttyS0 ][1][ null-----------------------/dev/lp0 ][2][ null-----------------------Experimental:  JNI_OnLoad called.


原创粉丝点击