Unknown Source
With the continuous development of computer technology, Industrial Control Using Microcomputer for data communication is also widely used in modern industry. Due to the influence of transmission distance, on-site conditions, and many other possible factors, the communication data between the computer and the controlled device often has unpredictable errors. To prevent the impact of errors, data verification is generally adopted during communication. Cyclic verification is one of the most common verification methods.
I. Principle of cyclic Verification Code
Cyclic verification (CRC. It uses the principle of division and remainder for error detection. In actual application, the sending device calculates the CRC value and sends it to the receiving device along with the data. The receiving device recalculates the CRC value for the received data and compares it with the received CRC value, if the two CRC values are different, the data communication error occurs.
Depending on the application environment and habits, CRC can be divided into the following standards:
(1) CRC-12 code;
(2) CRC-16 code;
③ CRC-CCITT code;
4 CRC-32 code.
The CRC-12 code is usually used to send 6-bit strings. CRC-16 and CRC-CCITT code is used to transmit 8-bit characters, the CRC-16 for the United States, and CRC-CCITT for European countries. Most CRC-32 codes are used in a synchronous transmission called point-to-point.
The following uses the most commonly used CRC-16 as an example to describe its generation process.
The CRC-16 code consists of two bytes. At the beginning, each bit of the CRC register is preset to 1, and then the CRC register and 8-bit data are different or, then the CRC register is shifted from high to low, and the position of the highest bit (MSB) is zero, and the percentile bit (LSB, which has been removed from the CRC register after the shift) is 1, the Register and the predefined polynomial code are exclusive or. Otherwise, if the LSB is zero, no exclusive or exclusive operation is required. Repeat the above eight shifts from high to low, and the first 8-bit data is processed, at this time, the CRC register value is different from the next 8-bit data and is shifted eight times like the previous data. After all characters are processed, the value in the CRC register is the final CRC value.
The following is the CRC calculation process:
1. Set the CRC register and assign FFFF (HEX) to it ).
2. the first 8-bit character of the data is the same or as the low 8-bit CRC register of the 16-bit CRC register, and the result is stored in the CRC register.
3. The CRC register shifts one bit to the right, and MSB zeros out and checks the LSB.
4. If LSB is 0, repeat Step 3. If LSB is 1, the CRC register is different from the polynomial code or.
5. Repeat steps 3rd and 4th until eight shifts are completed. At this time, an 8-bit data processing is complete.
6. Repeat steps 2nd to 5th until all data is processed.
7. the final content of the CRC register is the CRC value.
Ii. Compiling of the cyclic verification code verification program
After understanding the process of CRC verification code generation, it is very easy to compile a program. Due to the widespread popularity of Visual Basic and its important position in data communication, we will use VB to compile CRC generation programs. Other languages only need to be slightly modified.
There are two methods to compile the CRC verification program: one is the calculation method and the other is the table query method. The following two methods are discussed respectively.
1. Calculation Method
The calculation method is to design the program based on the principle of CRC verification code generation. The advantage is that the module has less code, flexible modification, and good portability. The disadvantage is the large computing capacity. For ease of understanding, here we assume three-digit data, and the polynomial code is a001 (HEX ).
Place a command button command1 on the form and add the following code:
Private sub commandementclick ()
Dim CRC () as byte
Dim D () as byte 'data to be transmitted
Redim D (2) as byte
D (0) = 123
D (1) = 112
D (2) = 135
CRC = crc16 (d) 'Call the crc16 calculation function
'Crc (0) is high
'Crc (1) is low
End sub
Note: During data transmission, the low level of CRC may be prior, while the high level may be later.
Function crc16 (data () as byte) as string
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 ubound (data)
Crc16lo = crc16lo XOR data (I) 'Each data is inconsistent with 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
Dim returndata (1) as byte
Returndata (0) = crc16hi 'crc high
Returndata (1) = crc16lo' CRC low
Crc16 = returndata
End Function
2. look-up table method
The advantages and disadvantages of the lookup method are the opposite of those of the calculation method. For ease of comparison, all the assumptions here are exactly the same as those of the calculation method, while a command1 button is placed on the form, and the code part is exactly the same as the above. The following describes only the source code of the CRC function.
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)
Iindex = crc16lo XOR data (I)
Crc16lo = crc16hi XOR getcrclo (iindex) 'low-level processing
Crc16hi = getcrchi (iindex) 'high level processing
Next I
Dim returndata (1) as byte
Returndata (0) = crc16hi 'crc high
Returndata (1) = crc16lo' CRC low
Crc16 = returndata
End Function
'Crc low byte value table
Function getcrclo (IND as long) as byte
Getcrclo = 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
'Crc high byte value table
Function getcrchi (IND as long) as byte
Getcrchi = 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, & had, & 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, & h2, & 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)
End Function
The above programs are successfully debugged under Win98 and VB6.
I did not test