8-5
Chapter 8.  Modbus COMMUNICATION FUNCTION
Modbus RTU
All messages are written in binary data. 
A Modbus RTU message consists of (1) to (3) below. 
The part of (2) stores commands, which are transmission contents from the master 
station and responses, which are transmission contents from the slave station. 
All messages use binary data. (Each slot below corresponds to one character.)
(2)(1) (3)
1 frame
(1)  Station address (1 byte)
(2)  Send message, response message
(3)  Checksum (2 bytes)
•  Station address
Of the messages sent by the master station, the device creates response messages 
only when station addresses are the same.  Station addresses in the messages are 
expressed in one byte. The station address is set up by the station address setup 
(setup setting C65).  However, when the station address is set to 0, the device 
creates no response even if station addresses match.  The device returns the same 
station address as that of the received message.
•  Checksum (CRC)
This value is for checking whether or not some abnormality (e.g. noise) causes 
the message content to change during communications.  The checksum is 
expressed as 2 bytes.
The checksum (CRC) creation method is shown below.
/* CRC calculation */ 
/* Input  unsigned char  length : Number of transmission bytes   */ 
/*    unsigned char  *top  : Transmission 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; 
}