Modbus it is:
Initialize the checksum as FFFFhex.
For each byte
Checksum = Checksum XOR (current byte)
For I = 0 to 7
If ( ( Checksum AND 1 ) = 0 )
Checksum = Right_Bit_Shift Checksum 1 bit
Else
Checksum = (Right_Bit_Shift Checksum 1 bit) XOR A001 hex
Next I
Next Byte
'CRC-16/MODBUS
Function CRC16(Dataa As String)
Dim chksum As Integer = 65535
For i = 1 To Dataa.Length
chksum = ByteCRC(chksum, Asc(Mid(Dataa, i, 1)))
Next i
CRC16 = Hex(chksum)
End Function
Function ByteCRC(sum As Integer, dat As Integer) As Integer
sum = sum Xor dat
For i = 0 To 7
If ((sum And 1) = 0) Then
sum = Int(sum / 2) ' /2 = Right Shift
Else
sum = Int(sum / 2) Xor 40961 ' A001 Hex
End If
Next i
ByteCRC = sum
End Function