Modbus CRC generation

akreel

Member
Join Date
Apr 2002
Location
Milwaukee, WI
Posts
1,172
Here's a question to keep our resident experts entertained.

I've been trying to write a little application that speaks Modbus. I'm starting from scratch, writing code to read/write the message string and calculate the CRC value. To test my program I've been inserting a known message string into it and comparing the CRC generated to the correct value. Guess what... They don't match, and neither to my hand calculations. So, I know I'm not reading the manual correctly.

Those of you who've dealt with the Modbus protocol on this level know that there are a series of binary shifts and XOR statements executed to determine the CRC. Somewhere in there, I must be doing a shift when I should be doing an XOR... What I'd REALLY like to see is a table that demostrates when to shift and when to XOR. Say for example; I want to transmit the hex number 0C (no commands, or anything just ONE byte). The CRC is preset to FFFF and I begin the shifting/XOR business (there should be 8 shifts, right?). What is my CRC going to look like at each step?

I decided to post this question because I thought you guys might enjoy showing off a little. Go nuts!

AK
 
I've done this (about four years ago) with a Mitsubishi FX2N PLC and an FX2N-485-BD board (cheap add-on at about $60). It was tedious and confusing to write, as well as difficult to validate, but it does work and have had one of the Mitsubishi distributor's applications engineers use it on a project of his.

One caveat is that the FX instruction set doesn't have a bit shift instruction, but does have a rotate instruction that can be used as a bit shift by clearing the holding register.

If you have access to Mitsubishi software (originally written in MEDOC but available in GX Developer), I could e-mail the code to you.

If anyone is interested in having me try and post this code, I could give it a shot.
 
Steve Bailey said:
There is a good step-by-step example in the GE Serial communications manual GFK-0582, chapter 7.

http://www.gefanuc.com/support/plc/m-s9030.htm

Remember that you don't XOR if the bit shifted out is a 0.

That looks like exactly the type of example I was looking for! I had a routine for performing all sorts of shifts and XORs, but with out this guide there really wasn't any way to tell where I might be making a mistake. I didn't see anything so detailed on the Modbus website.

The pseudo code is an especially nice touch. I shouldn't have any more trouble when I finally get around to tinkering with this thing again. Thanks.

AK
 
Another unique example. Not many say much or use GE Fanuc (except for Steve Bailey) BUT they have an awesome support site in a way. They show examples of 1 PB to turn off/on an output, drum sequence, indirect addressing, indexing, etc etc etc. When it comes to a plc manufacturer actually providing code (thats easy to find) for doing things GE Fanuc cant be beat.
 
need help with fx2n protocol

hello, I saw you worked with fx2n protocol, could you please provide me with this protocol, or a link..? I need it really bad.. thanks very much.
[email protected]






RBergmann said:
I've done this (about four years ago) with a Mitsubishi FX2N PLC and an FX2N-485-BD board (cheap add-on at about $60). It was tedious and confusing to write, as well as difficult to validate, but it does work and have had one of the Mitsubishi distributor's applications engineers use it on a project of his.

One caveat is that the FX instruction set doesn't have a bit shift instruction, but does have a rotate instruction that can be used as a bit shift by clearing the holding register.

If you have access to Mitsubishi software (originally written in MEDOC but available in GX Developer), I could e-mail the code to you.

If anyone is interested in having me try and post this code, I could give it a shot.
 

Similar Topics

Hello, i am connected l&t energy meter to PLC through RS485 in tia portal v15. i was reading values in plc. my problem is how to check crc 16 in...
Replies
2
Views
1,686
Hi All, I'm currently working on a project with some fuji drives (frenic mini) and a micro850. Communication is through Modbus RTU. I don't...
Replies
2
Views
1,754
This is my first post to the group. It's also the first day of trying to integrate my PLC (A DirectLogic 06) with a PT100 RTU heat sensor monitor...
Replies
4
Views
1,780
Has anyone ever run into an instance where the CRC word needed to be byte swapped? I know there has been confusion in the past because of some...
Replies
2
Views
2,978
Good day guys. I have a third party device with its Modbus registers which I want to copy/set up on a plc because I need to convert my Modbus tcp...
Replies
3
Views
112
Back
Top Bottom