ubuntu 16.04 Java RXTXcomm.jar 串口編程及問題解決


 

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

 

備注:學習備忘,希望也給同行提供些幫助,如有疑問,可以評論交流。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM