Skip to content

Latest commit

 

History

History
127 lines (96 loc) · 3.5 KB

README.md

File metadata and controls

127 lines (96 loc) · 3.5 KB

tokio-modbus

A tokio-based modbus library.

Crates.io version Docs Build Status Build status Coverage Status

Features

  • pure Rust library
  • async (non-blocking)
  • sync (blocking)
  • Modbus TCP
  • Modbus RTU
  • Client & Server
  • Open Source (MIT/Apache-2.0)

Installation

Add this to your Cargo.toml:

[dependencies]
tokio-modbus = "*"

If you like to use Modbus TCP only:

[dependencies]
tokio-modbus = { version = "*", default-features = false, features = ["tcp"] }

If you like to use Modbus RTU only:

[dependencies]
tokio-modbus = { version = "*", default-features = false, features = ["rtu"] }

Examples

TCP client

use tokio_core::reactor::Core;
use futures::future::Future;
use tokio_modbus::*;

pub fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();
    let addr = "192.168.0.222:502".parse().unwrap();

    let task = Client::connect_tcp(&addr, &handle).and_then(|client| {
        client
            .read_input_registers(0x1000, 7)
            .and_then(move |data| {
                println!("Response is '{:?}'", data);
                Ok(())
            })
    });
    core.run(task).unwrap();
}

Sync TCP client

use tokio_modbus::*;

pub fn main() {
    let addr = "192.168.0.222:502".parse().unwrap();
    let mut client = SyncClient::connect_tcp(&addr).unwrap();
    let buff = client.read_input_registers(0x1000, 7).unwrap();
    println!("Response is '{:?}'", buff);
}

RTU client

use tokio_core::reactor::Core;
use futures::future::Future;
use tokio_modbus::*;
use tokio_serial::{Serial, SerialPortSettings};

pub fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();
    let tty_path = "/dev/ttyUSB0";
    let server_addr = 0x01;

    let mut settings = SerialPortSettings::default();
    settings.baud_rate = 19200;
    let mut port = Serial::from_path_with_handle(tty_path, &settings, &handle).unwrap();

    let task = Client::connect_rtu(port, server_addr, &handle).and_then(|client| {
        println!("Reading a sensor value");
        client
            .read_holding_registers(0x082B, 2)
            .and_then(move |res| {
                println!("Sensor value is: {:?}", res);
                Ok(())
            })
    });

    core.run(task).unwrap();
}

More examples can be found in the examples folder.

Protocol-Specification

License

Copyright 2018 slowtec GmbH

MIT/Apache-2.0