31
CRC-16 Calculation Example in C
void
getMBCRC(char
*,
int,
char
*)
//
function
prototype
void getMBCRC(char *buf, int bufLen, char *crc) {
// 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
unsigned long crc_0 = 0xffff;
//
Declare
and
initialize
variables
unsigned long crc_1 = 0x0000;
// Declare and initialize variables
int
i,j;
//
Declare
and
initialize
variables
for (i=0; i<bufLen; i++) {
// Loop through characters of input array
crc_0 ^= ((unsigned long)buf[i] & 0x00ff);
// XOR current character with 0x00ff
for
(j=0;j<8;j++)
{
//
Loop
through
characters
bits
crc_1 = (crc_0 >> 1) & 0x7fff;
// shift result right one place and store
if (crc_0 & 0x0001)
// if pre-shifted value bit 0 is set
crc_0 = (crc_1 ^ 0xa001);
// XOR the shifted value with 0xa001
else
// if pre-shifted value bit 0 is not set
crc_0 = crc_1;
// 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
crc[0] = (unsigned char)((crc_0/256) & 0x00ff);
// Hi byte
crc[1] = (unsigned char)(crc_0 & 0x00ff);
// Lo byte
return;
//
Return
to
calling
function
}
//
End
of
CRC
calculation
function
Summary of Contents for Varispeed G7
Page 1: ...Modbus Communication Manual G7 Drive Drive Model CIMR G7U Document Number TM G7 11...
Page 2: ...2 Page intentionally blank...
Page 6: ...6 Notes...
Page 10: ...10 G7 Drive Connections Figure 1 2 G7 Drive Connections...
Page 18: ...18 Notes...
Page 32: ...32 Notes...
Page 80: ...80 Notes...
Page 82: ...82 Notes drive drive Name Date...