VB中的CRC 校验问题,30高分
'CRC校验规则:
' An+1=TAB_CRC[(An xor Dn)*2+1] xor Bn
' Bn+1=TAB_CRC[(An xor Dn)*2]
' A0=FFH,B0=FFH,n=0-N(N=被校验数据字节数)。
'其中An+1 是CRC校验码第一字节,Bn+1是CRC校验码第二字节,Dn是被校验数据,TAB_CRC是数据表格,TAB_CRC[ ]表示查表, xor表示异或。
'校验:DATA=01 03 ,CRC=40 21, DATA=01 04 06 04 0B 13 01,CRC=3E 46。DATA=01 03 00 20 00 16 CRC=C5 CE。
'以下是我写的代码,用的第2组DATA,可死活对不上CRC的校验,郁闷。
Private Sub Command1_Click()
Dim CRC() As Byte
Dim d() As Byte '待传输数据
ReDim d(6) As Byte‘这里用第2组DATA
d(0) = &H1
d(1) = &H3
d(2) = &H0
d(3) = &H20
d(4) = &H0
d(5) = &H16
CRC = CRC16(d) '调用CRC16计算函数
End Sub
Private Function CRC16(data() As Byte) As String
Dim CRC16Hi As Byte
Dim CRC16Lo As Byte
CRC16Hi = &HFF
CRC16Lo = &HFF
Dim I As Integer
Dim iIndex As Long
For I = 0 To UBound(data) - 1
CRC16Hi = GetCRC((CRC16Hi Xor data(I)) * 2 + 1) Xor CRC16Lo '高位处理
CRC16Lo = GetCRC((CRC16Hi Xor data(I)) * 2) '低位处理
Next I
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi 'CRC高位
ReturnData(1) = CRC16Lo 'CRC低位
CRC16 = ReturnData
sss = 0
End Function
'CRC字节值表
Function GetCRC(Ind As Long) As Byte
GetCRC = Choose(Ind + 1, &H0, &H0, &HC0, &HC1, &HC1, &H81, &H1, &H40, &HC3, &H1, &H3, &HC0, &H2, &H80, &HC2, &H41, &HC6, &H1, &H6, &HC0, &H7, _
&H80, &HC7, &H41, &H5, &H0, &HC5, &HC1, &HC4, &H81, &H4, &H40, &HCC, &H1, &HC, &HC0, &HD, &H80, &HCD, &H41, &HF, &H0, &HCF, &HC1, &HCE, &H81, &HE, &H40, &HA, &H0, &HCA, _
&HC1, &HCB, &H81, &HB, &H40, &HC9, &H1, &H9, &HC0, &H8, &H80, &HC8, &H41, &HD8, &H1, &H18, &HC0, &H19, &H80, &HD9, &H41, &H1B, &H0, &HDB, &HC1, &HDA, &H81, &H1A, &H40, &H1E, _
&H0, &HDE, &HC1, &HDF, &H81, &H1F, &H40, &HDD, &H1, &H1D, &HC0, &H1C, &H80, &HDC, &H41, &H14, &H0, &HD4, &HC1, &HD5, &H81, &H15, &H40, &HD7, &H1, &H17, &HC0, &H16, &H80, &HD6, _
&H41, &HD2, &H1, &H12, &HC0, &H13, &H80, &HD3, &H41, &H11, &H0, &HD1, &HC1, &HD0, &H81, &H10, &H40, &HF0, &H1, &H30, &HC0, &H31, &H80, &HF1, &H41, &H33, &H0, &HF3, &HC1, &HF2, _
&H81, &H32, &H40, &H36, &H0, &HF6, &HC1, &HF7, &H81, &H37, &H40, &HF5, &H1, &H35, &HC0, &H34, &H80, &HF4, &H41, &H3C, &H0, &HFC, &HC1, &HFD, &H81, &H3D, &H40, &HFF, &H1, &H3F, _
&HC0, &H3E, &H80, &HFE, &H41, &HFA, &H1, &H3A, &HC0, &H3B, &H80, &HFB, &H41, &H39, &H0, &HF9, &HC1, &HF8, &H81, &H38, &H40, &H28, &H0, &HE8, &HC1, &HE9, &H81, &H29, &H40, &HEB, _
&H1, &H2B, &HC0, &H2A, &H80, &HEA, &H41, &HEE, &H1, &H2E, &HC0, &H2F, &H80, &HEF, &H41, &H2D, &H0, &HED, &HC1, &HEC, &H81, &H2C, &H40, &HE4, &H1, &H24, &HC0, &H25, &H80, &HE5, _
&H41, &H27, &H0, &HE7, &HC1, &HE6, &H81, &H26, &H40, &H22, &H0, &HE2, &HC1, &HE3, &H81, &H23, &H40, &HE1, &H1, &H21, &HC0, &H20, &H80, &HE0, &H41, &HA0, &H1, &H60, &HC0, &H61, _
&H80, &HA1, &H41, &H63, &H0, &HA3, &HC1, &HA2, &H81, &H62, &H40, &H66, &H0, &HA6, &HC1, &HA7, &H81, &H67, &H40, &HA5, &H1, &H65, &HC0, &H64, &H80, &HA4, &H41, &H6C, &H0, &HAC, _
&HC1, &HAD, &H81, &H6D, &H40, &HAF, &H1, &H6F, &HC0, &H6E, &H80, &HAE, &H41, &HAA, &H1, &H6A, &HC0, &H6B, &H80, &HAB, &H41, &H69, &H0, &HA9, &HC1, &HA8, &H81, &H68, &H40, &H78, _
&H0, &HB8, &HC1, &HB9, &H81, &H79, &H40, &HBB, &H1, &H7B, &HC0, &H7A, &H80, &HBA, &H41, &HBE, &H1, &H7E, &HC0, &H7F, &H80, &HBF, &H41, &H7D, &H0, &HBD, &HC1, &HBC, &H81, &H7C, _
&H40, &HB4, &H1, &H74, &HC0, &H75, &H80, &HB5, &H41, &H77, &H0, &HB7, &HC1, &HB6, &H81, &H76, &H40, &H72, &H0, &HB2, &HC1, &HB3, &H81, &H73, &H40, &HB1, &H1, &H71, &HC0, &H70, _
&H80, &HB0, &H41, &H50, &H0, &H90, &HC1, &H91, &H81, &H51, &H40, &H93, &H1, &H53, &HC0, &H52, &H80, &H92, &H41, &H96, &H1, &H56, &HC0, &H57, &H80, &H97, &H41, &H55, &H0, &H95, _
&HC1, &H94, &H81, &H54, &H40, &H9C, &H1, &H5C, &HC0, &H5D, &H80, &H9D, &H41, &H5F, &H0, &H9F, &HC1, &H9E, &H81, &H5E, &H40, &H5A, &H0, &H9A, &HC1, &H9B, &H81, &H5B, &H40, &H99, _
&H1, &H59, &HC0, &H58, &H80, &H98, &H41, &H88, &H1, &H48, &HC0, &H49, &H80, &H89, &H41, &H4B, &H0, &H8B, &HC1, &H8A, &H81, &H4A, &H40, &H4E, &H0, &H8E, &HC1, &H8F, &H81, &H4F, _
&H40, &H8D, &H1, &H4D, &HC0, &H4C, &H80, &H8C, &H41, &H44, &H0, &H84, &HC1, &H85, &H81, &H45, &H40, &H87, &H1, &H47, &HC0, &H46, &H80, &H86, &H41, &H82, &H1, &H42, &HC0, &H43, _
&H80, &H83, &H41, &H41, &H0, &H81, &HC1, &H80, &H81, &H40, &H40)
End Function
[解决办法]
CRC16Hi = GetCRC((CRC16Hi Xor data(I)) * 2 + 1) Xor CRC16Lo <-这里 CRC16HI 已改变 CRC16Lo = GetCRC((CRC16Hi Xor data(I)) * 2) <-这是用新的 CRC16HI 进行计算
用一个中间变量临时存放 CRC16Hi