wjwwood / serial

Cross-platform, Serial Port library written in C++
http://wjwwood.github.com/serial/
MIT License
2.16k stars 1.05k forks source link

Incorrect timeout duration with `readline` #291

Open nicolas-cadart opened 1 year ago

nicolas-cadart commented 1 year ago

Thanks for this great library!

I was running some tests and found out that the timeout feature does not work as expected when using the readline (and probably readlines too) method. After digging a bit into the code, I found that readline(256) runs 256 times a read(1) call. Therefore, the timeout is reset for each individual byte, and does not account for the whole 256 bytes reading.

I noticed this issue when running some tests using an incorrect baudrate (9600 instead of the correct 115200), with a simple global timeout of 3000 ms. The readline(256) call terminates after several tens of seconds (value not stable across different repetitions), while the read(256) call correctly terminates after max 3 s (it might return earlier, but returning noise because of wrong baudrate). In my situation, I don't know the baudrate being used, so I need to automatically check if the received data looks correct to validate a baudrate before using it.

My machine is running on Ubuntu 20.04.