User Guide
User Guide
©2008-2020 Seeed Technology Co., Ltd. All rights reserved. solution.seeedstudio.com
14 / 21
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
0x41, 0x81, 0x80, 0x40
};
unsigned short usCRC16( unsigned char * pucFrame, unsigned short usLen )
{
unsigned char ucCRCHi = 0xFF;
unsigned char ucCRCLo = 0xFF;
int iIndex;
while( usLen-- )
{
iIndex = ucCRCLo ^ *( pucFrame++ );
ucCRCLo = ( UCHAR )( ucCRCHi ^ aucCRCHi[iIndex] );
ucCRCHi = aucCRCLo[iIndex];
}
return ( unsigned short )( ucCRCHi << 8 | ucCRCLo );
}
The CRC generated by this function has exchanged high and low bytes and can be sent directly into a
message.
For example: The CRC16 of a certain frame is calculated by this function to be equal to 0x4112, then the
message is placed as follows:
AA
03
RRRR
NNNN
CC
CC
1 Byte
1 Byte
2 Bytes
2 Bytes
1 Byte
1 Byte
Slave address,
range 0-247
Function Code is
3
Start register
address, high
byte first
The number of
registers to read,
high byte first
CRC check, low
byte 0x41
CRC check,
high byte
0x12