Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.35k stars 287 forks source link

Locked java.io.BufferedInputStream on Windows #264

Closed marker91 closed 4 years ago

marker91 commented 4 years ago

Hi, please help to understand why there is a lock when i use InputStream for reading: `"main" #1 prio=5 os_prio=0 cpu=18437.50ms elapsed=80729.23s tid=0x000001633a79e000 nid=0x2148 runnable [0x0000000f7d1fd000] java.lang.Thread.State: RUNNABLE at com.fazecast.jSerialComm.SerialPort.readBytes(Native Method) at com.fazecast.jSerialComm.SerialPort.access$800(SerialPort.java:50) at com.fazecast.jSerialComm.SerialPort$SerialPortInputStream.read(SerialPort.java:1463) at java.io.BufferedInputStream.fill(java.base@11/BufferedInputStream.java:252) at java.io.BufferedInputStream.read1(java.base@11/BufferedInputStream.java:292) at java.io.BufferedInputStream.read(java.base@11/BufferedInputStream.java:351)

hedgecrw commented 4 years ago

I suspect what is happening here is that you are using the Apache IOUtils to read from the serial port instead of just reading directly from the serial port's InputStream. The reason this is a problem is that according to the IOUtils documention, "all the methods in this class that read a stream are buffered internally", apparently using a BufferedInputStream which means that IOUtils/BufferedInputStream is trying to fill its own internal buffer of unspecified size by calling a blocking read() on the underlying serial port. Try using the serial port's read() method or its InputStream directly, and that should solve your problem. Thanks!

marker91 commented 4 years ago

@hedgecrw, thanks for the answer, fixed implementation to use java.util.Scanner with SerialPortInputStream solve problem.