Recent

Author Topic: Synaser timeout handling  (Read 2315 times)

JJJ

  • New Member
  • *
  • Posts: 13
Synaser timeout handling
« on: May 21, 2021, 02:12:42 pm »
I'm using synaser to send and read serial data in console app in Win 10. If for some reason the app never receive line end mark (CR/LF) I get timeout error and software hangs.
I can make an exception but then I have to start the app again. What is the best way to handle error and prevent the program from crashing.

engkin

  • Hero Member
  • *****
  • Posts: 2927
Re: Synaser timeout handling
« Reply #1 on: June 13, 2021, 04:34:10 am »
If you don't want exceptions raised, there is a property for that, RaiseExcept, just set it to false:
Code: Pascal  [Select][+][-]
  1.     {:If @true, communication errors raise exceptions. If @false (default), only
  2.      the @link(LastError) value is set.}
  3.     property RaiseExcept: boolean read FRaiseExcept write FRaiseExcept;

Code: Pascal  [Select][+][-]
  1. procedure TBlockSerial.ExceptCheck;
  2. var
  3.   e: ESynaSerError;
  4.   s: string;
  5. begin
  6.   if FRaiseExcept and (FLastError <> sOK) then
  7.   begin
  8.     s := GetErrorDesc(FLastError);
  9.     e := ESynaSerError.CreateFmt('Communication error %d: %s', [FLastError, s]);
  10.     e.ErrorCode := FLastError;
  11.     e.ErrorMessage := s;
  12.     raise e;
  13.   end;
  14. end;

The other possibility is to catch the exception and handle the error properly.



As for the other part of the problem, waiting for CR/LF. it happens if you use RecvTerminated
Code: Pascal  [Select][+][-]
  1.     {:This method waits until a terminated data string is received. This string
  2.      is terminated by the Terminator string. The resulting string is returned
  3.      without this termination string! If no data is received within the Timeout
  4.      (in milliseconds) period, @link(LastError) is set to @link(ErrTimeout).}
  5.     function RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString; virtual;

or if you use any of the methods that rely on RecvTerminated: 
RecvString, ATCommand, ATConnect. For instance:
Code: Pascal  [Select][+][-]
  1. function TBlockSerial.RecvString(Timeout: Integer): AnsiString;
  2. var
  3.   s: AnsiString;
  4. begin
  5.   Result := '';
  6.   s := RecvTerminated(Timeout, #13 + #10);
  7.   if FLastError = sOK then
  8.     Result := s;
  9. end;

 

TinyPortal © 2005-2018