Calculating CRC for Modbus RTU


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81

    Default 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?

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    do you have the bytes in the correct order?

    if not, try changing it to: FOR X=11 TO 0 STEP -1
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    There are only 11 bytes in the packet.
    Change it to ...

    For X = 0 to 10

    Then you should get $0FD2 for the CRC.
    <br>
    DT

  4. #4
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default 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.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    According to this page ...
    http://www.lammertbies.nl/comm/info/...lculation.html

    0FD2 is the correct CRC for CRC-16 (Modbus) using [0F 03 08 13 24 13 88 13 5B 00 01].
    enterd as hex.

    Could the simulator be using a different CRC polynomial?
    <br>
    DT

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Also,

    If I enter [0F 03 00 00 00 04] as sent from the host above, it shows 0x2745 for the CRC, which is the same thing the host sent.

    So that has to be the right algorithm.
    <br>
    DT

  7. #7
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default My simulator setup

    FYI - here's a screen shot of my Modbus Poll simulator screen and setup:



    Hope the image shows OK
    Attached Images Attached Images  

  8. #8
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Data format?

    Quote Originally Posted by Darrel Taylor View Post
    Also,

    If I enter [0F 03 00 00 00 04] as sent from the host above, it shows 0x2745 for the CRC, which is the same thing the host sent.

    So that has to be the right algorithm.
    <br>

    So maybe it's my data formatting - or something???

  9. #9
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Success!!!! :)

    Quote Originally Posted by Darrel Taylor View Post
    Also,

    If I enter [0F 03 00 00 00 04] as sent from the host above, it shows 0x2745 for the CRC, which is the same thing the host sent.

    So that has to be the right algorithm.
    <br>
    Hey Darrel,
    You gave me the final clue - it was a byte order issue I needed to reply with the bytes reversed: D2 0F. Your result above clued me in!

    Thanks much - see screen shot showing no errors.

    Guy
    Attached Images Attached Images  

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    He He,

    You gotta watch out for those "Little Endians" ...
    They'll shoot you in the butt with an arrow if your not looking.

    Glad I could help,
    DT

Similar Threads

  1. Dallas CRC8 Routines
    By Tom Estes in forum Code Examples
    Replies: 23
    Last Post: - 8th May 2018, 18:07
  2. CRC Calculations
    By timmers in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2009, 17:10
  3. Sensirion CRC
    By Tom Estes in forum Code Examples
    Replies: 3
    Last Post: - 9th November 2007, 15:09
  4. Problems with CRC8 Calc in 1Wire
    By JohnB in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 16th March 2007, 22:01
  5. crc16 help
    By beto in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 4th January 2006, 21:21

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts