
Parameter Byte 1 Byte 2 Byte 3 Byte 4 Byte 5
Device Function Exception CRC CRC
Address Code Code (LSB) (MSB)
Illegal function XX 83 01 XX XX
Illegal data address XX 83 02 XX XX
The Modbus Cyclic Redundancy Routine (CRC) is defined as follows:
- First load the 16 bit CRC register with FFFF hex.
- Copy an 8 bit data character into the least significant byte of a 16 bit register, setting the
most significant byte (MSB) = 00 hex.
- Exclusive-or (XOR) this with the CRC register contents.
- If the least significant bit (LSB) of the result is 0, the CRC is shifted right.
- If the LSB is 1, the CRC is shifted right, then XOR'ed with A001 hex.
- This is repeated for all 8 bits of the data character, the most significant bit after the shift
is filled with a 0.
- After the 8th shift, the next data char is XOR'ed with the CRC's current value.
- Repeat for all characters.
- Transmit the resulting 2 byte CRC LSB first.
Modbus CRC programme in BASIC
Dim crc
Dim TempCrc
Dim i As Integer
Dim j As Integer
Dim Character
crc = 65535
For i = 1 To Len(message)
Character = Asc(Mid$(message, i, 1))
crc = crc Xor Character
For j = 1 To 8
TempCrc = (crc / 2)
If Int(TempCrc) <> TempCrc Then
TempCrc = Int(TempCrc)
TempCrc = TempCrc Xor 40961
End If
crc = TempCrc
Next j
Next i
Calculate CRC = crc