Modbus RS485 mode CRC
Moderator: Benj
-
- Posts: 444
- Joined: Tue Apr 15, 2014 4:19 pm
- Location: Kajaani, Finland
- Has thanked: 345 times
- Been thanked: 227 times
- Contact:
Modbus RS485 mode CRC
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: 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:
********
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:
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: 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:
********
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:
- Attachments
-
- MB_Slave1.fcfx
- (8.23 KiB) Downloaded 319 times
-
- MASTER.fcfx
- (10.64 KiB) Downloaded 311 times
Ill just keep the good work up!
- LeighM
- Matrix Staff
- Posts: 2178
- Joined: Tue Jan 17, 2012 10:07 am
- Has thanked: 481 times
- Been thanked: 699 times
- Contact:
Re: Modbus RS485 mode CRC
Thanks for the comprehensive investigation. I think Ben has been looking at similar recently, so will no doubt post update soon.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Modbus RS485 mode CRC
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.
Let us know how you get on.
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.
Let us know how you get on.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 444
- Joined: Tue Apr 15, 2014 4:19 pm
- Location: Kajaani, Finland
- Has thanked: 345 times
- Been thanked: 227 times
- Contact:
Re: Modbus RS485 mode CRC
Hello, and thank you!
This seems to do the trick!
No problems anymore! This is great, now it works!
This seems to do the trick!
No problems anymore! This is great, now it works!
Ill just keep the good work up!
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Modbus RS485 mode CRC
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 444
- Joined: Tue Apr 15, 2014 4:19 pm
- Location: Kajaani, Finland
- Has thanked: 345 times
- Been thanked: 227 times
- Contact:
Re: Modbus RS485 mode CRC
Hi Benj!
I noticed that there is another bug inside the component, or this seems to be not implemented for some reason--> 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..
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.
I noticed that there is another bug inside the component, or this seems to be not implemented for some reason--> 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..
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 326 times
-
- ExceptionSlave.fcfx
- (9.07 KiB) Downloaded 289 times
Ill just keep the good work up!
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Modbus RS485 mode CRC
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 444
- Joined: Tue Apr 15, 2014 4:19 pm
- Location: Kajaani, Finland
- Has thanked: 345 times
- Been thanked: 227 times
- Contact:
Re: Modbus RS485 mode CRC
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...
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
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...
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
Ill just keep the good work up!
- Steve001
- Valued Contributor
- Posts: 1189
- Joined: Wed Dec 31, 2008 3:37 pm
- Has thanked: 460 times
- Been thanked: 523 times
- Contact:
Re: Modbus RS485 mode CRC
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
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 308 times
-
- Modbus Protocol.pdf
- (168.19 KiB) Downloaded 296 times
Success always occurs in private and failure in full view.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Modbus RS485 mode CRC
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.
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.
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 444
- Joined: Tue Apr 15, 2014 4:19 pm
- Location: Kajaani, Finland
- Has thanked: 345 times
- Been thanked: 227 times
- Contact:
Re: Modbus RS485 mode CRC
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...
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...
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...
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!
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Modbus RS485 mode CRC
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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
Many thanks!
I has the same problem with Modbus (232 works fine but 485 not) and now it is fixed!
I has the same problem with Modbus (232 works fine but 485 not) and now it is fixed!