I'm developing a C++ Modbus application which already uses the libmodbuspp library to implement a Modbus Master device to query Modbus Slaves either in TCP or RTU modes (respectively for devices connected over an Ethernet network or RS-232/485 serial links). It is already working fine, but recently a new requirement was set that this application should also implement RTU over TCP, so it would be able to communicate over TCP with a gateway which has many RTU Modbus slave devices connected to its many serial ports (the gateway just forwards the RTU packets to the corresponding slave IDs set in them). Basically this means our application should send Modbus RTU PDUs over a TCP/IP connection instead of a serial port.

For a quick and dirty solution (might help somebody else), I used socat to create a virtual serial port with an outbound TCP connection to the target TCP gateway, making my application just work in its regular RTU mode (the virtual serial port being /dev/ttyS4 and the TCP gateway endpoint 192.168.0.10:8000):

socat -d -d pty,link=/dev/ttyS4,raw,echo=0 tcp-connect:192.168.0.10:8000

but for the end application I wanted something cleaner, without depending on external apps.

I was wondering if I could use libmodbuspp's virtual-rtu layer to achieve this. Although the libmodbuspp documentation is very good, it seems this is a new feature, so it is not so clear yet to me. The docs and examples only show it working for receiving RTU over TCP through a Server or Router (an specialized Server), which in turn can reach RTU slaves associated to these objects, but what I need is exactly the opposite: the ability to connect to a TCP endpoint as a client, but sending it Modbus RTU instead of Modbus TCP datagrams.

Since the libmodbuspp library is actually a C++ wrapper to the well known Modbus C library libmodbus, there is also the possibility of using this fork of libmodbus which added support for RTU over TCP, but I would rather not have to reimplement the support in the C++ wrapper library libmodbuspp if somehow it already supports it (but maybe in an incomplete way - not in the client connection). Maybe somebody more familiar with libmodbuspp codebase could give me some pointers here? Thanks!

0

There are 0 answers