RXTX serial connection - issue with blocking read()

后端 未结 4 1039
别那么骄傲
别那么骄傲 2021-01-03 11:58

I am trying to use the RXTX library for blocking serial communication on Windows (XP and 7). I have tested the connection with Hyperterminal in both ends, and it works flawl

4条回答
  •  臣服心动
    2021-01-03 12:12

    I think the code you wrote in your own readLine implementation is buggy. nextByte[0] is never restored to -1 after the first character is read. You should try to use the value returned by inStream.read(nextByte) to state the number of bytes read from the stream instead of the value of your byte array.

    Anyway I think you should go for an event based method of reading the inputs with a SerialPortEventListener:

    serialPort.addEventListener(new SerialPortEventListener() {
    
          public void serialEvent(SerialPortEvent evt) {
               switch (evt.getEventType()) {
               case SerialPortEvent.DATA_AVAILABLE:
                   dataReceived();
                   break;
               default:
                   break;
               }
          }
    });
    serialPort.notifyOnDataAvailable(true);
    

提交回复
热议问题