8-6
Chapter 8. Modbus Communication Function
• Checksum (CRC)
This value is for checking whether something abnormal (e.g., electromagnetic
noise) has caused the message content to change during transmission. The check
sum is expressed in two bytes.
The procedure for calculating the checksum (CRC) is as follows.
/* CRC calculation */
/* Input unsigned char length: Number of bytes sent */
/* unsigned char * top: Sent data start pointer */
/* Output unsigned short CRC: CRC calculation result */
unsigned short crc16 (unsigned char length, unsigned char *top)
{
unsigned short CRC= 0xffff;
unsigned short next;
unsigned short carry;
unsigned short n;
unsigned char crcl;
while ( length-- ) {
next = (unsigned short)*top;
CRC ^= next;
for (n = 0; n < 8; n++) {
carry = CRC & 1;
CRC >>= 1;
if (carry) {
CRC ^= 0xA001;
}
}
top++;
}
crcl = (CRC & 0xff00)>>8;
CRC <<= 8;
CRC |= crcl;
return CRC;
}
• Determining the end of one frame
The message end (end of 1 frame) is determined when the period of time during
which no character is received exceeds the time specified for the transmission
speed. If the next character is not received by the timeout time shown below, the
frame is determined to have ended.
Note that the timeout time has a fluctuation of ±1 ms.
Set transmission speed
(bps)
Timeout time
4800 9 ms or more
9600 5 ms or more
19200 3 ms or more
38400 2 ms or more