Modbus RS485 mode CRC

A forums to allow bugs and problems with Flowcode v7 to be reported and resolved.

Moderator: Benj

Post Reply
EtsDriver
Valued Contributor
Valued Contributor
Posts: 438
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Contact:

Modbus RS485 mode CRC

Post by EtsDriver »

Using 7.1.1:
http://www.matrixtsl.com/mmforums/viewt ... 64&t=18187, as ghost is not working exactly right ive been playing around now scoping as the data old school way, and i see it is not working correctly:


The blue line is sent from other microcontroller to RS485 pin, and the other green one is the receiving end data, and by looking at the decoding, you see that the upper line (transmitting one) ends with hex 39, and the lower one (receiving one) doesnt get it:
SDS00004.png
(19.44 KiB) Downloaded 5967 times
I tried at first without EB062 connecting directly to each other, (Cross over "RS232"), communications work ok!... But then i tried using RS485 eblock, and seems that atleast master is not allowing data direction pin (green) to be high enough for CRC to go through:
SDS00005.png
(17.53 KiB) Downloaded 5967 times
********
Tested this too from slave side ( Sending data direcly to rxtx and looking just at the data dir pins), and its timing on the data direction pin seems ok, only master is the problem:
SDS00006.png
(17.32 KiB) Downloaded 5967 times
Attachments
MB_Slave1.fcfx
(8.23 KiB) Downloaded 149 times
MASTER.fcfx
(10.64 KiB) Downloaded 153 times
Ill just keep the good work up!

User avatar
LeighM
Matrix Staff
Posts: 2043
Joined: Tue Jan 17, 2012 10:07 am
Contact:

Re: Modbus RS485 mode CRC

Post by LeighM »

Thanks for the comprehensive investigation. I think Ben has been looking at similar recently, so will no doubt post update soon.

User avatar
Benj
Matrix Staff
Posts: 14854
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Modbus RS485 mode CRC

Post by Benj »

Hello,

Please can you try this updated component and let me know how you get on. It should wait a bit longer for the bytes to send before switching the I/O state.

To add the file to your installation simply copy the file to the "Flowcode 7/components" folder and restart Flowcode.
Modbus.fcpx
(18.36 KiB) Downloaded 150 times
Let us know how you get on.

EtsDriver
Valued Contributor
Valued Contributor
Posts: 438
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Contact:

Re: Modbus RS485 mode CRC

Post by EtsDriver »

Hello, and thank you!
SDS00010.png
(18.71 KiB) Downloaded 5952 times
This seems to do the trick!

No problems anymore! This is great, now it works! :)
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 14854
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Modbus RS485 mode CRC

Post by Benj »

Great thanks for letting me know.

I fixed this same issue a while back for the dsPIC as it's possible to store a few values in the outgoing buffer and so I now specifically check to see when the buffers are empty before I toggle the output state.

I don't think this is possible on other micros so we have to rely on delays, anyway hopefully the latest component will fix all the other cores too.

EtsDriver
Valued Contributor
Valued Contributor
Posts: 438
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Contact:

Re: Modbus RS485 mode CRC

Post by EtsDriver »

Hi Benj!

I noticed that there is another bug inside the component, or this seems to be not implemented for some reason-->
SDS00030.png
(14.4 KiB) Downloaded 5933 times
When master tries to read slave's non-exists input register instead of holding registers (slave should answer illegal data-address exception 0x02), for some reason slave answers with holding register content or some nonexists data..
SDS00029.png
(15.1 KiB) Downloaded 5933 times
The returning function code should come back with its highest bit set (exception):
For illegal function the 0x04 shoud return to master as 0x84, and followed by exception code 0x02 and crc checksum...

Ive attached below the programs for you to try it out, should get exception code to all other except the holding register reading. Slave loads the holding registers with data, but no input registers are defined.

EDIT: changed fcfx files to better ones to demonstrate.
Attachments
ExceptionThrowingMASTER.fcfx
(33.45 KiB) Downloaded 156 times
ExceptionSlave.fcfx
(9.07 KiB) Downloaded 139 times
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 14854
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Modbus RS485 mode CRC

Post by Benj »

Hello,

