SECTION 3
CIM PROTOCOL
PM055 Rev 2 00/08/31
Thomson Technology
74
19. CRC
Calculation
The Cyclical Redundancy Check (CRC) calculation used by Modbus is commonly called
the CRC-16 algorithm. Refer to
A Painless Guide To CRC Error Detection Algorithms
by
Ross N. Williams for more detailed information; using the terminology of that paper, the
CRC algorithm used by the CIM is:
Name : "CRC-16"
Width : 16
Poly : 8005
Init : FFFF
RefIn : True
RefOut : True
XorOut : 0000
Check : BB3D
The following is a C code implementation of the CRC-16 method:
unsigned short CalculateCrc(unsigned char *pAddress, int nByteCount)
{
unsigned short wCrcValue = 0xffff;
while (nByteCount--)
{
wCrcValue ^= *pAddress;
for (int i = 0; i < 8; i++)
{
if (wCrcValue & 1)
{
wCrcValue >>= 1;
wCrcValue ^= 0xa001;
}
else
{
wCrcValue >>= 1;
}
}
p+;
}
return wCrcValue;
}