Sharing a serial ports with libmodbus

50 views
Skip to first unread message

Dave Rathnow

unread,
Sep 23, 2016, 2:03:40 PM9/23/16
to libmodbus
I'm trying to fit libmodus into an existing application that runs on embedded Linux The application shares a serial port amoung threads where each thread must first lock the port, write, read, then unlock.  This app is written in C++ so all ports are wrapped in a class with the necessary methods to lock/unlock and read/write.  At application startup, the port is opened and held open during the life of the app.

The problem I ran into with libmodbus is that it expects to have exclusive access to the serial port.  I can create a new modbus context by calling modbus_new_rtu but then I have to call modbus_connect to open the port.  I'm currently trying this on a SUSE 11 system and, with the port already open in another thread, I can still call modbus_connect and everything works fine, even though this shouldn't work.  I should get an "access denied" error. I don't like this approach and don't want to use it on our embedded device.

Another approach I tried was the hack libmodbus by adding in a function modbus_set_handle that sets the port handle in the _modbus struct.  This *seems* to work but I don't know if there are other side effects from me dong this. Can someone tell me if this is a big no-no, other than the obvious "no support' issue.

Does anyone have a another solution to my problem?

Thanks,
Dave.

Stéphane Raimbault

unread,
Sep 24, 2016, 2:27:22 PM9/24/16
to libm...@googlegroups.com
You could handle the modbus connection in one thread and send requests (custom message with slave, function code, size to read/write, data and name of the message queue to send the answer) in the message queue of this thread. This way you can open the message queue with many threads w/o taking care of any lock.

--
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 obtenir davantage d'options, consultez la page https://groups.google.com/d/optout.

Dave Rathnow

unread,
Sep 26, 2016, 12:12:30 PM9/26/16
to libmodbus
Thanks for the reply Stephane,

That would work if all we did was talk modbus to the device but that's not the case.  Our application acts like master device sending modbus commands to slave devices but it also provides a facility for third party application to tunnel through our app to the slave devices.  There are a mix of threads that send modbus command and application commands over the same serial port. 


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.
Reply all
Reply to author
Forward
0 new messages