
91
Modbus
(
RTU
)
Protocol
get the CRC16 check code
At least 3.5 character time squelch interval is requested
11.1.2
CRC-16 Calculation Method
1.
Set the initial value of the CRC-16 register to 0xFFFF.
2.
Perform XOR calculation fo CRC-16 register and information of the first byte, then it
return the result to the CRC register.
3.
Fill in the MSB with 0 and use the CRC register to shift to the right by one bit.
4.
If the bit moved from the LSB is "0", repeat step (3) (process the next shift). If the bit
moved from the LSB is "1", will perform XOR calculation for the CRC register and 0xA001,
then the result is returned to the CRC register.
5.
Repeat steps (3) and (4) until you move 8 bits.
6.
If the information processing has not been completed, perform XOR calculation for CRC
register and information of the next Byte, and return to the CRC register, repeating from
step (3).
7.
Append the result of the calculation (the value of the CRC register) from the low byte to
the message.
The following is a CRC calculation function for a VB language.
Function CRC16(data() As Byte) As Byte()
Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC register
Dim CL As Byte, CH As Byte '
Polynomial code &HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim i As Integer
Dim flag As Integer
CRC16Lo = &HFF
CRC16Hi = &HFF
CL = &H1
CH = &HA0
For i = 0 To UBound(data)
CRC16Lo = CRC16Lo Xor data(i) 'Each data is XORed with the CRC register
For flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2 '
High bit right shift one bit
CRC16Lo = CRC16Lo \ 2 '
Low bit right shift one bit
If ((SaveHi And &H1) = &H1) Then '
If the last bit of the high byte
is 1
CRC16Lo = CRC16Lo Or &H80 'Then the low byte is shifted to the
right and then supplement 1 at front
End If '
Otherwise automatically fill 0
If ((SaveLo And &H1) = &H1) Then '
If the LSB is 1, then XOR with
the polynomial code
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next flag
Next i
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi 'CRC high bit
ReturnData(1) = CRC16Lo 'CRC low bit
CRC16 = ReturnData
End Function
Reference
:
My company's "Communication Test Tool", which has Modbus communication debugging method. It
contains the CRC-16 calculator.
Calculate the CRC-16 data to be appended to the end of the command frame, for example:
1234H:
Figure 11-2
Modbus append CRC-16 value