need CRC16 for modbus

S

Thread Starter

Saeid Yousefpour

Hi,
I am doing some modbus application but I have problem with calculating CRC16 for my modbus commands. can any one help me how to calculate CRC16 or is any component or dll for crc16 calculation?

Thanks
Saeid Yousefpour

Saied_Yousefpour [at] Yahoo.Com
 
unsigned short crc16( unsigned char *data, short N)
{

unsigned short reg, bit, yy, flag;

yy = 0;
reg = 0xffff;

while( N-- > 0 )
{
reg = reg ^ data[yy++];

for ( bit = 0; bit <= 7; bit++ )
{
flag = reg & 0x0001;
reg = reg >> 1;
if ( flag == 1 )
reg = reg ^ 0xa001;
}
}

return ( reg );
}



Example of calling above crc16()
{

crc = crc16( polldata, bytes);
polldata[bytes] = crc & 0xff;
polldata[++bytes] = (crc >> 8) & 0xff;
txd( polldata, bytes);
}
 
Top