YASKAWA G7 Modbus Communication Manual page 31

Hide thumbs Also See for G7:
Table of Contents

Advertisement

CRC-16 Calculation Example in C
void getMBCRC(char *, int, char *)
void getMBCRC(char *buf, int bufLen, char *crc) {
unsigned long crc_0 = 0xffff;
unsigned long crc_1 = 0x0000;
int i,j;
for (i=0; i<bufLen; i++) {
crc_0 ^= ((unsigned long)buf[i] & 0x00ff);
for (j=0;j<8;j++) {
crc_1 = (crc_0 >> 1) & 0x7fff;
if (crc_0 & 0x0001)
else
}
}
crc[0] = (unsigned char)((crc_0/256) & 0x00ff);
crc[1] = (unsigned char)(crc_0 & 0x00ff);
return;
}
crc_0 = (crc_1 ^ 0xa001);
crc_0 = crc_1;
// function prototype
// Function name and parameter list returning a void
// *buf
pointer to character array used to calculate CRC
// bufLen number of characters to calculate CRC for
// *crc
pointer to the array that contains the calculated CRC
// Declare and initialize variables
// Declare and initialize variables
// Declare and initialize variables
// Loop through characters of input array
// XOR current character with 0x00ff
// Loop through characters bits
// shift result right one place and store
// if pre-shifted value bit 0 is set
// XOR the shifted value with 0xa001
// if pre-shifted value bit 0 is not set
// set the pre-shifted value equal to the shifted value
// End for loop - Loop through characters bits
// End for loop - Loop through characters of input array
// Hi byte
// Lo byte
// Return to calling function
// End of CRC calculation function
31

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents