Modbus TCP/IP server that translates incoming requests to RTU and relays them to configured serial device.
Caution
🚧 This project is still in WIP 🚧
- Thessla Green recuperator on
/dev/ttyAMA0
modbus-relay
running on Raspberry Pi 3 Model B+, with debug enabled
- Client using rust & tokio-modbus, connects via TCP/IP
- Exposes RTU device via TCP/IP
- Option to flush after every write1
- Option to manual control of RTS1
- Simple configuration via JSON in
/etc/modbus-relay.json
- Made with modern C++23
- cmake for building
- ASIO for networking/io
- libmodbus for Modbus
- spdlog for logging
- fmt for formatting
- nlohmann_json for JSON
-
Handle errors
-
Handle function code 0x01 - Read Coil Status
-
Handle function code 0x02 - Read Input Status
-
Handle function code 0x03 - Read Holding Registers
-
Handle function code 0x04 - Read Input Registers
-
Handle function code 0x05 - Force Single Coil
-
Handle function code 0x06 - Preset Single Register
-
Handle function code 0x0F - Force Multiple Coils
-
Handle function code 0x10 - Preset Multiple Registers
-
Handle function code 0x10 - Preset Multiple Registers
-
Handle multiple connections
-
CI/CD
-
Debian/ArchLinux packages
-
systemd service
Configuration file is in /etc/modbus-relay.json
. See Usage on how to generate it.
TCP/IP server options:
tcp_ip
- on which interface to listen to, ex."0.0.0.0"
tcp_port
- on which port to listen to, ex.5000
RTU client options:
rtu_device
- tty device, ex."/dev/ttyAMA0"
rtu_serial_type
- serial type,"RS232"
|"RS485"
rtu_baud_rate
- baud rate, ex.9600
rtu_parity
- parity,"None"
|"Even"
|"Odd"
rtu_data_bits
- data bits,5
|6
|7
|8
rtu_stop_bit
- stop bit,1
|2
rtu_slave_id
- slave id in decimal, ex.10
(0x0A
)rtu_rts_enabled
- wheter to use RTS,true
|false
rtu_rts_type
- RTS type,"None"
|"Up"
|"Down"
rtu_rts_delay
- delay in us, ex.3500
rtu_rts_manual_control
- wheter to manually setTIOCM_RTS
viaioctl
onrtu_device
, before and after each write,true
|false
rtu_flush_after_write
- wheter to manually calltcflush(rtu_device, TCIOFLUSH)
after each write,true
|false
rtu_enable_libmodbus_debug
- to enablelibmodbus
debug logging,true
|false
Install dependencies mentioned in tech stack section
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build .
To generate config file
sudo sh -c './modbus-relay -dump-default-config > /etc/modbus-relay.json'
Just run it
./modbus-relay
Needed to expose RTU device via TCP/IP, because developing on RPI3B+ was painfully slow and full of OOMs