damian177 - 2022-07-01

Hi, I read data from device by RS232 like this:

////////////////////////receiving data///////////////
//wait for all data
tonRcv(IN:= (iCountG>0) AND NOT tonRcv.Q, PT:=T#1000MS);


IF hCom <> RTS_INVALID_HANDLE AND bReadCom THEN
    // Complete the data
    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

        IF iCountG >= SIZEOF(rdData)-1 THEN
            overFlowBuf:=TRUE;
            bWriteNak := TRUE;
            beginNewRead:=TRUE;
        END_IF

    END_IF
END_IF


ctud_buf(CU:= overFlowBuf);
overFlowBuf:=FALSE;

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

//If all data and control sum OK
IF find_chars(ADR(rdData), iCountG, ctrl.STX, ctrl.ETX) AND (calc_LRC(ADR(rdData), iCountG) = read_LRC(ADR(rdData), iCountG)) THEN  
    bReadCom:=FALSE;
    bWriteAck := TRUE;
    beginDecode:=TRUE;      
    beginNewRead:=TRUE;
    iiLRCok:=iiLRCok+1; 
END_IF

//If all data and control sum not OK
IF find_chars(ADR(rdData), iCountG, ctrl.STX, ctrl.ETX) AND NOT (calc_LRC(ADR(rdData), iCountG) = read_LRC(ADR(rdData), iCountG)) THEN
    bReadCom:=FALSE;
    bWriteNak := TRUE;
    beginNewRead:=TRUE;
    iiLRCbad:=iiLRCbad+1;   
END_IF


IF  tonRcv.Q THEN
     bReadCom:=FALSE;
     beginNewRead:=TRUE;
     iiTimeout:=iiTimeout+1;
END_IF


//decode data
rt_decode(CLK:=beginDecode);
beginDecode:=FALSE;
encode1(enable:=rt_decode.Q, array_input:=ADR(rdData), array_count:=SIZEOF(rdData));


//ready to next data
IF beginNewRead THEN
    iCountG:=0;
    SysMemSet(ADR(rdData),0,SIZEOF(rdData));
    SysMemSet(ADR(temp_rdData),0,SIZEOF(temp_rdData));
    beginNewRead:=FALSE;
END_IF

but it not optymally code... How change this code to read frame beetwen <stx> nad <etx> char ....?</etx></stx>

 

Last edit: damian177 2022-07-01