CRC-16 (Cyclic Redundancy error check)
The CRC-16 error check program is as follows: Packet (only data bit, not the start bit, stop bit and optional parity bit) is considered as a continuous binary, its maximum valid bit (MSB) preferred sending. The packet is first multiplied by x 16 (16 digits left), and then divided into x 16 + x 15 + X 2 + 1, X percentile 16 + x percentile 15 + x percentile 2 + 1 can be expressed as 11000000000000101 binary numbers. Ignore and ignore integer business bits. The 16-bit remainder is added to the message (sent first by MSB), which becomes two CRC verification bytes. Initialize all the values of 1 in the remainder to prevent all zeros from being received. After the preceding processing, if there is no error in the CRC packet, the received device is then divided by the same polynomial (x 16 + x 15 + X 2 + 1, A zero remainder is obtained (the receiving device verifies this CRC byte and compares it with the transmitted CRC ). All operations take 2 as the modulo (without carry ).
Devices that are used to sending data in strings will first send the rightmost character (LSB-least valid bit ). In the case of CRC generation, the highest effective bit MSB of the dividend should be sent first. Because carry is not used in the operation, MSB is set to the rightmost bits for the convenience of the operation. The order of the generated polynomials must also be reversed to maintain consistency. The MSB of polynomial is omitted, because it only affects the business and does not affect the remainder.
The steps to generate a CRC-16 validation byte are as follows:
① If a 16-bit register is installed, all digits are 1.
② The high byte of the 16-bit register and the start 8-bit byte perform the "XOR" operation. Put the calculation result into this 16-bit register.
③ Shift the 16-bit register to the right.
④ If the number of digits removed from the right (flag bit) is 1, the polynomial 1010000000000001 is generated and the register is "exclusive or". If the number of digits removed from the right is 0, return ③.
⑤ Repeat ③ and ④ until 8 digits are removed.
⑥ The other 8 bits perform the "XOR" operation with the 16-bit register.
7. Repeat ③ ~ (6) until all the bytes in the message are "exclusive or" with the 16-bit register, and the value is shifted 8 times.
The contents of the ⑧ 16-bit register are two-byte CRC error checks, which are added to the maximum valid bit of the message.
In addition, crc16 is often used as the verification method in some non-Modbus communication protocols, and some crc16 variants are generated, they use the crc16 polynomial x percentile 16 + x percentile 15 + x percentile 2 + 1, and the first 16-bit register is 0000; use the reverse order of crc16 x 0000 + x 14 + X 1 + 1 and load the register value or ffffh for the first time.
A simple CRC function is as follows:
Unsigned short crc16 (puchmsg, usdatalen)
Unsigned char * puchmsg;/* message for CRC verification */
Unsigned short usdatalen;/* number of bytes in the message */
{
Unsigned char uchcrchi = 0xff;/* High CRC bytes initialization */
Unsigned char uchcrclo = 0xff;/* low CRC bytes initialization */
Unsigned uindex;/* index in CRC loop */
While (usdatalen --)/* Transmit message buffer */
{
Uindex = uchcrchi ^ * puchmsgg ++;/* calculate CRC */
Uchcrchi = uchcrclo ^ auchcrchi [uindex };
Uchcrclo = auchcrclo [uindex];
}
Return (uchcrchi <8 | uchcrclo );
}
LRC (vertical redundancy error check)
LRC error verification is used in ASCII mode. This error check is an 8-bit binary number, which can be transmitted as two ASCII hexadecimal bytes. Convert the hexadecimal character to binary, and add the binary character without cyclic carry and the binary complement result to generate LRC error verification (see the figure ). The LRC is verified on the receiving device and compared with the transmitted LRC, including the colon (:), carriage return (CR), and line feed (LF) and any other non-ASCII hexadecimal characters entered are not counted During computation. The LRC method accumulates 8-bit bytes in a message consecutively and discards carry.
The simple functions of LRC are as follows:
Static unsigned char LRC (auchmsg, usdatalen)
Unsigned char * auchmsg;/* message to be calculated */
Unsigned short usdatalen;/* number of bytes to be processed by LRC */
{Unsigned char uchlrc = 0;/* LRC byte initialization */
While (usdatalen --)/* Send message */
Uchlrc + = * auchmsg ++;/* accumulate */
Return (unsigned char) (-(char_uchlrc )));
}