View Issue Details

IDProjectCategoryView StatusLast Update
0036566FPCRTLpublic2020-02-21 22:12
ReporterArmin Linder Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityalways
Status feedbackResolutionopen 
PlatformRaspberry Pi 3+OSRaspbian 
Product Version3.0.4 
Summary0036566: serReadTimeout hangs indefinitely if serial device is disconnected
DescriptionI 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 ReproduceCall SerReradTimeout in a loop and disconnect the device. the call won't ever return.
Additional InformationIf this info isn't enough to reproduce the problem I will happily drag together a short sample.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Activities

Florian

2020-02-21 22:12

administrator   ~0121187

This is hard to fix without real serial hardware, but maybe you can create yourself a fix based on: https://stackoverflow.com/questions/34170350/detecting-if-a-character-device-has-disconnected-in-linux-in-with-termios-api-c and provide it.

Issue History

Date Modified Username Field Change
2020-01-13 16:41 Armin Linder New Issue
2020-02-21 22:12 Florian Assigned To => Florian
2020-02-21 22:12 Florian Status new => feedback
2020-02-21 22:12 Florian FPCTarget => -
2020-02-21 22:12 Florian Note Added: 0121187