CRC calculation in Modbus

R

Thread Starter

Ralf Obertacke

I am a Modbus beginner and I need to calculate the CRC for a modbus command to a slave. The CRC examples I found are not very clear to me and they seem to conflict with each other.

1. Can you give a hint where to find a good and valid example how to calculate CRC and how to queue the whole frame, preferably in Ansi C?

With kind regards
Ralf Obertacke
 
B
Yes, I realized two years ago a Modbus communication including the CRC-16 Checksum calculation on a Honeywell IPC620 PLC.

The source for calculation I've found somewhere as C Code. I don´t remember from what website, but I have it still printed on paper. If You send me a fax number I can send You a copy.

With kind regards

Bernd Bauer

[email protected]
 
<p>This seems pretty straightforward. I typed it all in from memory, so if there are bugs, let me know.

<p>Max<br>
[email protected]
<pre>
-------------------------------------------------
unsigned int CRC16;
#define SEED 0xFFFF //initialization for CRC16
#define GP 0xA001 //generating polynomial

//for standard CRC16
//(remainder of division)
//to start a new CRC, set CRC16 = SEED
//then for each byte call Calc_CRC(byte, &CRC16);
//CRC16 will contain the result
//(if you calculate all of the incoming data
//INCLUDING the CRC, the result should be 0x0000
//and if you are sending the CRC be sure to
//send the bytes in the correct order)

void Calc_CRC(unsigned byte b, unsigned int* CRC)
{
BOOL carry;
int i;

CRC[0] ^= b & 0xFF;
for (i=0; i<8; i++)
{
carry = CRC[0] & 0x0001;
CRC[0]>>=1;
if (carry) CRC[0] ^= GP;
}
}
-------------------------------------------------
</pre>
 
I am also new to MODBUS... i just need to use a limited set of commands.

i want to know if theres a crc calculator available to calculate the same? i don't need the code. a windows application. i tried to use the standard crc calculators available (Hex Workshop) but the generated checksum is different.
 
Top