connection status

360 views
Skip to first unread message

Francisco Ares

unread,
Mar 3, 2015, 7:44:30 AM3/3/15
to libm...@googlegroups.com
Hi, All.

Is there any means to check for the connection status before any read or write operation? Or is it enough to try read or write and check the result of the operation, and based on that, consider the connection status?

And if the connection is lost, be the program a client or a server, is the re-connection procedure identical as the first connection, I mean, should I delete the context or just keep it and try connecting again?

Thanks!
Francisco

Julien Blanc

unread,
Mar 4, 2015, 3:48:42 AM3/4/15
to libm...@googlegroups.com
On 03/03/2015 13:43, Francisco Ares wrote:
> Hi, All.
>
> Is there any means to check for the connection status before any read
> or write operation? Or is it enough to try read or write and check the
> result of the operation, and based on that, consider the connection
> status?

You can’t check without trying to read / write (at least in TCP, for RTU
i’m not sure). So yes, you need to check the return value.

>
> And if the connection is lost, be the program a client or a server, is
> the re-connection procedure identical as the first connection, I mean,
> should I delete the context or just keep it and try connecting again?

Delete the context and create a new one. That’s inexpensive, and will
guarantee you a clean start (i remember having had issues with reusing a
context).

Regards,

Julien Blanc

Francisco Ares

unread,
Mar 4, 2015, 4:22:18 AM3/4/15
to libm...@googlegroups.com
2015-03-04 5:48 GMT-03:00 Julien Blanc <julien...@nmc-company.fr>:
On 03/03/2015 13:43, Francisco Ares wrote:
Hi, All.

Is there any means to check for the connection status before any read or write operation? Or is it enough to try read or write and check the result of the operation, and based on that, consider the connection status?

You can’t check without trying to read / write (at least in TCP, for RTU i’m not sure). So yes, you need to check the return value.

Sorry, I forgot to tell I'm using TCP mode. Just for completeness, Modbus TCP mode is a protocol over another protocol, and still there are the TCP/IP logical layers down to the ethernet physical one (that's why I asked), but as far as we are using just the top protocol, I understand and it makes perfect sense when you say that there is no other way of checking connection status;  but for RTU mode, it's Modbus directly over a physical layer, so the answer, as far as I can understand, is the same, there is also no other way to check for connection status but trying to read or write something.


And if the connection is lost, be the program a client or a server, is the re-connection procedure identical as the first connection, I mean, should I delete the context or just keep it and try connecting again?

Delete the context and create a new one. That’s inexpensive, and will guarantee you a clean start (i remember having had issues with reusing a context).

Thanks for the tip. my program was written to keep the context and I got some weirdness, maybe that's what you are talking about. After the program was modified to delete the context on read or write operation error (and also closing the connection and trying for a new one, of course), now it works as expected.
 
Regards,

Julien Blanc

--
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+unsubscribe@googlegroups.com.
Pour plus d'options, visitez le site https://groups.google.com/d/optout .


Thank you again, and Best Regards.
Francisco

Stéphane Raimbault

unread,
Mar 11, 2015, 1:06:59 PM3/11/15
to libm...@googlegroups.com
You don't need to delete the context, you can just close the connection and open it again (modbus_close/connect).

See http://libmodbus.org/docs/v3.1.2/modbus_set_error_recovery.html for information about the ways to handle connection errors in 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.
Pour obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Francisco Ares

unread,
Mar 11, 2015, 1:24:07 PM3/11/15
to libm...@googlegroups.com
Thank you, gonna try it out immediately.

Best regards,
Francisco

Reply all
Reply to author
Forward
0 new messages