Unknown Intermittent Error with modbus_read_registers()

457 views
Skip to first unread message

Software Monkey

unread,
Apr 3, 2014, 12:48:45 PM4/3/14
to libm...@googlegroups.com
My PC based application uses LibModbus to communicate with a PLC using modbus TCP. Everything works OK for a few minutes then modbus_read_registers() starts to fail (returns -1). I have checked modbus_strerror and it returns "No Error". I can still write to the PLC without any problems.

If I disconnect then reconnect, it starts to work again. The time between connecting and it failing is not always the same. I am reading a total of 5 registers every 500mSec and I am using LibModBus version 3.1.0.2

Has anyone experienced this before or have any suggestions as to what I can try.

Thanks

Tony

Inatec Engenharia

unread,
May 27, 2014, 1:35:20 PM5/27/14
to libm...@googlegroups.com
Hi,

I have the same problem to use the function modbus_read_registers() that return -1. In my case I never got to read holding registers which are at address 400001-400010. 

Please,You could be send for me your code?

Waiting your response.

Thank you.

Luis

Tony Dean

unread,
May 29, 2014, 11:08:54 AM5/29/14
to libm...@googlegroups.com
Hi Luis.

Since posting my question, I traced into libmodbus and found that the problem was that the transaction ID being returned by the PLC was incorrect. I think one request got lost somewhere so from that point forward, the transaction ID returned was always different to the one  issued by libmodbus. I never got to find out where it was going wrong but it worked OK with another slave device. If the code you are you are referring to is my Visual Studio 2008 libmodbus project, you should be able to get it here https://onedrive.live.com/redir?resid=228E4618763618EC!3426&authkey=!AGmLqE4Z2TlRhI8&ithint=file%2c.7z

Please let me know whether you are able to download it OK. There is a simple test application in the project which reads registers on a timer - just to prove that the library works! If you need any information please let me know.

Regards,

Tony


--
Vous recevez ce message, car vous êtes abonné à un sujet dans le groupe Google Groupes "libmodbus".
Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/libmodbus/OhSw9dsJAkk/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Inatec Engenharia

unread,
May 29, 2014, 2:26:04 PM5/29/14
to libm...@googlegroups.com
Thank you for your reply Monkey.

You already send for me this your code example and it was very useful for me to understand how to compile the library modbus.

But, when I use the libmodbus functions not work, always I got error and have not communication.

Are you have any tutorial or e-book that where teach the algorithms and how to use the functions of libmodbus (modbus)? The documentation of site and examples available on the website is not completed and is not useful.

Luis

Chuck Watson

unread,
May 29, 2014, 2:40:02 PM5/29/14
to libm...@googlegroups.com

Double check the hardware.

 

Get an application that you know works like Kepware (demo mode) and verify that all the hardware (master, client, and any cabling or interface cards/cables) works first.

 

When I first started using the library, my first USB RS485 cable worked great. The second identical cable I received required biasing resistors even in my test setup. If I would have received the second unit originally, I would have had a lot of troubleshooting issues.

 

Chuck Watson BS, CEM, CMVP
Direct USA 724-799-1529
www.EnergyChaser.com


--
Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "libmodbus".
Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse libmodbus+...@googlegroups.com.

Tony Dean

unread,
May 29, 2014, 4:08:57 PM5/29/14
to libm...@googlegroups.com
I am unsure of the nature of your problem but it might be a good idea to test your modbus read/write using a simulator running on the same PC. I use Mod_RSim. If you specify the loopback IP address (127.0.0.1) when you call modbus_new_tcp as in my test application (and also shown below), it should connect to Mod_RSim and you should be able to read/write data. At least this way you can rule out any connection problems.

    mb = modbus_new_tcp("127.0.0.1", 502);
    modbus_connect(mb);

Hope this helps.

Tony

Inatec Engenharia

unread,
May 30, 2014, 8:30:17 AM5/30/14
to libm...@googlegroups.com
Chuck Watson, thank you for your reply.

Now I am trying to use the Mod_RSim and others software where I can test my connection with iFix SCADA and it works fine. 

The problem is to read/write any data of the libmodbus with my owner software. I guess It's happened, because I don't understand how modbus works right.
I read the specification of the site modbus.org, but is not friendly to understand how it works in detail. 
Are you have any example (code) or reference (books) that can advise me.

Thank you.

Luis

Inatec Engenharia

unread,
May 30, 2014, 8:44:47 AM5/30/14
to libm...@googlegroups.com
Software Monkey, thank you for your reply.

I already have using like a Mod_RSim to test my connection with iFix SCADA and it works fine. My problem is to read/write any data using the functions available in libmodbus. I guess it happens, because I don't understand very well how it works, may be I need to wait a response (some time in milliseconds) or I need to send any information to obtain the data.
And I already have using the examples available in the website of the libmodbus and it not works to read/write any date, whats works is just the connection.
Are you have any example (code) or reference (books) that can advise me.

Thank you

Luis

Chuck Watson

unread,
May 30, 2014, 8:48:02 AM5/30/14
to libm...@googlegroups.com

Get the Kepware software. It’s free (runs for 1 hour) for demonstration purposes and the documentation with this software is great. You will want the MODBUS Suite software driver.

Reply all
Reply to author
Forward
0 new messages