
5-3
Chapter 5. Modbus Communication (RTU Mode)
Check code
In RTU mode, CRC check codes are included in both command and response
messages.
z
Calculation of CRC check code
The part of the message from the device address to immediately before the check
code is used for the calculation. (The object of calculation is the 8-bit data for each
character. It does not include the start bit, stop bit, or parity bit.)
The binary data in the message is used as is in the calculation.
The 16-bit CRC check code can be calculated with the following C language
function get_crc16().
In the CRC in the message, the lower byte precedes the upper byte. This order is the
opposite of that in other 16-bit data.
/ ************************************************
*
Argument 1: Length of character string (number of bytes)
Argument 2: Pointer to beginning of character string
Function value: Calculation result
******************************************
*
/
unsigned short get_crc16(signed int len, unsigned char *p)
{
unsigned short crc16;
unsigned short next;
unsigned short carry;
signed int i;
crc16 = 0xffff;
while (len > 0) {
next = (unsigned short)*p;
crc16 ^= next;
for (i = 0; i < 8; i++) {
carry = (unsigned short) (crc16 & 0x0001);
crc16 >>= 1;
if (carry != 0) {
crc16 ^= 0xA001;
}
}
p++;
len--;
}
return crc16;
}
Содержание F4Q
Страница 10: ......
Страница 14: ......
Страница 46: ......
Страница 66: ...7 2 Chapter 7 Device Operation by Communication Commands...
Страница 68: ......
Страница 70: ......
Страница 71: ...Revision History CP SP 1458E Printed Edn New Page No Description June 2021 1...
Страница 72: ......
Страница 73: ......