Modbus RS485 mode CRC

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

Moderator: Benj

EtsDriver
Valued Contributor
Valued Contributor
Posts: 437
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 348 times
Been thanked: 228 times
Contact:

Modbus RS485 mode CRC

Postby EtsDriver » Tue Oct 11, 2016 9:02 pm

Using 7.1.1:
viewtopic.php?f=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
SDS00004.png (19.44 KiB) Viewed 5943 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
SDS00005.png (17.53 KiB) Viewed 5943 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
SDS00006.png (17.33 KiB) Viewed 5943 times
Attachments
MB_Slave1.fcfx
(8.23 KiB) Downloaded 144 times
MASTER.fcfx
(10.64 KiB) Downloaded 150 times
These users thanked the author EtsDriver for the post:
LeighM (Wed Oct 12, 2016 8:37 am)
Rating: 5%
 
Ill just keep the good work up!

User avatar
LeighM
Matrix Staff
Posts: 2014
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 480 times
Been thanked: 691 times
Contact:

Re: Modbus RS485 mode CRC

Postby LeighM » Wed Oct 12, 2016 8:47 am

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: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4362 times
Contact:

Re: Modbus RS485 mode CRC

Postby Benj » Wed Oct 12, 2016 10:26 am

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 146 times


Let us know how you get on.
These users thanked the author Benj for the post:
EtsDriver (Wed Oct 12, 2016 2:16 pm)
Rating: 5%
 

EtsDriver
Valued Contributor
Valued Contributor
Posts: 437
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 348 times
Been thanked: 228 times
Contact:

Re: Modbus RS485 mode CRC

Postby EtsDriver » Wed Oct 12, 2016 2:15 pm

Hello, and thank you!

SDS00010.png
SDS00010.png (18.71 KiB) Viewed 5928 times

This seems to do the trick!

No problems anymore! This is great, now it works! :)
These users thanked the author EtsDriver for the post:
Benj (Wed Oct 12, 2016 4:57 pm)
Rating: 5%
 
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4362 times
Contact:

Re: Modbus RS485 mode CRC

Postby Benj » Wed Oct 12, 2016 5:14 pm

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: 437
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 348 times
Been thanked: 228 times
Contact:

Re: Modbus RS485 mode CRC

Postby EtsDriver » Thu Oct 13, 2016 9:52 am

Hi Benj!

I noticed that there is another bug inside the component, or this seems to be not implemented for some reason-->
SDS00030.png
SDS00030.png (14.4 KiB) Viewed 5909 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
SDS00029.png (15.1 KiB) Viewed 5909 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 154 times
ExceptionSlave.fcfx
(9.07 KiB) Downloaded 134 times
These users thanked the author EtsDriver for the post:
Benj (Thu Oct 13, 2016 1:02 pm)
Rating: 5%
 
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4362 times
Contact:

Re: Modbus RS485 mode CRC

Postby Benj » Fri Oct 14, 2016 12:39 pm

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 147 times
These users thanked the author Benj for the post:
EtsDriver (Fri Oct 14, 2016 8:30 pm)
Rating: 5%
 

EtsDriver
Valued Contributor
Valued Contributor
Posts: 437
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 348 times
Been thanked: 228 times
Contact:

Re: Modbus RS485 mode CRC

Postby EtsDriver » Fri Oct 14, 2016 7:03 pm

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:
These users thanked the author EtsDriver for the post:
Benj (Mon Oct 17, 2016 12:04 pm)
Rating: 5%
 
Ill just keep the good work up!

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1128
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 521 times
Contact:

Re: Modbus RS485 mode CRC

Postby Steve001 » Fri Oct 14, 2016 7:39 pm

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.5 KiB) Downloaded 151 times
Modbus Protocol.pdf
(172.26 KiB) Downloaded 148 times
These users thanked the author Steve001 for the post (total 2):
EtsDriver (Fri Oct 14, 2016 8:31 pm) • Benj (Mon Oct 17, 2016 12:04 pm)
Rating: 10%
 
Success always occurs in private and failure in full view.

User avatar
Benj
Matrix Staff
Posts: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4362 times
Contact:

Re: Modbus RS485 mode CRC

Postby Benj » Mon Oct 17, 2016 12:39 pm

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 111 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.
These users thanked the author Benj for the post:
EtsDriver (Mon Oct 17, 2016 2:39 pm)
Rating: 5%
 

EtsDriver
Valued Contributor
Valued Contributor
Posts: 437
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 348 times
Been thanked: 228 times
Contact:

Re: Modbus RS485 mode CRC

Postby EtsDriver » Mon Oct 17, 2016 5:13 pm

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
mbpol.PNG (22.52 KiB) Viewed 5811 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...
These users thanked the author EtsDriver for the post:
Benj (Tue Oct 18, 2016 6:04 pm)
Rating: 5%
 
Ill just keep the good work up!

User avatar
Benj
Matrix Staff
Posts: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4362 times
Contact:

Re: Modbus RS485 mode CRC

Postby Benj » Tue Oct 18, 2016 6:04 pm

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.61 KiB) Downloaded 144 times
These users thanked the author Benj for the post:
radist73 (Mon Oct 31, 2016 9:58 am)
Rating: 5%
 

radist73
Posts: 36
Joined: Mon Dec 22, 2014 10:04 pm
Location: TALLINN
Has thanked: 5 times
Been thanked: 2 times
Contact:

Re: Modbus RS485 mode CRC

Postby radist73 » Mon Oct 31, 2016 10:00 am

Many thanks!
I has the same problem with Modbus (232 works fine but 485 not) and now it is fixed!
These users thanked the author radist73 for the post:
Benj (Mon Oct 31, 2016 1:15 pm)
Rating: 5%