Example for sending/receiving with “SysLibCom”

The following example shows how data are sent/received with the protocol “SysLibCom”.

-> Telegrams of 32 bytes length are to be received and sent.

  1. 1. Setting in PLC configuration:

  2. 2. Declaration part of the program PROGRAM proSysLibCom_Test

    VAR
      strComSettings : COMSETTINGS; (* Structure of COM settings *)
      dwHandle : DWORD;  
      byStep : BYTE; (* Step chain *)
      dwRead : DWORD; (* Number of characters received *)
      dwWritten : DWORD; (* Number of characters sent *)
      bEnSend : BOOL; (* Enable sending *)
      byCom : BYTE := COM2; (* COM number *)
      dwBaudrate : DWORD := 19200; (* Baudrate *)
      wLenRec : WORD := 32; (* Number of characters to be received *)
      wLenTele : WORD := 32; (* Telegram length, here 32 characters for example *)
      wLenSend : WORD := 32; (* Number of characters to be sent, for example 32 characters *)
      dwTimeoutSend : DWORD := 0; (* Timeout in [ms] for sending *)
      dwTimeoutRec : DWORD := 0; (* Timeout in [ms] for receiving *)
      abyRecBuffer : ARRAY[0..271] OF BYTE; (* Receive butter, 272 bytes min.! *)
      abyTeleBuffer : ARRAY[0..543] OF BYTE; (* Telegram buffer, 2 x receive buffer min. *)
      aby SendBuffer : ARRAY[0..271] OF BYTE; (* Send buffer, telegram length max.! *)
      strDataRec : StrucReceiveData; (* Structure of receive telegram *)
      strDataSend : StrucSendData (* Structure of send telegram *)
    END_VAR
  3. 3. Code part of the program

    -> Processing of a step chain containing the following steps

    CASE byStep OF
    0: (* Step 0: Open the interface COMx -> SysComOpen -> get handle *)
      strComSettings.Port := byCom; (* COM_Number *)
      dwHandle := SysComOpen(strComSettings.Port); (* Open COM interface -> get handle *)
      byStep := SEL( dwHandle <> INVALID_HANDLE, 250, 1); (* handle ok -> Step 1, otherwise error step 250 *)
               
    1: (* Step 1: Transfer of COMx interface parameters *)
      strComSettings.dwBaudRate := dwBaudrate; (* Set baudrate *)
      (* Enter at this point the number of stop bits and parity, if necessary *)
               
      (* set COM settings -> if OK, run step 2, in case of an error step 250 *)
      byStep := SEL( SysComSetSettings(dwHandle, ADR(strComSettings)), 250, 2);
               
    2: (* Step 2: Initialization completed successfully -> now receiving and/or sending *)
               
      (* Receive data: read all data received since last run, but wLenRec max.! *)
      dwRead := SysComRead( dwHandle,  
         

    ADR(abyRead), W

    ORD_TO_DWORD(wLenRec),

    dwTimeoutRec);

    (* READ DATA *)
      IF (dwRead > 0) THEN (* Number of characters received; in bytes *)
        (* here, ignore for example all characters until valid telegram start detected *)
         
        (* Number of receiving cycles for the telegram *)
        dwNumReadPerTele[byCom] := dwNumReadPerTele[byCom] + 1;
        (* Copy data to buffer *)
        SysMemCpy(

    dwDest := ADR(abySumDataRead[dwSum]DataRead]),

    dwSrc := ADR(abyRead[0]),

    dwCount := dwRead );

        (* Sum of read data of a telegram *)
        dwSumDataRead := dwSumDataRead + dwRead;
        IF dwSumDataRead >= wLenTele THEN (* Telegram complete ? *)
          dwRecCount := dwRecCount +1; (* Number of telegrams received *)
          (* Copy received telegram to structure strDataRec *)
          SysMemCpy( dwDest := ADR(strDataRec,
           

    dw Src := ADR(abySumDataRead[0]),

    dwCount := wLenTele );

          dwNumReadPerTele := 0; (* init for following telegram *)
          dwSumDataRead := 0;  
               
          (* here the evaluation of data starts !!! *)
               
        END_IF; (* Telegram complete *)
      END_IF; (* Data received *)
               
      (* Send data *)
      (* Enabling the sending of data can be done, for example, cyclical or by program control *)
      IF bEnSend THEN (* Enable sending *)
        (* Copy data to be sent to send buffer *)
        SysMemCpy(

    dwDest: := ADR(abyDataSend[0]),

    dwSrc := ADR(strDataSend),

    dwCount := wLenSend);

        (* Send data *)
        dwWritten := SysComWrite( dwHandle,  
           

    ADR(abyDataSend[0]),

    WORD_TO_DWORD(wLenSend),

    dwTimeoutSend);

    SEND DATA !!! *)
        IF (dwWritten <> wLenSend THEN  
          byStep := 250; (* Error when sending *)
        END_IF; (* dwWritten *)
        bEnSend := FALSE; (* Deactivate enable sending *)
      END_IF; (* bEnSend *)
               
    250: (* Step 250: Error step -> Close COMx and start with step 0 *)
     

    bResult := SysComClose(dwHandle);

    dwHandle := 0;

    byStep := 0;

    (* Close COM interface *)
               
    END_CASE; (* End of step chain *)