早上好,
我在Windows 7 64位上使用/或安装rxtx时遇到问题.我以前在x86 win XP系统上使用它并没有问题.由于某些原因重新安装到这个新系统,rxtx无法找到任何端口.我已经尝试了rxtx安装,Cloud Hopper的64位本机库并删除所有rxtx文件并从头开始.找到RXTXcomm.jar,我可以浏览NetBeans中的软件包,但实现似乎已被破坏或找不到.
每次执行时,此行都会失败:
- comPort = "COM1";
- portId = CommPortIdentifier.getPortIdentifier(comPort);
并抛出NoSuchPortException.
使用它列出串行端口不会产生任何结果.
- Enumeration ports = CommPortIdentifier.getPortIdentifiers();
- String portArray[] = null;
- while (ports.hasMoreElements()) {
- CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
- System.out.println(port.getName());
- }
我已经检查过串口是否可用所以此时我想知道本机库是否只是针对Windows 7 64位而被破坏了.
有没有人在Windows 7 64位下成功使用RXTX 2.2pre2?
- public SerialControl(String name,String comPort,int baudrate,int databits,String parity,double stopbits) throws Exception {
- int stop = 0;
- int data = 0;
- int par = 0;
- this.name=name;
- // Sanity checks and interpretation
- if (baudrate > 115200 || baudrate < 300) {
- System.err.println(name+": constructor(): Invalid baudrate "+baudrate);
- throw new Exception("Invalid baudrate," + baudrate);
- }
- if (databits >= 5 && databits <= 8) {
- switch (databits) {
- case 5:
- data = SerialPort.DATABITS_5;
- break;
- case 6:
- data = SerialPort.DATABITS_6;
- break;
- case 7:
- data = SerialPort.DATABITS_7;
- break;
- case 8:
- data = SerialPort.DATABITS_8;
- break;
- default:
- System.err.println(name+": constructor(): Invalid data bits,switched " + databits);
- throw new Exception("Invalid data bits,switched " + databits);
- }
- } else {
- throw new Exception("Invalid data bits=" + databits);
- }
- if (stopbits >= 1.0 && stopbits <= 2.0) {
- if (stopbits == 1.0) {
- stop = SerialPort.STOPBITS_1;
- } else if (stopbits == 1.5) {
- stop = SerialPort.STOPBITS_1_5;
- } else if (stopbits == 2.0) {
- stop = SerialPort.STOPBITS_2;
- } else {
- System.err.println(name+": constructor(): Invalid stop bits,switched " + stopbits);
- throw new Exception("Invalid stop bits,switched " + stopbits);
- }
- } else {
- System.err.println(name+": constructor(): Invalid stop bits,switched " + stopbits);
- throw new Exception("Invalid stop bits " + stopbits);
- }
- switch (parity) {
- case "S":
- par = SerialPort.PARITY_SPACE;
- break;
- case "E":
- par = SerialPort.PARITY_EVEN;
- break;
- case "M":
- par = SerialPort.PARITY_MARK;
- break;
- case "O":
- par = SerialPort.PARITY_ODD;
- break;
- case "N":
- par = SerialPort.PARITY_NONE;
- break;
- default:
- System.err.println(name+": constructor(): Invalid parity,switched " + parity);
- throw new Exception("Invalid parity,switched " + parity);
- }
- // Inits
- // Try to find the port specified
- try {
- portId = CommPortIdentifier.getPortIdentifier(comPort);
- } catch (Exception e) {
- System.err.println(name+": constructor(): No such port \"" + comPort+"\"");
- e.printStackTrace();
- throw e;
- }
- // Open the port
- try {
- serialPort = (SerialPort) portId.open("User Port",2000);
- } catch (PortInUseException e) {
- System.err.println(name+": constructor(): Could not open port " + comPort);
- throw e;
- }
- // Grab the input stream
- try {
- inputStream = serialPort.getInputStream();
- } catch (IOException e) {
- System.err.println(name+": constructor(): Could not get input stream for " + comPort);
- throw e;
- }
- // Set the serial port parameters,no flow control
- try {
- serialPort.setSerialPortParams(baudrate,data,stop,par);
- serialPort.setDTR(false);
- serialPort.setRTS(false);
- } catch (UnsupportedCommOperationException e) {
- System.err.println(name+": constructor(): Error initializing " + comPort);
- throw e;
- }
- }
解决方法
我遇到了同样的问题.我使用Eclipse作为IDE进行编程,我在官方维基中找到了这个替代配置:
>将RXTXcomm.jar复制到项目的lib目录中
>将package explorer导航到lib文件夹,右键单击RXTXcomm.jar |构建路径|添加到构建的路径
>将rxtxSerial.dll和rxtxParallel.dll文件复制到项目的根目录
>在运行|运行配置| Classpath选项卡|用户条目|高级|添加文件夹,选择项目的根文件夹
>这应该足以在Eclipse下运行它,在部署可运行的jar时,只需确保dll与jar相同的文件夹(JVM假定它为类路径)
(这是我的第一个答案,我不知道我是否可以发布外部链接,但是从http://rxtx.qbang.org/wiki/index.php/Using_RXTX_In_Eclipse开始的五步)
希望它有所帮助!