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.
This is a fork of tiagocoutinho/modbus-proxy-rs, the Rust version of the Python based modbus-proxy project. On top of the original it adds:
- Register descriptions in the log: with a register map, every request is logged with register names, decoded values and the client that made it. A map for Sigenergy inverters is included.
- Robustness: timeouts on device and client I/O so a stalled device no
longer leaves sockets in
CLOSE_WAIT(upstream PR #12, issue #7), accept errors such as running out of file descriptors are retried instead of crashing, and a busy listen port is reported instead of panicking. - Docker Compose setup configured from a
.envfile. - Up to date dependencies (clap instead of the unmaintained structopt, clean
cargo audit) and tests with 100% line coverage.
Build from source (needs Rust 1.99 or newer):
$ cargo install --git https://github.com/clawster/modbus-proxy-rsor clone the repository and run cargo build --release. Note that
cargo install modbus-proxy-rs installs the original crate from crates.io,
without the changes of this fork. See Docker to run it in a
container instead.
First, you will need write a configuration file where you specify for each modbus device you which to control:
- modbus connection (the modbus device url)
- listen interface (to which url your clients should connect)
Configuration files can be written in YAML (.yml or .yaml) or TOML (.toml).
Suppose you have a PLC modbus device listening on plc1.acme.org:502 and you want your clients to connect to your machine on port 9000. A YAML configuration would look like this:
devices:
- modbus:
url: plc1.acme.org:502 # device url (mandatory)
listen:
bind: 0:9000 # listening address (mandatory)Assuming you saved this file as modbus-config.yml, start the server with:
$ modbus-proxy-rs -c ./modbus-config.ymlNow, instead of connecting your client(s) to plc1.acme.org:502 you just need to
tell them to connect to *machine*:9000 (where machine is the host where
modbus-proxy is running).
Note that the server is capable of handling multiple modbus devices. Here is a configuration example for 2 devices:
devices:
- modbus:
url: plc1.acme.org:502
listen:
bind: 0:9000
- modbus:
url: plc2.acme.org:502
listen:
bind: 0:9001Restrict which clients may connect with an ip_filter, either globally (for
every device) or per device. Each filter has an allow and a deny list of
addresses (10.0.0.5, fd00::1) or networks (10.0.0.0/24, fd00::/8):
ip_filter: # global: applies to all devices
allow: [10.0.0.0/24, 192.168.1.0/24]
deny: [10.0.0.66]
devices:
- modbus:
url: 10.0.0.30:502
listen:
bind: 0.0.0.0:10502
ip_filter: # this device only
allow: [10.0.0.5, 10.0.0.6]A client matching deny is refused; if allow is not empty, only clients
matching it are accepted. Without a filter (or with empty lists) everyone is
accepted. A client must pass both the global and the device filter, so a
device filter can only narrow down what the global one lets through. In the
example only 10.0.0.5 and 10.0.0.6 reach the device. IPv4 clients of an IPv6
listen address ([::]:10502) are matched against IPv4 networks.
Refused clients are disconnected right away and logged with a warning. The proxy refuses to start if a filter contains an invalid entry.
Log levels can be adjusted by setting the RUST_LOG environment variable
(default is info):
$ RUST_LOG=debug modbus-proxy-rs -c ./modbus-config.ymlEvery status_interval seconds (default 120, 0 disables it) each device
logs a summary: the number of connected clients and the reads, writes and
errors since the last summary, then one line per connected client with its
totals:
status_interval: 120
devices:
...INFO modbus_proxy_rs::status] sigenergy: 2 clients, last 2m00s: 1240 reads, 1 writes, 0 errors
INFO modbus_proxy_rs::status] sigenergy: client 10.0.0.5:51234 connected 3h12m05s: 69120 reads, 0 writes, 0 errors
INFO modbus_proxy_rs::status] sigenergy: client 10.0.0.6:40112 connected 5m10s: 3100 reads, 1 writes, 0 errors
Errors are exception replies and requests the device didn't answer.
Give a device a register map and every request is logged in words, with register names, decoded values (gain and unit applied) and descriptions:
devices:
- modbus:
url: 10.0.0.30:502
name: sigenergy # optional, defaults to the url
registers: registers/sigenergy.json # optional
listen:
bind: 0.0.0.0:10502DEBUG modbus_proxy_rs::registers] 10.0.0.5:51234 -> sigenergy: unit 247 read 30005 plant_grid_sensor_active_power = -1.234 kW (Grid Active Power (>0 buy from grid; <0 sell to grid))
INFO modbus_proxy_rs::registers] 10.0.0.5:51234 -> sigenergy: unit 247 write 40000 plant_start_stop = 1 (Start/Stop (0: Stop 1: Start))
Each line starts with the address of the client that sent the request and
the device name.
Reads (functions 3/4), writes (6/16) and exception replies are described.
The lines are logged under the modbus_proxy_rs::registers target: reads
at debug (clients poll constantly), writes at info and exception
replies at warn. To see reads as well use
RUST_LOG=info,modbus_proxy_rs::registers=debug.
A register map is a JSON file listing address, count, type (u16,
s16, u32, s32, u64, string), gain, and optionally unit, name
and description per register; see registers/. The proxy
refuses to start if a configured map can't be loaded.
registers/sigenergy.json is generated from the register definitions of the Sigenergy-Local-Modbus Home Assistant integration (MIT), included as a git submodule:
$ git submodule update --init
$ python3 -I tools/gen_register_map.py \
Sigenergy-Local-Modbus/custom_components/sigen/modbusregisterdefinitions.py \
registers/sigenergy.jsonThe generator only parses the Python source, it doesn't import it, so Home
Assistant isn't needed. It also applies corrections checked against the
Sigenergy protocol document (e.g. the inverter power factor at 31023 is
signed), listed in CORRECTIONS in the script.
This project ships with a Dockerfile which you can use as a base to launch modbus-proxy inside a docker container.
The easiest way is the supplied compose.yml. It is configured
from a .env file next to it, from which compose generates the proxy
configuration (no config file needed):
$ cp .env.example .env # then set MODBUS_DEVICE and PROXY_PORT
$ docker compose build
$ docker compose up -d
$ docker compose logs -f| Variable | Example | Meaning |
|---|---|---|
MODBUS_DEVICE |
10.0.0.30:502 |
modbus device to proxy (host:port) |
PROXY_PORT |
10502 |
port clients connect to |
MODBUS_NAME |
sigenergy |
device name in the register log (defaults to MODBUS_DEVICE) |
REGISTER_MAP |
/etc/modbus-proxy/registers/sigenergy.json |
register map for the log (shipped in the image) |
STATUS_INTERVAL |
120 |
seconds between status summaries, 0 disables |
IP_ALLOW |
10.0.0.0/24,10.0.0.5 |
clients allowed to connect, empty allows all (see IP filter) |
IP_DENY |
10.0.0.66 |
clients refused even if allowed |
RUST_LOG |
info |
log level (debug dumps requests/replies) |
Clients connect to <your-hostname/ip>:$PROXY_PORT. After code changes,
rebuild and restart with docker compose up -d --build. To proxy more than
one device, add entries to the configs section of compose.yml, each with
its own listen port, and publish those ports.
First, build the docker image with:
$ docker build -t modbus-proxy .Assuming you have prepared a config.yml in the current directory:
devices:
- modbus:
url: plc1.acme.org:502
listen:
bind: 0.0.0.0:5020The supplied docker image by default runs the command /modbus-proxy-rs -c /etc/modbus-proxy.yml.
Therefore, running launching a container is as simple as:
docker run --init --rm -p 5020:5020 -v $PWD/config.yml:/etc/modbus-proxy.yml modbus-proxyYou can supply a different configuration path (ex: /config.yml):
docker run --init --rm -p 5020:5020 -v $PWD/config.yml:/config.yml modbus-proxy -c /config.ymlNow you should be able to access your modbus device through the modbus-proxy by
connecting your client(s) to <your-hostname/ip>:5020.
Note that for each modbus device you add in the configuration file you need
to publish the corresponding bind port on the host
(-p <host port>:<container port> argument).
The container runs as an unprivileged user, so bind ports above 1024 inside
the container (e.g. 0.0.0.0:5020) and map them to any host port you like;
binding :502 inside the container fails with Permission denied.
$ cargo test # unit tests, including real loopback sockets
$ cargo clippy --all-targets
$ ./coverage.sh # line/region coverage report (needs llvm-tools and jq)
$ ./coverage.sh --show # ... with annotated sourceThe code is split by the path a request takes: bridge.rs (clients) sends it
over a channel to device.rs (the device connection); frame.rs reads Modbus
TCP frames, modbus.rs and registers.rs describe them, ipfilter.rs decides
which clients may connect, settings.rs is the
configuration format and server.rs ties it together.
Copyright (C) 2020 Tiago Coutinho, and the contributors of this fork.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. See LICENSE.
The Sigenergy register map is derived from the Sigenergy-Local-Modbus integration, MIT licensed, Copyright (c) 2025 Andrei Ignat; see registers/LICENSE.sigenergy. The Docker image ships both license texts.
This project is not affiliated with or endorsed by Sigenergy Technology Co., Ltd.
- Original project and author: modbus-proxy-rs by Tiago Coutinho coutinhotiago@gmail.com
- Upstream contributors: Ingmar Stein (reference counting fix, smaller binaries), Daniel Gruber (shutdown on Ctrl+C/SIGTERM), and schakko, whose PR #12 the I/O timeouts are based on
- Sigenergy register definitions: Sigenergy-Local-Modbus