RXTX 版本不匹配 [英] Mismatched RXTX Versions

查看:57
本文介绍了RXTX 版本不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个通过串行端口在 Java 中与 Arduino 通信的代码,并想尝试让它工作以便扩展它以实现项目创意,但我不断收到此错误

I found a code to communicate in Java via Serial Port to an Arduino and wanted to try and get it working in order to expand on it for a project idea, but I keep getting this error

Stable Library
=========================================
Native lib Version = RXTX-2.2-20081207 Cloudhopper Build rxtx.cloudhopper.net
Java lib Version   = RXTX-2.1-7
WARNING:  RXTX Version mismatch
Jar version = RXTX-2.1-7
native lib Version = RXTX-2.2-20081207 Cloudhopper Build rxtx.cloudhopper.net
Could not find COM port.
Started

我认为这意味着 RXTX 库存在 jar 不匹配,但指向本机库中构建的链接是一个网站不再存在.我不确定如何解决这个问题.如果您认为这是问题所在,我的代码如下.任何帮助将不胜感激.

I think that it means there is jar mismatch for the RXTX library, but the link to the build in the native lib is a website is no longer there. I'm not exactly sure how to fix the problem. My code is below if you believe that is the issue. Any help would be appreciated.

import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.InputStream;
import java.util.Enumeration;

public class AraInterface {
SerialPort serialPort;
        /** The port we're normally going to use. */
        private static final String PORT_NAMES[] = { 
                        "/dev/tty.usbserial-A9007UX1", // Mac OS X
                        "/dev/ttyUSB0", // Linux
                        "COM35", // Windows //shin: ardu com port here
                        };
        private InputStream input;
        private OutputStream output;
        private static final int TIME_OUT = 2000;
        private static final int DATA_RATE = 9600;

        public void initialize() {
                CommPortIdentifier portId = null;
                Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

                while (portEnum.hasMoreElements()) {
                        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
                        for (String portName : PORT_NAMES) {
                                if (currPortId.getName().equals(portName)) { 
                                        portId = currPortId;
                                        break;
                                }
                        }
                }

                if (portId == null) {
                        System.out.println("Could not find COM port.");
                        return;
                }

                try {
                        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                                        TIME_OUT);

                        serialPort.setSerialPortParams(DATA_RATE,
                                        SerialPort.DATABITS_8,
                                        SerialPort.STOPBITS_1,
                                        SerialPort.PARITY_NONE);

                        input = serialPort.getInputStream();
                        output = serialPort.getOutputStream();

                        serialPort.addEventListener((SerialPortEventListener) this);
                        serialPort.notifyOnDataAvailable(true); 
                } catch (Exception e) {
                        System.err.println(e.toString());
                }
        }

        public synchronized void close() {
                if (serialPort != null) {
                        serialPort.removeEventListener();
                        serialPort.close();
                }
        }

        public synchronized void serialEvent(SerialPortEvent oEvent) {
                if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
                        try {
                                int available = input.available();
                                byte chunk[] = new byte[available];
                                input.read(chunk, 0, available);

                                String temp = new String (chunk);
                                String ref = "Hel";
                                if(temp.compareTo(ref)==0){
                                System.out.println("Hello World request received");
                                }
                                else{
                                System.out.println("lala" + temp);
                                }

                        } catch (Exception e) {
                                System.err.println(e.toString());
                        }
                }

        }

        public static void main(String[] args) throws Exception {
                AraInterface main = new AraInterface();
                main.initialize();
                System.out.println("Started");
        }
}

推荐答案

  1. 检查您是否已安装该库.如果没有,在 Ubuntu 的情况下,sudo apt-get install librxtx-java
  2. 如果已经安装,转到安装路径 --- 在我的情况下 /usr/share/java/RXTXcomm.jar
  3. 如果您使用的是 Ubuntu 或任何 Linux 发行版,请从 Eclipse 构建路径中删除任何下载的 rxtx 库,并在步骤 2 中添加已安装的 rxtx 库的路径.您也可以将其复制到您的项目中.
  4. 检查您的 Java 库属性 (System.getProperty("java.library.path")) 以了解文件夹.
  5. librxtxSerial.so 复制到 Java 库路径 - 就我而言sudo cp -r/usr/lib/jni/librxtxSerial.so/usr/lib/x86_64-linux-gnu
  1. Check if you have installed the library. If not, in case of Ubuntu, sudo apt-get install librxtx-java
  2. If already installed, go to the path of the installation --- in my case /usr/share/java/RXTXcomm.jar
  3. If you are using Ubuntu or any Linux distro, remove any downloaded rxtx library from Eclipse build path, and add the path of the installed rxtx library in step 2. You can also copy it into your project.
  4. Check your java library property (System.getProperty("java.library.path")) to know the folder.
  5. Copy the librxtxSerial.so to the Java library path - in my case sudo cp -r /usr/lib/jni/librxtxSerial.so /usr/lib/x86_64-linux-gnu

然后再次尝试您的代码.

After that try your code again.

这篇关于RXTX 版本不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