Forum > General

Synaser timeout handling

(1/1)

JJJ:
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:
If you don't want exceptions raised, there is a property for that, RaiseExcept, just set it to false:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    {:If @true, communication errors raise exceptions. If @false (default), only     the @link(LastError) value is set.}    property RaiseExcept: boolean read FRaiseExcept write FRaiseExcept;

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TBlockSerial.ExceptCheck;var  e: ESynaSerError;  s: string;begin  if FRaiseExcept and (FLastError <> sOK) then  begin    s := GetErrorDesc(FLastError);    e := ESynaSerError.CreateFmt('Communication error %d: %s', [FLastError, s]);    e.ErrorCode := FLastError;    e.ErrorMessage := s;    raise e;  end;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    {:This method waits until a terminated data string is received. This string     is terminated by the Terminator string. The resulting string is returned     without this termination string! If no data is received within the Timeout     (in milliseconds) period, @link(LastError) is set to @link(ErrTimeout).}    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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TBlockSerial.RecvString(Timeout: Integer): AnsiString;var  s: AnsiString;begin  Result := '';  s := RecvTerminated(Timeout, #13 + #10);  if FLastError = sOK then    Result := s;end;

Navigation

[0] Message Index

Go to full version