Theory of the CRC look up table ?

  • Thread starter Jean - Pierre Van - Roy
  • Start date
J

Thread Starter

Jean - Pierre Van - Roy

Hello everbody,


I search the means for obtain theoretically the two look up tables mentioned in the document
"MODBUS over Serial Line Specification & Implementation guide V1.0"?
To my knowledge, the values are obtained by carrying out an offset of the poly and a XOR for all possible values from 0 to 255.
Unfortunately, the values which I obtain are always false compared to those of the standards.
Being a little maniac, I wish to understand the base of this implementation.

Thank You for any response,

Jean - Pierre
 
L

Lynn at Alist

<p>Just search for XMODEM documents - many of the "Serial Communications with C" type books will include discussions of XMODEM transfer. That is where I got my CRC16 routines from. I had a fat one from Que Tech books that included a theory discussion on the CRC table & why it worked, but left that book in Singapore so don't have the details of it anymore. XMODEM uses the same CRC16 method as Modbus/RTU.

<p>The routine below will create the table for you. I needed it for a small embedded system that didn't allow static init of RAM. You can also put this routine into a simple main() to spit out the table to stdout, and so capture it for your code instead of manually typing all those numbers by hand. This is a single table of 16-bits instead of the 2 x 8-bit tables Modbus mentions.
<pre>
ushort crc16_Table[256]; // table for partial CRC seeds

/* crc16_Init() - Initialize the CRC-16 table (crc16_Table[])
*/
void crc16_Init( void)
{
ushort crc_data; // holder of byte during calc
ushort crc_crc; // holder of CRC
int crc_i; // loop counter
char j; // loop counters

for( crc_i = 0; crc_i < 256; crc_i++) {
crc_data = (crc_i << 1);
crc_crc = 0;
for( j = 8; j > 0; j--) {
crc_data >>= 1;
if( (crc_data ^ crc_crc) & 0x0001)
crc_crc = (crc_crc >> 1) ^ 0xA001;
else
crc_crc >>= 1;
} // endfor j
crc16_Table[crc_i] = crc_crc;
// printf( " %d:%x", crc_i, crc16_Table[crc_i]);
} // endfor crc_i

return;
}
</pre>
 
E

Eugene Zharkov

I use just one 'unsigned short' table instead of two 'unsigned char' tables. On the net there are many examples showing how such tables are generated. See, for example, Appendix B.2 of the following document:

http://www.faqs.org/rfcs/rfc1171.html

Note that the modbus polynomial is 0xA001, rather than 0x8408 used in that RFC.

--

Eugene Zharkov
Vista Control Systems, Inc.
 
Top