- 🔄 Protocol Forwarding: Support for forwarding between Modbus TCP and RTU protocols
- 🌐 Multi-Server Support: Connect to multiple Modbus slave devices simultaneously
- 📡 Complete Function Code Support: Support for all standard Modbus function codes
- 🔧 Flexible Configuration: Flexible connection parameter configuration via YAML files
- 📊 Connection Monitoring: Automatic connection status monitoring with reconnection support
- 🚀 Cross-Platform: Support for Linux, Windows, macOS and other platforms
| Function Code | Name | Description |
|---|---|---|
| 01 | Read Coils | Read single or multiple coil states |
| 02 | Read Discrete Inputs | Read single or multiple discrete input states |
| 03 | Read Holding Registers | Read single or multiple holding register values |
| 04 | Read Input Registers | Read single or multiple input register values |
| 05 | Write Single Coil | Write single coil state |
| 06 | Write Single Register | Write single register value |
| 15 | Write Multiple Coils | Write multiple coil states |
| 16 | Write Multiple Registers | Write multiple register values |
- Go 1.24.0 or higher
- Linux/Windows/macOS operating system
# Clone repository
git clone https://github.com/TwoMental/mb-forwarder.git
cd mb-forwarder
# Run
make runDownload pre-built versions suitable for your system from the Releases page.
Create a config.yaml file, following the format in config.yaml.example:
# Listen port
listen_port: 1602
# Server configuration
servers:
# Slave device 1 (TCP connection)
1:
conn_type: "tcp" # Connection type: "tcp" or "rtu"
slave_id: 1 # Slave device ID (1-255)
addr: "192.168.1.100" # TCP address or serial device name
port: 502 # TCP port (required for TCP connections)
timeout: 5 # Connection timeout in seconds
# Slave device 2 (RTU connection)
2:
conn_type: "rtu" # RTU connection
slave_id: 1
addr: "/dev/ttyUSB0" # Serial device name
baud_rate: 9600 # Baud rate
data_bits: 8 # Data bits
stop_bits: 1 # Stop bits
parity: "N" # Parity: "N"(none), "E"(even), "O"(odd)
timeout: 3listen_port: Port number for the forwarder to listen on, default 1602
conn_type: Connection type, supports "tcp" or "rtu"slave_id: Slave device ID, range 1-255addr: Connection address- TCP: IP address
- RTU: Serial device name (e.g.,
/dev/ttyUSB0,COM1)
port: TCP port number (required only for TCP connections)baud_rate: Baud rate (required only for RTU connections)data_bits: Data bits (required only for RTU connections)stop_bits: Stop bits (required only for RTU connections)parity: Parity (required only for RTU connections)timeout: Connection timeout in seconds
# Start with configuration file
./mb-forwarder -config config.yaml
# Or specify configuration file path
./mb-forwarder -config /path/to/config.yaml- Startup Phase: After startup, the forwarder creates a Modbus server and listens on the specified port
- Connection Initialization: Creates connections to various slave devices according to configuration
- Request Processing: Receives client requests, parses them, and forwards them to corresponding slave devices
- Response Return: Returns slave device responses to clients
- Connection Monitoring: Regularly checks connection status and records connection anomalies
The forwarder outputs detailed runtime logs:
2024/01/01 12:00:00 modbus forwarder listening on 0.0.0.0:1602
2024/01/01 12:00:00 initialized slave 1 connection (tcp)
2024/01/01 12:00:00 initialized slave 2 connection (rtu)
2024/01/01 12:00:00 modbus forwarder started with 2 servers
2024/01/01 12:00:00 Modbus forwarder started, press Ctrl+C to stop...
-
Connection Failures
- Check if slave devices are online
- Verify IP addresses and port numbers
- Confirm firewall settings
-
Serial Connection Issues
- Check serial device permissions
- Verify serial parameter settings
- Confirm device driver installation
-
Timeout Errors
- Increase timeout configuration values
- Check network latency
- Verify slave device response times
- Use connection pools to manage connections
- Set appropriate timeout values
- Monitor connection status and handle anomalies promptly
- Adjust buffer sizes based on network environment
Issues and Pull Requests are welcome!
This project is licensed under the LICENSE license.
- Project URL: https://github.com/TwoMental/mb-forwarder
- Issue Reports: Issues