Skip to content

Modbus TCP/IP server that translates incoming requests to RTU and relays them to configured serial device.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

aljen/modbus-relay

Repository files navigation

modbus-relay

🚀 High-performance Modbus TCP to RTU relay written in Rust

Crates.io Documentation License Build Status

FeaturesInstallationUsageConfigurationMonitoringContributing

🌟 Features

  • 🔄 Transparent TCP to RTU protocol conversion
  • 🚀 High-performance asynchronous I/O with Tokio
  • 🔧 Advanced RS485 support with configurable RTS control
  • 🛡️ Robust error handling and connection management
  • ⚡ Zero-copy buffer handling for optimal performance
  • 📝 Structured logging with multiple output formats
  • 🔌 Connection pooling with per-IP limits
  • 🔄 Automatic reconnection with configurable backoff
  • 🎯 Comprehensive test suite
  • 📊 Built-in metrics and monitoring via HTTP API

🚀 Quick Start

Installation

# Install from crates.io
cargo install modbus-relay

# Or build from source
git clone https://github.com/aljen/modbus-relay
cd modbus-relay
cargo build --release

Basic Usage

# Generate default configuration
modbus-relay --dump-default-config > config.yaml

# Run with custom config
modbus-relay -c /path/to/config.yaml

# Run with default settings
modbus-relay

⚙️ Configuration

Configuration is managed through YAML files. Here's a complete example (config.yaml):

tcp:
  bind_addr: "0.0.0.0"
  bind_port: 502

rtu:
  device: "/dev/ttyUSB0"
  baud_rate: 9600
  data_bits: 8
  parity: "none"
  stop_bits: 1
  flush_after_write: true
  rts_type: "none"  # Options: none, up, down
  rts_delay_us: 0
  transaction_timeout: "1s"
  serial_timeout: "100ms"
  max_frame_size: 256

http:
  enabled: true
  bind_addr: "127.0.0.1"
  bind_port: 8080
  metrics_enabled: true

connection:
  max_connections: 100
  idle_timeout: "60s"
  connect_timeout: "5s"
  per_ip_limits: 10
  backoff:
    initial_interval: "100ms"
    max_interval: "30s"
    multiplier: 2.0
    max_retries: 5

logging:
  trace_frames: false
  log_level: "info"
  format: "pretty"  # Options: pretty, json
  include_location: false

📊 Monitoring

The HTTP API provides basic monitoring endpoints:

  • GET /health - Health check endpoint
  • GET /status - Detailed status information

Planned monitoring features:

  • Prometheus metrics support
  • OpenTelemetry integration
  • Advanced connection statistics
  • Detailed performance metrics

🔍 Examples

Industrial Automation Setup

modbus_relay.png

Example setup running on Raspberry Pi with multiple Modbus RTU devices connected via RS485.

🛠️ Tech Stack

Coming Soon

  • Prometheus metrics integration
  • OpenTelemetry support

📚 Documentation

🤝 Contributing

Contributions are welcome! Please check out our:

📄 License

This project is licensed under either of

at your option.