|
Generating Checksums for Send Sequences (Scripting) |
|
|
Many communication protocols include additional checksum fields to ensure data integrity and detect transmission errors. A common algorithm is the CRC (Cyclic Redundancy Code) method, which is used in different variations for different protocols. The following step-by-step example describes how to implement a checksum algorithm, and how to define Send Sequences with a simple checksum that is calculated on-the-fly.
TIP: For a working example to address a MODBUS slave device, see the step-by-step tutorial Sending MODBUS commands with CRC-16 checksum.
Preconditions
You need a specification for your protocol messages, in particular the message length, the position of the checksum / CRC bytes, and how the checksum is calculated.
Defining Send Sequences that include a checksum
01 | 02 | 03 | 04 | 05
01 | 02 | 03 | 04 | 05 | 00
Repeat steps 1 - 4 to define other commands needed to perform your test.
TIP: CRC checksums are especially easy to implement using Docklight Scripting's CRC functionality. See the DL.CalcChecksum method for details.
Performing the test
1. Choose Scripting > 2. Use the
Docklight now executes the DL_OnSend() procedure code which calculates the actual checksum and overwrites the "00" placeholder with the checksum value. For our simple example command defined above, and the checksum algorithm from the DL_OnSend() Example, the transmitted sequence looks like this:
8/29/2008 21:07:23.251 [TX] - 01 02 03 04 05 0F
The placeholder has been replaced by the sum over the message bytes: 1 + 2 + 3 + 4 + 5 = 15 or HEX 0F
|