• Home
  • Company
    • About
    • Customers
    • Testimonials
    • Contact Us
    • My Account
  • Products
  • Downloads
  • Tutorials
  • Professional Services
  • Home
  • Company
    • About
    • Customers
    • Testimonials
    • Contact Us
    • My Account
  • Products
  • Downloads
  • Tutorials
  • Professional Services
Free Edition Compatible
How to Communicate over Modbus-RTU
Objectives
In this tutorial you'll learn HOW TO:
► Communicate to a Modbus device using Coronys ETS
► Calculate Modbus-RTU CRC
► Extract data from the Modbus device response
All Tutorials
How to ...
Add a New Test Step
Right click on any sibling leaf to add a new test step "Modbus RTU CRC" on Test Step Tree.
No parameters are required for this test script.
Write a Script
The script performs the following:
► Calculate Modbus CRC for a Modbus command "01 03 00 14 00 3C" (CRC is "05 DF").
    Define external parameters for your real automation.
► Send a Modbus command with CRC over COM1 with baud rate 9600 bps.
    Use RC_TelnetPortSet instead of RC_SeralPortSet to communicate over Telnet.
► Read the "response with some delay to let your target "think".
► Extract the data part from the response
► Calculate the Modbus checksum and compare it with the received one.
► If the CRC is wrong, fail the test.

Copy the script below and paste it to the template created by the ESL Editor.
Click F1 in ESL Editor for Help and API index.
Adjust the code to you needs and run.
Declare  PortID ,  Data
     Declare  MyCmd  =   "0x1,0x3,0x0,0x14,0x0,0x3C"   ; command to be sent

     GoSub   "Sub.CalcModBusCRC" ,  MyCmd   ; Calculate Modbus CRC
     NumToList   _RetVal , _RetVal , 2 , 1       ; Convert CS to list of bytes (F! to see Help)
    
     ; Print command and CRC to the ETS terminal (Blue window)
     HBClear
     PrintMessage   "Sent: Command | "   $  MyCmd
     PrintMessage   "          CRC | "   $   _RetVal
    
     ; Use RC_TelnetPortSet to communicate over Telnet
     RC_SerialPortSet  PortID ,   "COM1" ,   9600   ; Init Serial port on COM1
    MyCmd  $=   ","   $   _RetVal
     RC_Send  MyCmd ,  PortID ,   - 1 ,   1       ; Send as a binary data (the last argument set to 1)
     Delay   100                          ; It takes time until the target responces.    
     RC_Read  Data ,  PortID ,   1            ; Read the response data in binary format

     ; Convert the Data to HEX format
     StrConvertValueRadix  Data ,   16 ,   10 ,   1
     Print   "Received      | "   $  Data  $   "^M^J"

     ; Analyze recevied Data
     Declare  Num ,  CRC ,  Cmd
     StrGetFieldElements  Num ,  Data ,   ","   ; Get number of bytes in message
     StrField  Cmd ,  Data ,   0 ,   "," ,  Num - 2
     StrField  CRC ,  Data ,  Num - 2 ,   "," ,   2

     GoSub   "Sub.CalcModBusCRC" ,  Cmd      ; Calculate Modbus CRC

     ListToNum  CRC ,  CRC ,   2                ; Convert to U16 number
     If   _RetVal   !=  CRC  Then   TMMessage   GT_ErrorID ,   "Wrong CRC..."  
    
     ; Close the port
     RC_Close  PortID
    
     End  
     ;-------------------------------------------
    
<Sub.CalcModBusCRC>  i_Cmd
     Declare  Byte ,  Cnt ,  Carry ,  CS
    CS  =   0xffff
     ForEach  Byte  in  i_Cmd
        CS  ^=  Byte
         For  Cnt = 1   To   8            ; Loop over each bit
            Carry  =  CS  &   1        ; Extract LSB
            CS  >>=   1              ; Shift right
             If  Carry == 1   Then  CS  ^=   0xA001   ; XOR 0xA001 if LSB is set 
         Next
     EndEach
     Return  CS
  • Home
  • Products
  • Downloads
  • Tutorials
  • Contact Us
  • Terms of Use
  • Privacy Policy

Copyright © 2002-2017 Coronys Ltd. All Rights Reserved.
 
Back to top