Public chCRCHTalbe As UInteger() = {&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H0, &Hc1, &H81, &H40, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H0, &Hc1, _
&H81, &H40, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H0, &Hc1, &H81, &H40, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H0, &Hc1, &H81, &H40, _
&H1, &Hc0, &H80, &H41, &H1, &Hc0, _
&H80, &H41, &H0, &Hc1, &H81, &H40, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H0, &Hc1, &H81, &H40, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H0, &Hc1, _
&H81, &H40, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H0, &Hc1, _
&H81, &H40, &H1, &Hc0, &H80, &H41, _
&H1, &Hc0, &H80, &H41, &H0, &Hc1, _
&H81, &H40, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H0, &Hc1, _
&H81, &H40, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H0, &Hc1, &H81, &H40, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H0, &Hc1, _
&H81, &H40, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40, &H1, &Hc0, _
&H80, &H41, &H1, &Hc0, &H80, &H41, _
&H0, &Hc1, &H81, &H40}
Public chCRCLTalbe As Uinteger() = {&H0, &Hc0, &Hc1, &H1, &Hc3, &H3, _
&H2, &Hc2, &Hc6, &H6, &H7, &Hc7, _
&H5, &Hc5, &Hc4, &H4, &Hcc, &Hc, _
&Hd, &Hcd, &Hf, &Hcf, &Hce, &He, _
&Ha, &Hca, &Hcb, &Hb, &Hc9, &H9, _
&H8, &Hc8, &Hd8, &H18, &H19, &Hd9, _
&H1b, &Hdb, &Hda, &H1a, &H1e, &Hde, _
&Hdf, &H1f, &Hdd, &H1d, &H1c, &Hdc, _
&H14, &Hd4, &Hd5, &H15, &Hd7, &H17, _
&H16, &Hd6, &Hd2, &H12, &H13, &Hd3, _
&H11, &Hd1, &Hd0, &H10, &Hf0, &H30, _
&H31, &Hf1, &H33, &Hf3, &Hf2, &H32, _
&H36, &Hf6, &Hf7, &H37, &Hf5, &H35, _
&H34, &Hf4, &H3c, &Hfc, &Hfd, &H3d, _
&Hff, &H3f, &H3e, &Hfe, &Hfa, &H3a, _
&H3b, &Hfb, &H39, &Hf9, &Hf8, &H38, _
&H28, &He8, &He9, &H29, &Heb, &H2b, _
&H2a, &Hea, &Hee, &H2e, &H2f, &Hef, _
&H2d, &Hed, &Hec, &H2c, &He4, &H24, _
&H25, &He5, &H27, &He7, &He6, &H26, _
&H22, &He2, &He3, &H23, &He1, &H21, _
&H20, &He0, &Ha0, &H60, &H61, &Ha1, _
&H63, &Ha3, &Ha2, &H62, &H66, &Ha6, _
&Ha7, &H67, &Ha5, &H65, &H64, &Ha4, _
&H6c, &Hac, &Had, &H6d, &Haf, &H6f, _
&H6e, &Hae, &Haa, &H6a, &H6b, &Hab, _
&H69, &Ha9, &Ha8, &H68, &H78, &Hb8, _
&Hb9, &H79, &Hbb, &H7b, &H7a, &Hba, _
&Hbe, &H7e, &H7f, &Hbf, &H7d, &Hbd, _
&Hbc, &H7c, &Hb4, &H74, &H75, &Hb5, _
&H77, &Hb7, &Hb6, &H76, &H72, &Hb2, _
&Hb3, &H73, &Hb1, &H71, &H70, &Hb0, _
&H50, &H90, &H91, &H51, &H93, &H53, _
&H52, &H92, &H96, &H56, &H57, &H97, _
&H55, &H95, &H94, &H54, &H9c, &H5c, _
&H5d, &H9d, &H5f, &H9f, &H9e, &H5e, _
&H5a, &H9a, &H9b, &H5b, &H99, &H59, _
&H58, &H98, &H88, &H48, &H49, &H89, _
&H4b, &H8b, &H8a, &H4a, &H4e, &H8e, _
&H8f, &H4f, &H8d, &H4d, &H4c, &H8c, _
&H44, &H84, &H85, &H45, &H87, &H47, _
&H46, &H86, &H82, &H42, &H43, &H83, _
&H41, &H81, &H80, &H40}
Public Function CRC16(pchMsg As uinteger(), wDataLen As ushort) As ushort
Dim chCRCHi As uinteger = &Hff
' 高CRC 字节初始化
Dim chCRCLo As uinteger = &Hff
' 低CRC 字节初始化
Dim wIndex As ushort
Dim i As ushort = 0
' CRC 循环中的索引
While wDataLen > 0
' 计算CRC
wIndex = chCRCLo Xor pchMsg(i)
i += 1
chCRCLo = chCRCHi Xor chCRCHTalbe(wIndex)
chCRCHi = chCRCLTalbe(wIndex)
wDataLen -= 1
End While
Return ((chCRCHi << 8) Or chCRCLo)
End Function