Thread interrupt not ending blocking call on input stream read

前端 未结 3 1332
时光取名叫无心
时光取名叫无心 2021-01-04 13:30

I\'m using RXTX to read data from a serial port. The reading is done within a thread spawned in the following manner:

CommPortIdentifier portIdentifier = Co         


        
3条回答
  •  感情败类
    2021-01-04 14:08

    The RXTX SerialInputStream (what is returned by the serial.getInputStream() call) supports a timeout scheme that ended up solving all my problems. Adding the following before creating the new SerialReader object causes the reads to no longer block indefinitely:

    serial.enableReceiveTimeout(1000);
    

    Within the SerialReader object, I had to change a few things around to read directly from the InputStream instead of creating the ReadableByteChannel, but now, I can stop and restart the reader without issue.

提交回复
热议问题