Modbus doubts - connection/ reconnection ... blocking/ non-blocking - with Raspberry Pi

468 views
Skip to first unread message

Hari

unread,
Jan 2, 2018, 5:39:38 AM1/2/18
to libmodbus

Hello all,

Happy new year! I am a masters student working on a project involving Modbus communication. I am using a Raspberry Pi (with TCP/IP) for the project along with libmodbus5 Debian release (version 3.0.6 -2). After I had encountered some problems I thought the better way is to ask someone who knows the library well. So here are my problems:

1. I guess the library uses a blocking connection call. (Can you please confirm the same?). I was not able to connect/reconnect to a device for some time (<4min) using the modbus_connect() function call. Is there a workaround rather than waiting for "connection timed out"? I had also set the time out using modbus_set_response_timeout() function, but it did not work. I guess it works only if there exists a connection and the server replies.


2. I had used the same modbus_connect() function call for a reconnection, in case any communication fails. Should I necessarily use modbus_close() and open the connection again ? I had implemented the same and shielded the modbus_close() with a sleep(5) to make sure that closing the socket does not affect the other devices connected to the same port 502. This had avoided the “Bad file descriptor” error if any communication was made immediately after closing the port.

3. I had seen the documentation and one of them stated that non-blocking modes were already implemented (from version 3.1). So can I download, compile and use the same on Pi ? Does it work directly after compiling the C files to get .o files and linking them? What might be the possible disadvantages? (I suppose one might be that it was not ARM specific.). I need your advice in this direction as I am new to Linux/ Debian.

regards

Stéphane Raimbault

unread,
Jan 3, 2018, 8:09:43 AM1/3/18
to libm...@googlegroups.com
2018-01-02 11:39 GMT+01:00 Hari <93hari...@gmail.com>:

Hello all,

Happy new year! I am a masters student working on a project involving Modbus communication. I am using a Raspberry Pi (with TCP/IP) for the project along with libmodbus5 Debian release (version 3.0.6 -2). After I had encountered some problems I thought the better way is to ask someone who knows the library well. So here are my problems:

1. I guess the library uses a blocking connection call. (Can you please confirm the same?). I was not able to connect/reconnect to a device for some time (<4min) using the modbus_connect() function call. Is there a workaround rather than waiting for "connection timed out"? I had also set the time out using modbus_set_response_timeout() function, but it did not work. I guess it works only if there exists a connection and the server replies.


You can read the source code to be sure so yes it's a blocking call:

vs the master version (non blocking if available):

modbus_set_response_timeout() http://libmodbus.org/docs/v3.1.4/modbus_set_response_timeout.html is used to set response timeout (not related to the connect timeout).


2. I had used the same modbus_connect() function call for a reconnection, in case any communication fails. Should I necessarily use modbus_close() and open the connection again ?

I had implemented the same and shielded the modbus_close() with a sleep(5) to make sure that closing the socket does not affect the other devices connected to the same port 502. This had avoided the “Bad file descriptor” error if any communication was made immediately after closing the port.

I don't know why the port isn't available immediately after a modbus_close(). 

3. I had seen the documentation and one of them stated that non-blocking modes were already implemented (from version 3.1). So can I download, compile and use the same on Pi ?

yes

Does it work directly after compiling the C files to get .o files and linking them?

yes 

What might be the possible disadvantages? (I suppose one might be that it was not ARM specific.).

I only see advantages to use the latest version! 

Stéphane

Hari

unread,
Jan 4, 2018, 1:20:33 AM1/4/18
to libmodbus
Hello,

 Thank you so much for your reply. It was very much helpful.

regards,
Hari
Reply all
Reply to author
Forward
0 new messages