Re: CRC checking error in MODBUS protocol

E

Thread Starter

Eugene Zharkov

<p>Marcelo,

<p>Here is a routine that I use.
<pre>
static unsigned short int vmbcrc16 (unsigned char *bytp, int count) {

#define VMBPOLY 0xA001

unsigned short int crc = 0xffff;
unsigned short int temp;
int i;
int j;

/*
* Checksum the buffer
*/
for (i = 0; i < count; i++) {
crc ^= bytp;
/*
* Use all 8 bits in the byte
*/
for (j = 0; j < 8; j++) {
unsigned short int one = crc &1;

/*
* Shift right one bit
*/
crc >>= 1;
/*
* If the previous LSB was a one XOR with the polynomial
*/
if (one)
crc ^= VMBPOLY;
}
}
temp = (crc >>8) &0xFF;
crc = ((crc &0xff) <<8) | temp;
return(crc);
}

--
</pre>
<p>Eugene Zharkov<br>
Vista Control Systems, Inc.<br>
http://www.vista-control.com
 
Top