Reading from serial communication

damian177
2021-12-22
2021-12-22
  • damian177 - 2021-12-22

    Hi,
    I try do communication on RS232. I do write Data but I have a problem with read Data. Someone can hel me with code to read data form RS232 ?

    In the following code:

    VAR
       rdData: ARRAY[0..41] OF BYTE;  //buf
    END_VAR
    IF hCom <> RTS_INVALID_HANDLE AND bReadCom THEN
        // Read data
        dwRead := SysComRead(hCom:= hCom, pbyBuffer:= ADR(rdData), ulSize:= SIZEOF(rdData), ulTimeout:= 100, pResult:= ADR(Result));
        IF dwRead > 0 THEN
            ///??
        END_IF
        bReadCom:=FALSE;
    
    END_IF
    

    I would like save bytes to my bufor beggining <stx> byte (including) and ending on <ext> + last byte(wher Should be CRC sum). How do this ?</ext></stx>

     
  • damian177 - 2021-12-22

    I done something like this. It works. But probably it not an optimal solution:

    //timeout to receive  complete message
    tonRcv(IN:= (iCountG>0) AND NOT tonRcv.Q, PT:=T#20S);
    
    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
        END_IF
        //check if all data and  CRC sum OK 
            IF find_chars(ADR(rdData), UDINT_TO_BYTE(iCountG), ctrl.STX, ctrl.ETX) THEN 
                IF calc_CRC(ADR(rdData), UDINT_TO_BYTE(iCountG)-1) = rdData[iCountG-1] THEN
                    iCountG:=0; 
                    weHaveWholePackage:=TRUE; 
                    bReadCom:=FALSE;    
                END_IF; 
            END_IF;
    
    END_IF
    
     

Log in to post a comment.