« Reply #19 on: September 30, 2022, 02:59:20 pm »
Or, as Zvoni wrote, in that particular case with an extra check:
expectOptionalEtx, expectBcc:
begin
if (RecvState = expectOptionalEtx) then begin
// expectOptionalEtx: if ch = ETX then begin
bccCount := BccLength;
recvState := expectBcc;
if (ch = ETX) then begin
buffer += ch;
break;
end;
end;
// expectBcc: begin
if not (ch in ['!'..'~']) then
break;
buffer += ch;
bccCount -= 1;
if bccCount = 0 then
recvState := completed
end
otherwise
...
My statement was more along the lines of
function TSerialThread.recvMessage(serHandle: TSerialHandle; var buffer: string;
timeout, limit: integer): boolean;
const
obeyTimeout= true; (* Normally true *)
type
tRecvState= (expectSOH, expectAddress, expectSeq, expectCtrl, expectPayload,
expectHandshake, expectOptionalEtx, expectBcc,
completed);
Function FuncExpectBCC(Const Ach:Char;var AbccCount:Integer;var ArecvState:tRecvState):Boolean;
Begin
Result:=False;
if not (Ach in ['!'..'~']) then
Exit(True);
buffer += ch;
AbccCount -= 1;
if AbccCount = 0 then
ArecvState := completed;
End;
........
expectOptionalEtx: if ch = ETX then begin
buffer += ch;
bccCount := BccLength;
recvState := expectBcc (* To process next character *)
end else begin
bccCount := BccLength;
recvState := expectBcc;
If FuncExpectBCC(ch,bccCount,recvState) Then Break;
end;
expectBcc: If FuncExpectBCC(ch,bccCount,recvState) Then Break
otherwise
break (* Quite simply can't happen *)
end
until obeyTimeout and Elapsed(limitTD, limit);
No Goto or labels or other such nonsense
Logged
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad