Private Function CRC16(data As String, mode As Integer) As byte
Dim buffer() As Byte = HexStringToBytes(data)
Const CRC_POLYNOM As Long = &H8408&
Const CRC_PRESET As Long = &HFFFF&
Dim CRC As Long, I As Long, J As Long
CRC = CRC_PRESET 'unsigned internal CRC = CRC_PRESET
For I = 0 To UBound(buffer) 'for(i=0; i < cnt; i++)
CRC = CRC Xor buffer(I) 'crc ^= Buffer
For J = 0 To 7 'for(j=0; j<8;j++)
If (CRC And 1) Then 'if(crc & 0x0001)
CRC = (CRC \ 2) Xor CRC_POLYNOM 'crc=(crc>>1)^CRC_POLYNOM;
Else
CRC = (CRC \ 2) 'crc=(crc>>1)
End If
Next J
Next I
CRC16 = (Not CRC) And &HFFFF&
return CRC16
End Function
Dim buffer() As Byte = HexStringToBytes(data)
Const CRC_POLYNOM As Long = &H8408&
Const CRC_PRESET As Long = &HFFFF&
Dim CRC As Long, I As Long, J As Long
CRC = CRC_PRESET 'unsigned internal CRC = CRC_PRESET
For I = 0 To UBound(buffer) 'for(i=0; i < cnt; i++)
CRC = CRC Xor buffer(I) 'crc ^= Buffer
For J = 0 To 7 'for(j=0; j<8;j++)
If (CRC And 1) Then 'if(crc & 0x0001)
CRC = (CRC \ 2) Xor CRC_POLYNOM 'crc=(crc>>1)^CRC_POLYNOM;
Else
CRC = (CRC \ 2) 'crc=(crc>>1)
End If
Next J
Next I
CRC16 = (Not CRC) And &HFFFF&
return CRC16
End Function