MODBUS commands with a CRC checksum - Sample Script: ModbusCrcScript.pts

Top  Previous  Next

The Docklight script ModbusCrcScript.pts and the accompanying project file ModbusCrcTest.ptp demonstrate how to use a Docklight script to automatically calculate the CRC value required to send a valid MODBUS RTU frame.

 

The project file uses the communication settings listed below, according to the MODBUS implementation class "Basic".

 

Communication Mode

Send/Receive

Send/Receive on comm. channel

COM1

COM Port Settings

9600 Baud, Even parity, 8 Data Bits, 1 Stop Bit

 

Getting started

 

Open the project file ModbusCrcTest.ptp (menu  OPEN Open Project ...) and the script file ModbusCrcScript.pts (menu Open Script ...). The files are located in the ScriptSamples folder, e.g. at C:\Program Files\FuH\Docklight Scripting V1.9\ScriptSamples.
Connect the PC's COM port to your MODBUS network. Open the PROPERTIES Project Settings... dialog and make sure you have selected the correct COM Port for Send/Receive on comm. channel.
Press the Startscript_new Run Script button in the toolbar.
Use the Pt_Send_Button Send button to read input register no. 1 from a slave.
Enter a slave number in the Send Sequence Parameter dialog, e.g. "01" for addressing slave no. 1.

 

Docklight now executes the DL_OnSend() procedure which calculates the actual CRC value and overwrites the last two Send Sequence bytes with the CRC low and high bytes.

 

After sending "Read Input Register" commands to slaves 1 - 4,  the communication window could look like this:

 

8/29/2008 18:45:23.193 [TX] - 01 04 00 00 00 01 31 CA

8/29/2008 18:45:23.342 [RX] - 01 04 02 FF FF B8 80

8/29/2008 18:45:33.145 [TX] - 02 04 00 00 00 01 31 F9

8/29/2008 18:45:33.292 [RX] - 02 04 02 27 10 E7 0C

8/29/2008 18:45:43.237 [TX] - 03 04 00 00 00 01 30 28

8/29/2008 18:45:43.392 [RX] - 03 04 02 00 00 C0 F0

8/29/2008 18:45:58.724 [TX] - 04 04 00 00 00 01 31 9F

8/29/2008 18:45:58.870 [RX] - 04 04 02 04 00 77 F0

 

The [RX] channel shows the responses from the MODBUS slaves:

slave 1 responded value "-1",

slave 2 responded "10000",

slave 3 responded "0" and

slave 4 responded "1024".

 

NOTE: If you are using the Docklight MODBUS example on a RS485 bus, you need to check if your RS485 hardware correctly switches between transmit and receive state. You might need to use Docklight's RS485 Transceiver Control feature.

 

Further Information

 

See Generating Checksums for Send Sequences for more general information on implementing checksum calculations.
The CRC calculation is made according to the specifications for MODBUS serial line transmission (RTU mode). Docklight's checksum function supports a "CRC-MODBUS" model for this purpose. See the documentation for DL.CalcChecksum for details.
If you do not have any MODBUS slave devices available, you can use a software simulator. See http://www.modbus.org, "Modbus Resources", "MODBUS Serial RTU Simulator". This simulator was used to produce the sample data shown above.