serReadTimeout hangs indefinitely if serial device is disconnected
Original Reporter info from Mantis: ArminLinder
-
Reporter name: Armin Linder
Original Reporter info from Mantis: ArminLinder
- Reporter name: Armin Linder
Description:
I use this line of code to read serial data from a serial device (/dev/ttyACM0)
Const
GAUGE_VALUE_LENGTH = 8; // Sign + 5 Digits + CR + LF
GAUGE_RESPONSE_TIMEOUT = 500;
MAX_BUFFER = 100;
var
ByteBuffer: array[0..MAX_BUFFER] of byte;
StringBuffer: ShortString absolute ByteBuffer;
... (loop until terminated)
if _ComHandle <> 0 then
begin
// query the gauge
SerFlushInput(_ComHandle);
StringBuffer := '?';
SerWrite(_ComHandle, StringBuffer[1], 1);
// read response
// For debugging -----
isReading(true);
// End debugging -----
StringBuffer[0] := chr(SerReadTimeout(_ComHandle, ByteBuffer[1], GAUGE_VALUE_LENGTH, GAUGE_RESPONSE_TIMEOUT));
// For debugging -----
isReading(false);
Sleep(500);
// End debugging -----
if Length(StringBuffer) = GAUGE_VALUE_LENGTH then
begin
SetGaugeValue(strToInt(LeftStr(StringBuffer,6)));
end
else
CloseCom();
end;
...
Generally this routine works well on WIndows (10) and Raspbian. Unfortunately, on Raspbian, if I disconnect the device, upon next call of this command, it does never return, nor throw any exception --> the program hangs. Re-Plugging the device doesn't help, I need to restart the program. On Windows everything works, after the timeout has expired, the call returns 0.
Steps to reproduce:
Call SerReradTimeout in a loop and disconnect the device. the call won't ever return.
Additional information:
If this info isn't enough to reproduce the problem I will happily drag together a short sample.
Mantis conversion info:
- Mantis ID: 36566
- OS: Raspbian
- OS Build: 10 (Buster)
- Build: 3.0.4+dfsg-22+rpii
- Platform: Raspberry Pi 3+
- Version: 3.0.4
- Monitored by: » ArminLinder (Armin Linder)