Function CRC16(data() As Byte) As bbbbbb ’CRC計算函數(shù)
Dim CRC16Lo As Byte, CRC16Hi As Byte ’CRC寄存器
Dim CL As Byte, CH As Byte ’多項式碼&HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim I As Integer
Dim Flag As Integer
CRC16Lo = &HFF
CRC16Hi = &HFF
CL = &H1
CH = &HA0
For I = 0 To UBound(data)
CRC16Lo = CRC16Lo Xor data(I) ’每一個數(shù)據(jù)與CRC寄存器進行異或
For Flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2 ’高位右移一位
CRC16Lo = CRC16Lo \ 2 ’低位右移一位
If ((SaveHi And &H1) = &H1) Then ’如果高位字節(jié)最后一位為1
CRC16Lo = CRC16Lo Or &H80 ’則低位字節(jié)右移后前面補1
End If ’否則自動補0
If ((SaveLo And &H1) = &H1) Then ’如果LSB為1,則與多項式碼進行異或
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next I
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi ’CRC高位
ReturnData(1) = CRC16Lo ’CRC低位
asd = Right("00" + Hex(CRC16Lo), 2) + Right("00" + Hex(CRC16Hi), 2)
End Function
程序是不是很簡單呀!!!!!










