Modbus RTU With CRC checksum - Sample Project: ModbusRtuCrc.ptp

<< Click to Display Table of Contents >>

Navigation:  Examples and Tutorials >

Modbus RTU With CRC checksum - Sample Project: ModbusRtuCrc.ptp

The Docklight project file ModbusRtuCrc.ptp demonstrates how 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

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

 

Getting started

 

Open the project file ModbusRtuCrc.ptp (menu OPEN Open Project ...). The file is located in the \Samples folder.

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.

Click the Pt_Send_Button Send button in the Read Input Register Slave=?.. line of the Send Sequence list

Enter a slave number in the Send Sequence Parameter dialog, e.g. "01" for addressing slave no. 1.

 

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

 

23.09.2019 07:04:56.170 [TX] - 01 04 00 03 00 01 C1 CA

23.09.2019 07:04:56.282 [RX] - 01 04 02 FF FF B8 80

Detected Modbus Frame = 01 04 02 FF FF B8 80

SlaveID=01

FunctionCode=04

Addr/Data=02 FF FF

CRC=B8 80

 

Input Register Answer: Slave=001 ValueHex=FFFF

 

23.09.2019 07:05:21.761 [TX] - 02 04 00 03 00 01 C1 F9

23.09.2019 07:05:21.873 [RX] - 02 04 02 7F 58 DC FA

Detected Modbus Frame = 02 04 02 7F 58 DC FA

SlaveID=02

FunctionCode=04

Addr/Data=02 7F 58

CRC=DC FA

 

Input Register Answer: Slave=002 ValueHex=7F58

 

23.09.2019 07:05:35.713 [TX] - 03 04 00 03 00 01 C0 28

23.09.2019 07:05:35.824 [RX] - 03 04 02 01 0A 41 67

Detected Modbus Frame = 03 04 02 01 0A 41 67

SlaveID=03

FunctionCode=04

Addr/Data=02 01 0A

CRC=41 67

 

Input Register Answer: Slave=003 ValueHex=010A

 

23.09.2019 07:05:51.677 [TX] - 04 04 00 03 00 01 C1 9F

23.09.2019 07:05:51.789 [RX] - 04 04 02 40 00 44 F0

Detected Modbus Frame = 04 04 02 40 00 44 F0

SlaveID=04

FunctionCode=04

Addr/Data=02 40 00

CRC=44 F0

 

Input Register Answer: Slave=004 ValueHex=4000

 

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

slave 1 responded value "-1",

slave 2 responded "32600",

slave 3 responded "266" and

slave 4 responded "16384".

 

NOTE: If you are using the Docklight Modbus example on a RS485 bus and do not see a device answer, check if your RS485 hardware interface automatically switches between transmit and receive mode, or you need to use the RS485 Transceiver Control option.

 

Further Information

 

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 Calculating and Validating Checksums for more general information on implementing checksum calculations.

If you do not have any Modbus slave devices available, you can use a software simulator. See the www.plcsimulator.org/ as originally mentioned on www.modbus.org, "Modbus Technical Resources", "Modbus Serial RTU Simulator". This simulator was used to produce the sample data shown above.