Page 1 of 1

Modbus RS485 mode CRC

Posted: Tue Oct 11, 2016 9:02 pm
by EtsDriver
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 (19.44 KiB) Viewed 5939 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) Viewed 5939 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.33 KiB) Viewed 5939 times

Re: Modbus RS485 mode CRC

Posted: Wed Oct 12, 2016 8:47 am
by LeighM
Thanks for the comprehensive investigation. I think Ben has been looking at similar recently, so will no doubt post update soon.

Re: Modbus RS485 mode CRC

Posted: Wed Oct 12, 2016 10:26 am
by Benj

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.

(18.36 KiB) Downloaded 146 times

Let us know how you get on.

Re: Modbus RS485 mode CRC

Posted: Wed Oct 12, 2016 2:15 pm
by EtsDriver
Hello, and thank you!

SDS00010.png (18.71 KiB) Viewed 5924 times

This seems to do the trick!

No problems anymore! This is great, now it works! :)

Re: Modbus RS485 mode CRC

Posted: Wed Oct 12, 2016 5:14 pm
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.

Re: Modbus RS485 mode CRC

Posted: Thu Oct 13, 2016 9:52 am
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) Viewed 5905 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) Viewed 5905 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.

Re: Modbus RS485 mode CRC

Posted: Fri Oct 14, 2016 12:39 pm
by Benj

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.

(89 KiB) Downloaded 147 times

Re: Modbus RS485 mode CRC

Posted: Fri Oct 14, 2016 7:03 pm
by EtsDriver

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 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:

Re: Modbus RS485 mode CRC

Posted: Fri Oct 14, 2016 7:39 pm
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


Re: Modbus RS485 mode CRC

Posted: Mon Oct 17, 2016 12:39 pm
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.

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

Re: Modbus RS485 mode CRC

Posted: Mon Oct 17, 2016 5:13 pm
by EtsDriver

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) Viewed 5807 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...

Re: Modbus RS485 mode CRC

Posted: Tue Oct 18, 2016 6:04 pm
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.

(89.61 KiB) Downloaded 144 times

Re: Modbus RS485 mode CRC

Posted: Mon Oct 31, 2016 10:00 am
by radist73
Many thanks!
I has the same problem with Modbus (232 works fine but 485 not) and now it is fixed!