首页 > 技术文章 > ubuntu 16.04 Java RXTXcomm.jar 串口编程及问题解决

spqin 2020-07-07 18:20 原文

 

1、下载RXTXcomm,安装部署

    我开发工程用的是  mfz-rxtx-2.2-20081207-win-x64.zip  版本的,JDK是1.8,Windows 环境下正常开发,使用正常;部署时下载的是 linux版本,端口能正常打开,写数据报异常;

     C  [librxtxSerial.so+0x75da]  Java_gnu_io_RXTXPort_nativeDrain+0xea

 

 

 

 网上查,说是  rxtxcomm 版本问题导致的,下载一个可用的版本,具体来源也是花了钱的(程序员何必要难为程序员呢,分享不好吗),如有需要的可以加QQ 935958723

安装参照脚本,自行拷贝文件位置:

 

(1)拷贝 .so 文件到 ${JAVA_HOME}/jre/lib/amd64 下;

(2)拷贝RXTXcomm.jar 到 ${JAVA_HOME}/jre/lib/ext目录下

 

2、配置

  port: /dev/ttyS0     #串口(ubuntu 系统下的串口)
bitRate: 9600 #波特率
dataBit: 8 #数据位
stopBit: 1 #停止位
parity: 0 #

3、代码

  (1) 打开串行端口代码  (this.port,指代上述配置的   /dev/ttyS0,其他一样)


            CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(this.port);
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                // 超过2000ms未打开串口,则抛出异常
                this.serialPort = (SerialPort) portId.open(this.port, 150);
                this.inputStream = this.serialPort.getInputStream();
                this.serialPort.notifyOnDataAvailable(true);

                try {
                    this.serialPort.addEventListener(this);
                } catch (TooManyListenersException e) {
                    e.printStackTrace();
                }
                this.serialPort.setSerialPortParams(Integer.parseInt(this.bitRate), Integer.parseInt(this.dataBit),Integer.parseInt(this.stopBit), Integer.parseInt(this.parity));
                //开启线程
                this.sendThread = new SendThread(this.serialPort);
                sendThread.start();
            } 

(2)读串行端口代码(使用监听器,监听串口数据)

 

public class SimpleRead implements Runnable,SerialPortEventListener {

    private CommPortIdentifier portId;
    SerialPort serialPort;
    private InputStream inputStream;
    BufferedOutputStream bufferedOutputStream = null;

    private int msgCodeCount = 0;

    @Override
    public void serialEvent(SerialPortEvent serialPortEvent) {
        switch (serialPortEvent.getEventType()) {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
            case SerialPortEvent.FE:
            case SerialPortEvent.PE:
            case SerialPortEvent.CD:
            case SerialPortEvent.CTS:
            case SerialPortEvent.DSR:
            case SerialPortEvent.RI:
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                break;
            case SerialPortEvent.DATA_AVAILABLE:
                byte[] bytes = new byte[4];
                try {
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(bytes);
                        for (byte aByte : bytes) {
//                            System.out.println(aByte);
                        }
                    }
                } catch (IOException e) {
                }
                break;
        }
    }
}

 

(3) 写串行端口代码

/**
     * 发送消息
     * @throws IOException
     */
    private void sendMsg() throws IOException {
        if(this.bufferedOutputStream==null){
            this.bufferedOutputStream = new BufferedOutputStream(this.serialPort.getOutputStream());
        }
        byte[] testMsgBuffer = testMsg();  //此处就是生成一个字节数据,自行处理
        this.bufferedOutputStream.write(testMsgBuffer);
        this.bufferedOutputStream.flush();
    }

 

备注:学习备忘,希望也给同行提供些帮助,如有疑问,可以评论交流。

 

推荐阅读