8
47D Series Modbus Reference Guide EM-9501-C Rev.4
2.4.3 CRC CALCULATION ALGORITHM
The following shows Visual Basic 6.0 sources to calculate the CRC16.
Function CRC16(Message() As Byte, Size As Integer) As Integer
Dim I As Integer, N As Integer, CarryBit As Boolean
CRC16 = &HFFFF
For I = 0 To Size - 1
CRC16 = (CRC16 And &HFF00) Or ((CRC16 And 255) Xor Message(I))
For N = 1 To 8
CarryBit = CRC16 And 1
CRC16 = ((CRC16 And &HFFFE) \ 2) And &H7FFF
If CarryBit Then CRC16 = &HA001 Xor CRC16
Next
Next
End Function
START
0xFFFF
→
CRC16
END
CRC16 XOR BYTE
→
CRC16
0
→
N
Shift CRC16 by 1 LSB
Carry Over
N + 1
→
N
YES
NO
NO
YES
YES
N ≥ 8
End of Message
CRC16 XOR 0xA001
→
CRC16
Figures starting with 0x are hexadecimal.
NO
Following BYTE