With the development of computer technology, the industrial control of data communication using microcomputer has been applied more and more widely in modern industry. The communication data between the computer and the controlled device often occurs unpredictable errors due to the transmission distance, the field condition and many other possible factors. In order to prevent the impact of errors, generally in the communication with the method of data validation, and cyclic redundancy code check is the most common method of calibration.
The principle of cyclic redundancy code checking
Cyclic redundancy Code Verification English name is cyclical redundancy Check, referred to CRC. It uses the principle of division and remainder to make error detection (Error detecting). In practical application, the sending device calculates the CRC value and sends it to the receiving device with the data, the receiving device recalculates the CRC to the received data and compares with the received CRC, if the two CRC values are different, the data communication error is indicated.
According to the application environment and habits of different, CRC can be divided into the following several criteria:
①CRC-12 Code;
②crc-16 Code;
③crc-ccitt Code;
④CRC-32 code.
CRC-12 code is commonly used to transmit 6-bit strings. CRC-16 and Crc-ccitt codes are used to transmit 8-bit characters, of which CRC-16 are used by the United States, while Crc-ccitt is used by European countries. Most of the CRC-32 codes are used in a synchronous transmission called point-to-point.
The following is an example of the most commonly used CRC-16 to illustrate its build process.
The CRC-16 code is composed of two bytes, at the beginning, each of the CRC registers is preset to 1, then the CRC registers are 8-bit with the data, then the CRC registers are shifted from high to low, at the top (MSB) position to fill 0, and the lowest bit (LSB, After the shift has been moved out of the CRC register if 1, then the register with the predefined polynomial code, or if the LSB is zero, you do not need to be different or. Repeat the above shift from high to low 8 times, the first 8-bit data processing, with the CRC register with the value of the next 8-bit data or and carry out as the previous data like 8 times shift. The value in the CRC register is the final CRC value after all character processing is completed.
The following is the calculation procedure for CRC:
1. Set the CRC register and assign it a value of FFFF (hex).
2. The first 8-bit character of the data is different from the low 8 bits of the 16-bit CRC register, and the result is stored in the CRC register.
3. The CRC registers move one bit to the right, MSB 0, remove and check the LSB.
4. If the LSB is 0, repeat the third step, if the LSB is a 1,CRC register that is different from the polynomial code.
5. Repeat steps 3rd and 4th until all 8 shifts are completed. At this point a 8-bit data processing is completed.
6. Repeat steps 2nd through 5th until all data is processed.
7. The content of the final CRC register is the CRC value.
Second, the compilation of cyclic redundancy code check Program
Understand the CRC check code generation process, the program is very easy to write. Because of the widespread popularity of Visual Basic and its important position in the data communication, the following VB language to write the CRC generation program, the other language only need to do a little modification.
There are two ways to write a CRC calibration program: one for the calculation and one for the look-up table method. The following two methods are discussed separately.
1. Calculation method
The calculation method is to design the program according to the principle of CRC check code generation. The advantage is that the module code is less, the modification is flexible, the portability is good. The disadvantage is that the computational capacity is large. For the sake of understanding, three-bit data is assumed here, and the polynomial code is A001 (hex).
Place a command button Command1 on the form and add the following code:
Private Sub Command1_Click ()
Dim CRC () as Byte
Dim d () as Byte to transfer data
ReDim D (2) as Byte
D (0) = 123
D (1) = 112
D (2) = 135
CRC = CRC16 (d) ' Calls CRC16 calculation function
' CRC (0) is high
' CRC (1) is low
End Sub
Note: The CRC may be low on the data transfer, and the post is high.
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 one is different from the CRC register or
For Flag = 0 to 7
Savehi = Crc16hi
Savelo = Crc16lo
Crc16hi = crc16hi \ 2 ' High position to move right one
Crc16lo = crc16lo \ 2 ' Low right shift one
if ((Savehi and &h1) = &h1) Then ' if the last digit of the high-order byte is 1
Crc16lo = Crc16lo Or &h80 ' The lower byte moves to the right and the front is 1
End If ' Otherwise it will automatically fill 0
if (Savelo and &h1) = &h1) Then ' If the LSB is 1, the polynomial code is different 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. Tabular method
The advantages and disadvantages of the tabular method are the exact opposite of the computational method. For ease of comparison, all of the assumptions here are identical to the calculation, and a Command1 button is placed on the form, and the Code section is exactly the same as the above. The following only describes the CRC function to write the source code.
Private Function CRC16 (data () as Byte) as String
The above program is debugged and passed under WIN98,VB6.
Author blog:http://blog.csdn.net/huitiansou/
Related article string converted to hexadecimal
How to write CRC check program under VB
The realization of MD5 encryption algorithm in VB
MD5 Algorithm Implementation
Research on MD5 algorithm
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.