Many thanks for letting us know about the problem. I've been following Modbus tutorials but they rarely tell you the whole story.

I have now implemented the error code for command 0x04 read analogue registers.

If the requested number of registers is larger than the actual number of registers then I change the CommandID and set the most significant bit and then return a single byte value 0x02. Is this right or should I first return any valid register addresses?

For example the Master Requests the values of analogue registers 0 and 1. Register 0 does exist but register 1 does not. Currently I only return the Error Command ID and the Error code but would it be better to instead return register 0 and then have the error code 0x02?

If this is working as expected then let me know and I will roll out the change to the other Modbus Slave functions.
ModbusSlave.fcpx
(89 KiB) Downloaded 152 times

EtsDriver
Valued Contributor
Valued Contributor
Posts: 438
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Contact:

Re: Modbus RS485 mode CRC

Post by EtsDriver »

Hi!

I think by reading these and looking some MB485 Slave's should actually give exception 0x01, not implemented (the amount of registers is zero) if there are really no registers for function code available, and 02 if user tries to read registers/coils/inputs that are not available.

But the exception for input registers is now working pretty nice, all other's unimplemented still return things to master! :)

If user tries to access memory location that is not available (like read 6 registers, even tough slave has only 2, or read register 10, even tough slave has 0-2) illegal data address should be returned. If or atleast i think thats the way to do it... :D

In the modbus.org diagrams the exception codes are very well explained, and when the exceptions should be returned... Still its not written on stone, like the amount of maximum registers can vary from device to device. Ive seen the "busy flag" been implemented to signal master that user is editing on the slave's screen the settings, and the settings might change soon, and after timeout or user going out from the settings menu, only certain values are returned (like rpm, temp, pressure), but settings registers are kept "busy".

So the slave should have some kind of "throw exception" option maybe for future... Just ideas coming to mind :lol:
Ill just keep the good work up!

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1145
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

Re: Modbus RS485 mode CRC

Post by Steve001 »

HI Guys

Maybe this will help some stuff on page 12 and 82 about errors and a part from a modbus section on a protection relay

I know that from various devices that i have sent an incorrect address that i get a message "function 2 error" returned

Steve
Attachments
error codes.pdf
(39.45 KiB) Downloaded 155 times
Modbus Protocol.pdf
(168.19 KiB) Downloaded 151 times
Success always occurs in private and failure in full view.

User avatar
Benj
Matrix Staff
Posts: 14854
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Modbus RS485 mode CRC

Post by Benj »

Hi Guys,

Thanks for the help getting this correct. In the last component I only checked to see if the number of addresses was <= the actual number of available addresses. Now I also do this and also check the address is actually in range. This should hopefully now work for all the Modbus commands.

the value returned should now be 0x01 if the command is not supported at all (0 allocated addresses) or 0x02 if the command addresses are out of range.
ModbusSlave.fcpx
(89.6 KiB) Downloaded 116 times
I'll look at adding other temporary user based exceptions if this would also be handy to have. If you have a play then let me know how you get on.

EtsDriver
Valued Contributor
Valued Contributor
Posts: 438
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Contact:

Re: Modbus RS485 mode CRC

Post by EtsDriver »

Hi!

I noticed there was error still on the exceptions, because modpoll kept saying CRC error when communicating to slave over RS485 in RTU mode, i have not tested out the ASCII mode as the ASCII slave and master could not communicate even on the EBlocks...
mbpol.PNG
(22.52 KiB) Downloaded 5835 times
The answer is too long to be detected to be modbus exception code:

02 82 01 01 61 E4
slaveID, function code with its highest byte set, exception code,? ,CRC

Should be without the last 01...
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 14854
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Modbus RS485 mode CRC

Post by Benj »

Aha. that extra 1 was the response byte count. I've now got rid of it if the MSB of the command ID has been set.
ModbusSlave.fcpx
(89.6 KiB) Downloaded 147 times

radist73
Posts: 36
Joined: Mon Dec 22, 2014 10:04 pm
Location: TALLINN
Contact:

Re: Modbus RS485 mode CRC

Post by radist73 »

Many thanks!
I has the same problem with Modbus (232 works fine but 485 not) and now it is fixed!

Post Reply