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

Features β€’ Installation β€’ Usage β€’ Configuration β€’ Monitoring β€’ Contributing

🌟 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.