damian177 - 2022-02-11

I have a device with which I communicate via the serial port. I would like to refine the algorythm of receiving complete frames. Receiving frame have format <stx> ...... <etx><crc>. Is the following algorithm optimal for receiving a complete frame? Any tips ?</crc></etx></stx>

IF hCom <> RTS_INVALID_HANDLE AND bReadCom THEN

    numberOfbytes := SysComRead(hCom:= hCom, pbyBuffer:= ADR(temp_rdData), ulSize:= SIZEOF(temp_rdData), ulTimeout:= 100, pResult:= ADR(Result));

    IF numberOfbytes > 0 THEN
        FOR iCount :=0 TO numberOfbytes - 1 DO
            rdData[iCountG]:=temp_rdData[iCount];
            iCountG := iCountG + 1;
        END_FOR

        IF rdData[0] <> ctrl.STX  THEN
            beginNewRead:=TRUE;
        END_IF

    END_IF
END_IF


ton_read(IN:= NOT bReadCom, PT:=T#1000MS);
IF ton_read.Q THEN
    bReadCom:=TRUE;
END_IF


IF find_chars(ADR(rdData), iCountG, ctrl.STX, ctrl.ETX) AND (calc_CRC(ADR(rdData), iCountG) = read_CRC(ADR(rdData), iCountG)) THEN  
    bReadCom:=FALSE;
    bWriteAck := TRUE;
    beginDecode:=TRUE;      
    beginNewRead:=TRUE; 
END_IF


IF beginNewRead THEN
    iCountG:=0;
    SysMemSet(ADR(rdData),0,SIZEOF(rdData));
    SysMemSet(ADR(temp_rdData),0,SIZEOF(temp_rdData));
    beginNewRead:=FALSE;
END_IF
 

Last edit: damian177 2022-02-11