CRC16 Verification Algorithm of VB)

Source: Internet
Author: User

'Data is the starting point of the struct array 'start'. The size is a few, and the returned value is CRC.
Public Function CRC16 (Data () As Byte, start As Integer, size As Integer) As Long
Dim CRC16Lo As Byte, CRC16Hi As Byte 'crc register
Dim CL As Byte, CH As Byte 'polynomial code & 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 size-1
CRC16Lo = CRC16Lo Xor Data (start + I) 'Each Data is different from the CRC register or
For Flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2' move one digit to the right
CRC16Lo = CRC16Lo \ 2' move one position to the right
If (SaveHi And & H1) = & H1) then', If the last bit of the high byte is 1
CRC16Lo = CRC16Lo Or & H80 then the first byte after right shift
End If 'otherwise, auto-fill 0
If (SaveLo And & H1) = & H1) then' If LSB is 1, it is different from the polynomial code or
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next I
CRC16 = CRC16Hi 'crc high
CRC16 = CRC16 * 256
CRC16 = CRC16 + crc16lo' CRC low
End Function

 

Lookup method:

Private Sub CRC16 (data () As Byte)
Dim CRC16Hi As Byte
Dim CRC16Lo As Byte
ReDim ReturnData (2) As Byte
CRC16Hi = & HFF
CRC16Lo = & HFF
Dim I As Integer
Dim iIndex As Long
For I = 0 To UBound (data)
IIndex = CRC16Lo Xor data (I)
CRC16Lo = CRC16Hi Xor GetCRCHi (iIndex) 'low-level processing
CRC16Hi = GetCRCLo (iIndex) 'high level processing
Next I

ReturnData (0) = CRC16Hi 'crc high
ReturnData (1) = crc16lo' CRC low

'For I = 0 To UBound (ReturnData)
'Text9.text = Text9.Text & Hex (ReturnData (I ))
'Next I
Text7.Text = Hex (ReturnData (1 ))
Text8.Text = Hex (ReturnData (0 ))
Text9.Text = Hex (ReturnData (1) & Hex (ReturnData (0 ))
End Sub

'Crc low byte value table
Function GetCRCLo (Ind As Long) As Byte
GetCRCLo = Choose (Ind + 1, & 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, & HFF, & 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 ,_
& 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)
End Function

'Crc high byte value table
Function GetCRCHi (Ind As Long) As Byte
GetCRCHi = Choose (Ind + 1, & 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)
End Function

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.