Modbus communication based on serial links requires error verification, and different verification methods are used for the error verification field depending on the transmission mode (ASCII or RTU).
- ASCII pattern —— The verification field consists of two characters, Its value is based on the execution of all message contents LRC Verification (Longitudinal Redundancy Check, Vertical redundancy check) The calculation results. The calculation object does not include the starting colon (
:) Enter and line break symbols (CRLF) . - RTU pattern —— The verification field is composed of 16 One bit (Two bytes) compose, Its value is based on the execution of all message contents CRC Verification (Cyclical Redundancy Check, Cyclic redundancy check) The calculation results. The calculation object includes all bytes before the checksum field.
LRC Verification
LRC Verification is relatively simple, It is ASCII Used in the agreement, Detected the content in the message field except for the colon at the beginning and the carriage return line number at the end. It simply stacks each data that needs to be transmitted in bytes (Discard all carry values) , Then perform binary complement operation on the result.
Below is LRC Specific code for verification:
unsigned char LRC(unsigned char *auchMsg, unsigned short usDataLen)
{
unsigned char uchLRC = 0;
while (usDataLen--)
uchLRC += *auchMsg++;
return ((unsigned char)(-((char)uchLRC)));
}From the essence of algorithms, LRC The domain itself only occupies 1 bytes, but in ASCII When transmitting message frames in a pattern, LRC The value is encoded as 2 Byte by byte ASCII character. for example, Calculated LRC value 0xF3, So in ASCII In the message frame, it is represented as 'F' and '3' Two characters.
CRC Verification
CRC The domain is two bytes, Containing one 16 Binary value of bits. It is calculated by the transmission device and added to the message. The receiving device recalculates the received message CRC, And with the received CRC Comparison of values in the domain, If the two values are different, There is an error.
CRC Is it to first input one value? It is full“1”of 16 Bit register, Then call a procedure to 8 Process the values in each current register of the byte. Only within each character 8 Bit data pairs CRC effective, The start bit, stop bit, and parity bit are all invalid. CRC During the production process, each 8 Each bit character is individually associated with the contents of the register (OR) , Result moves towards the least significant bit direction, The most significant position is 0 fill. LSB Extracted for detection, If LSB for 1, Register individual and preset values or below, If LSB for 0, Then do not proceed. The whole process needs to be repeated 8 Next time.At the last one(The) 8 Position)After completion, next 8 Bit bytes are separately correlated with the current value of the register. The value in the final register, After all the bytes in the message have been executed CRC Value.
Below is CRC Specific code for verification:
unsigned short CRC16(unsigned char *puchMsg, unsigned short usDataLen)
{
int i, j;
unsigned short usRegCRC = 0xFFFF;
for (i=0; i<usDataLen; i++)
{
usRegCRC ^= *puchMsg++;
for (j=0; j<8; j++)
{
if (usRegCRC & 0x0001)
usRegCRC = usRegCRC >> 1 ^ 0xA001;
else
usRegCRC >>= 1;
}
}
return usRegCRC;
}in Modbus RTU In the mode, It is stipulated that during message frame transmission CRC The verification value must be placed first by the lower byte, The order of high bytes after. for example, Calculated CRC value 0xCA31, So the sending order is 0x31, 0xCA.
Above CRC16 The calculation method is not optimal, Because it requires a lot of computation, Only applicable to situations where speed is not sensitive. Those who have requirements for computing speed, The verification value can be obtained by using the lookup table method, It is a strategy of exchanging space for time.
Leave a Reply