java - 使用 Java 中的串行通信从 Arduino 写入和读取数据

标签 java windows netbeans serial-port arduino-uno

我正在尝试将数据写入我的 Arduino Uno 并从中接收日期。

我在 Windows 8.1 上使用 NetBeans,并使用库“RXTXcomm.jar”来执行此操作。

我的代码是这样的,我的Arduino位于COM3上,它在第25行和第80行抛出错误:

错误

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path

at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)

at java.lang.Runtime.loadLibrary0(Runtime.java:870)

at java.lang.System.loadLibrary(System.java:1122)

at gnu.io.CommPortIdentifier.(CommPortIdentifier.java:83)

at arduino.test.pkg3.ArduinoTest3.initialize(ArduinoTest3.java:25)

at arduino.test.pkg3.ArduinoTest3.main(ArduinoTest3.java:80)

代码:

01: package arduino.test.pkg3;
02: 
03: import java.io.BufferedReader;
04: import java.io.InputStreamReader;
05: import java.io.OutputStream;
06: import gnu.io.CommPortIdentifier; 
07: import gnu.io.SerialPort;
08: import gnu.io.SerialPortEvent; 
09: import gnu.io.SerialPortEventListener; 
10: import java.util.Enumeration;
11: 
12: public class ArduinoTest3 implements SerialPortEventListener {
13: 
14:     SerialPort serialPort;
15:     private static final String PORT_NAMES[] = {"COM3"};
16:     private BufferedReader input;
17:     private OutputStream output;
18:     private static final int TIME_OUT = 2000;
19:     private static final int DATA_RATE = 9600;
20: 
21:     public void initialize(){
22:         System.setProperty("gnu.io.rxtx.SerialPorts", "COM3");
23: 
24:         CommPortIdentifier portId = null;
25:         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 = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
            output = serialPort.getOutputStream();

            serialPort.addEventListener(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{
                String inputLine=input.readLine();
                System.out.println(inputLine);
            }catch (Exception e){
                System.err.println(e.toString());
            }
        }
    }

    public static void main(String[] args) throws Exception{
        ArduinoTest3 main = new ArduinoTest3();
80 :        main.initialize();
        Thread t=new Thread(){
            public void run(){
                try{Thread.sleep(1000000);}catch(InterruptedException ie) {}
            }
        };
        t.start();
        System.out.println("Started");
    }
}

希望你们能找到答案:)

最佳答案

我解决了这个问题......

我还应该使“RXTXcomm.jar”库与名为“rxtxSerial.dll”的“dll”文件对话。 但是在我添加那个“dll”文件之后,我得到了一个不同的错误,但是这次的问题来自“dll”文件本身,所以经过大量研究,我找到了完整的“dll”文件(http://jlog.org/rxtx-win.html)以及(https://www.youtube.com/watch?v=43Vdpz1YmdU)上的教程并且它有效:)。

希望有人会发现我的经验有用。

关于java - 使用 Java 中的串行通信从 Arduino 写入和读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42442928/

相关文章:

java - 哪种 Java 框架最适合大型 DML 操作

c++ - SChannel 中的密码套件选择

java - 线程 “main” java.util.zip.ZipException : error in opening zip file 中的异常

java - 包中的 Main 类?

xml 中的 java.lang.NullPointerException

java - 按字母顺序从目录中读取 Android 文件

linux - 从后台脚本运行 SSH

java - Netbeans:如何知道我当前正在查看什么 JAR

java - 错误的按钮激活

c++ - 读取 JPEG,调整大小并将其保存到磁盘