Calculating CRC for Modbus RTU
Hi, I'm working on a SCADA industrial control system that communicates via Modbus RTU. I'm using a 16F648A with a MAX232 serial driver. The hardware all works fine, but I can't figure out how to calculate the CRC. Apparently Modbus has a specific CRC format, and I found a routine in another forum that claimed to work in PBpro but it isn't.
Here's what I'm using:
CRC=$FFFF ' initialize value
For X = 0 to 11 ' number of bytes to process
CRC = CRC ^ dataCRC[X] ' read through data array
For Z = 1 to 8
IF CRC.Bit0 = 1 Then ' check bits
CRC = $A001 ^ (CRC >> 1)
Else
CRC = CRC >> 1
EndIF
Next Z
Next X
Return
The host is sending:
Tx:0F 03 00 00 00 04 45 27
and my code is returning:
Rx:0F 03 08 13 24 13 88 13 5B 00 01 38 6B
- the last 2 bytes should be the correct CRC but my host simulator is rejecting it.
FYI, the software Modbus host simulator I'm running is Modbus Poll from http://www.modbustools.com/
Nice tool!
Anyone been down this road before?
CRC checks out but is rejected
I changed the loop to 10 and got a CRC of 0F D2,
but the Modbus Poll simulator still shows a CRC error.
I am processing the bytes in the CRC calculation code in the same order that I am transmitting them - seem reasonable to me.