Skip to content

streef/modbus-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

Multithreaded Modbus proxy from Modbus TCP to Modbus TCP or RTU

modbus-proxy.py

modbus-proxy.py is a Python script that implements a Modbus proxy. It connects to a Modbus server either via the network (Modbus TCP) or via a serial port (Modbus RTU). It accepts one or more TCP client connections, forwards requests from the client to the server and sends the replies back to the client.

Depending on the connection strategy selected by the -c option, the server connection is permanent, on-demand when at least one client is connected (default), or set up separately for each request.

The -u option can be used to translate the unit ID of the client to a different unit ID for the server.

The -v option enables different levels of verbose output to be able to trace the Modbus connections and requests. The following Modbus requests are decoded with -vv or higher:

  • 3: Read Holding Registers
  • 6: Write Single Register
  • 16: Write Multiple Registers

With the -l option, the verbose output can be redirected to a log file. This log file is reopened for every line so that it can be rotated by an external tool.

Typical use cases

  • Allow network access to a serial Modbus connection
  • Allow multiple clients to connect to a server that only supports a single connection at a time
  • Translate TCP port number
  • Translate Modbus unit ID
  • Tracing and logging

Usage

usage: modbus-proxy.py [-h] [-a ADDR] [-p PORT] [-c CONN] [-u UNIT:UNIT] [-v]
                       [-F ADDR] [-D] [-l LOG]
                       [tcp|rtu] ...

Multithreaded Modbus proxy from Modbus TCP to Modbus TCP or RTU

positional arguments:
  [tcp|rtu]
    tcp         connect to Modbus TCP server
    rtu         connect to Modbus RTU server

options:
  -h, --help    show this help message and exit
  -a ADDR       IP address or host name to listen on (default: 0.0.0.0)
  -p PORT       port number to listen on (default: 502)
  -c CONN       server connection strategy (default: 2)
                1 - permanent connection
                2 - connect when at least one client is connected
                3 - separate connection for every request
  -u UNIT:UNIT  translate unit ID (may be used multiple times)
  -v            verbose output (may be used multiple times)
                -v    client connections
                -vv   decoded register read/write requests (filtered by -F)
                -vvv  server connections and PDUs
                -vvvv low-level TCP or RTU communication with client & server
  -F ADDR       suppress -vv output for an address (may be used multiple times)
  -D            suppress duplicate -vv output
  -l LOG        write verbose output to a log file instead of stdout

Additional positional arguments for TCP:

usage: modbus-proxy.py tcp [-h] host [port]

positional arguments:
  host        host name
  port        port number (default: 502)

Additional positional arguments for RTU:

usage: modbus-proxy.py rtu [-h] device [baudrate]

positional arguments:
  device      RS-485 device name
  baudrate    RS-485 baud rate (default: 115200)

Dependencies

The modbus-proxy.py script requires the pyserial Python module, which is distributed as a separate python3-serial package in Ubuntu.

License

Modbus-proxy.py is free software, licensed under the terms of the GNU General Public License as published by the Free Software Foundation.

About

Multithreaded Modbus proxy from Modbus TCP to Modbus TCP or RTU

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages