Debian 8 x86_64 Java 使用 NRSerialPort 开发串口通信

2017-01-12

使用方法: https://github.com/NeuronRobotics/nrjavaserial
下载: https://oss.sonatype.org/#nexus-search;gav~com.neuronrobotics~nrjavaserial~~~

测试代码

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.TooManyListenersException;

import gnu.io.NRSerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

public class NRJavaSerialDemo {

    void connect(String port) {
        for (String s : NRSerialPort.getAvailableSerialPorts()) {
            System.out.println("Availible port: " + s);
        }
        int baudRate = 115200;
        NRSerialPort serialPort = new NRSerialPort(port, baudRate);
        serialPort.connect();

        DataInputStream inStream = new DataInputStream(serialPort.getInputStream());
        DataOutputStream outStream = new DataOutputStream(serialPort.getOutputStream());

        new Thread(new SerialWriter(outStream)).start();

        try {
            serialPort.addEventListener(new SerialReader(inStream));
            serialPort.notifyOnDataAvailable(true);
        } catch (TooManyListenersException e1) {
            e1.printStackTrace();
        }

        try {
            String testData = "ATD139xxxxxxxx;\n";
            outStream.write(testData.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class SerialReader implements SerialPortEventListener {
        private DataInputStream inStream;
        private byte[] buffer = new byte[1024];

        public SerialReader(DataInputStream inStream) {
            this.inStream = inStream;
        }

        public void serialEvent(SerialPortEvent arg0) {
            int data;

            int len = 0;
            try {
                while ((data = inStream.read()) > -1)
                    buffer[len++] = (byte) data;
                System.out.println(new String(buffer, 0, len));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class SerialWriter implements Runnable {
        private DataOutputStream outStream;

        public SerialWriter(DataOutputStream outStream) {
            this.outStream = outStream;
        }

        public void run() {
            int c = 0;
            try {
                while ((c = System.in.read()) > -1)
                    this.outStream.write(c);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            new NRJavaSerialDemo().connect("/dev/ttyUSB0");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

说明
使用 DataInputstream 不要使用 InputStream, InputStream 会接收不到数据
测试代码除使用 NRSerialPort类 代替 RXTX 的 SerialPort类, 其他部分和 RXTX 案例一样
NRSerialPort 不需要 RXTX 的两个 so文件, 可以直接使用
RXTX 案例: http://rxtx.qbang.org/wiki/index.php/Examples

顶部