编程语言
首页 > 编程语言> > java – 不匹配的RXTX版本

java – 不匹配的RXTX版本

作者:互联网

我找到了一个代码,用Java通过串口与Arduino进行通信,并希望尝试使其工作,以便为项目构思扩展它,但我不断收到此错误

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不匹配,但是在本机lib中构建的链接是一个网站不再存在.我不确定如何解决这个问题.如果您认为这是问题,我的代码如下.任何帮助,将不胜感激.

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");
        }
}

解决方法:

>检查是否已安装库.如果没有,在Ubuntu的情况下,
sudo apt-get install librxtx-java
>如果已安装,
转到安装的路径—在我的案例中是/usr/share/java/RXTXcomm.jar
>如果您使用的是Ubuntu或任何Linux发行版,请从Eclipse构建路径中删除任何下载的rxtx库,并在步骤2中添加已安装的rxtx库的路径.您也可以将其复制到项目中.
>检查您的java库属性(System.getProperty(“java.library.path”))以了解该文件夹.
>将librxtxSerial.so复制到Java库路径 – 在我的例子中
sudo cp -r /usr/lib/jni/librxtxSerial.so /usr/lib / x86_64-linux-gnu

之后再次尝试您的代码.

标签:java,arduino,rxtx
来源: https://codeday.me/bug/20190624/1279509.html