Search code examples
pythonnetworkingmodbus

Multi clients modbus to single modbus server


I have two python scripts which need to get data from the same modbus server.

However they cannot be simultaneously connected to the same modbus server, so I am looking for a "proxy" software.

This proxy will then sequentially transfer the modbus queries to the modbus server. Solution should run on debian 11 lite. Thanks!


Solution

  • Assuming you are dealing with Modbus TCP you can try modbus-proxy:

    Many modbus devices support only one or very few clients. This proxy acts as a bridge between the client and the modbus device. It can be seen as a layer 7 reverse proxy. This allows multiple clients to communicate with the same modbus device.

    When multiple clients are connected, cross messages are avoided by serializing communication on a first come first served REQ/REP basis.

    EDIT: According to your comment below:

    ...it is RTU over a linux file descriptor /dev/ttyS1

    That seems to me a not-so-straightforward way of saying that you are dealing with a serial link.

    If that's the case I'm afraid you need to go back all the way to the physical layer.

    If you have a point-to-point serial connection (no matter the voltage levels you have, they can be TTL 3.3VDC or RS-232) there is no way you can have more than one Modbus client connecting to the same server.

    For a serial link to have more than two devices (two or multiple clients sending queries to a Modbus server, for instance) you need a way for the devices to take control of the bus when they communicate and let go of it when they are idle. An RS-485 multipoint serial link is the most frequent solution to this problem.

    If you follow the link you will find many resources and documentation. That might be a bit overwhelming but it is actually very easy: instead of connecting RX to TX as you'd do for a normal serial port you will have two cables for each device (they are called A and B or D+ and D-) that you can put together. And you only need to add a TTL to RS-485 transceiver in between each device and the bus (or RS-232 to RS-485, depending on your devices).

    There are a couple of nuances:

    The most complete reference on all these topics I know of is Jan Axelson's book Serial Port Complete. It's a bit old (the last edition was released in 2007) but still very relevant. And you can find older editions secondhand for next to nothing. If you need to work with serial ports, this book will be a very valuable companion.

    Final note: if you are not able or willing to mess with the phy layer, you might, of course, find other solutions that are more palatable. You might, for instance, implement a software forwarder to take queries from your clients and convert them to Modbus RTU to be sent to your server. Pymodbus includes an example, but since you did not elaborate much I'm not sure it will be useful for your situation.