(introduce
These days, a brother is debugging an industrial control project at a site, because the temporary replacement of the equipment makes the low-level communication program that has been written already had to be rewritten, and the communication protocol has also been greatly changed. The new device uses the standard Modubs-RTU protocol for data communication. Modubs protocol is a widely used protocol, which uses 16-bit CRC checksum. Brother, there is a problem with the verification, because it is inconvenient to search for information on site and entrust me to verify a series of byte data (send command messages) and reply him with the verification results. For the sake of convenience, I wrote a 16-bit CRC verification program specially used for Modubs protocol communication verification for him. Although the program is relatively simple, the intermediate data type conversion, hexadecimal conversion and calculation verification may be helpful to those who are looking for this information, so I put it up for us to study together! ^_^
text
CRC principle and termination:
With the continuous development of computer technology, in modern industry, industrial control using microcomputer for data communication is also used more and more widely. Due to the influence of many possible factors such as transmission interval and field conditions, the communication data between the computer and the controlled device often has unpredictable errors. In order to avoid the influence of errors, the method of data verification is generally adopted in communication, and the cyclic redundancy check is one of the most commonly used verification methods. Cyclic Redundancy Check Principle The English name of Cyclic Redundancy Check is Cyclical Redundancy Check, or CRC for short. It uses the principle of division and remainder for error detection (ErrorDetecting). In practice, the sending device calculates the CRC value and sends it to the receiving device together with the data. The receiving device re-calculates the CRC for the received data and compares it with the received CRC. If the two CRC values ??are different, it means that the data is communicating. Rendering error. According to the different usage environment and habits, CRC can be divided into the following specifications: ①CRC-12 code; ②CRC-16 code; ③CRC-CCITT code; ④CRC-32 code. The CRC-12 code is generally used to transmit 6-bit strings. CRC-16 and CRC-CCITT codes are used to transmit 8-bit characters, among which CRC-16 is selected for the United States, while CRC-CCITT is selected for European countries. CRC-32 codes are mostly used in a synchronous transmission called Point-to-Point. The following takes the most commonly used CRC-16 as an example to illustrate 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 is XORed with the 8-bit data, and then the CRC register is shifted from high to low. If it is 1, the register is XORed with the predefined polynomial code, otherwise if the LSB is zero, no XOR is required. Repeat the above-mentioned shift from high to low 8 times, the first 8-bit data management ends, use the value of the CRC register at this moment to XOR the next 8-bit data and perform 8 shifts like the previous data. . After all character management is completed, the value in the CRC register is the final CRC value. The following is the calculation process of CRC: 1. Set the CRC register and assign it a value of FFFF (hex). 2. XOR the first 8-bit character of the data with the lower 8 bits of the 16-bit CRC register, and store the result in the CRC register. 3. The CRC register is shifted one bit to the right, the MSB is filled with zeros, and the LSB is shifted out and detected. 4. If the LSB is 0, repeat the third step; if the LSB is 1, the CRC register is XORed with the polynomial code. 5. Repeat steps 3 and 4 until all 8 shifts are complete. At this point an 8-bit data management ends. 6. Repeat steps 2 to 5 until all data is fully managed. 7. After all, the content of the CRC register is the CRC value. In this way, we can obtain the function encapsulation for calculating the checksum.
WORDCCalCrcDlg::Calculation_CRC16(BYTEBuff,intnSize)
{
WORDm_Crc;
WORDm_InitCrc=0xffff;
unsignedshorti,j;
for(i=0;igt;=1;
if(m_Crc0x0001)
m_InitCrc^=0xa001;
}
}
returnm_InitCrc;
}
In this way, we can customize an interface for calculating the CRC checksum. Because it is used for a Demo of Modubs, Modubs sends the command format as: address, function code, read register high address, read register low address, read byte count high address, read byte count low address, check low , check high. So our custom interface runs as follows:
55980
Let's write a function wrapper for converting hexadecimal data of CString type from the interface to int type and a direction function wrapper.
DWORDCCalCrcDlg::H2D(CStringB)
{
DWORDD;
DWORDtmpD;
B.Remove('');
CStringtmpBit;
intBitLen=B.GetLength();
for(inti=0;i calculation button trigger calculation management can be:
voidCCal)