A tokio-based modbus library
Branch: master
Clone or download
Markus Kohlhase
Latest commit f5c6638 Dec 7, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Switch to Rust 2018 Dec 6, 2018
src Switch to Rust 2018 Dec 6, 2018
.gitignore Update .gitignore Dec 1, 2018
.travis.yml travis: only test on stable Jan 21, 2018
CHANGELOG.md Update CHANGELOG for v0.2.3 Dec 3, 2018
Cargo.toml Switch to Rust 2018 Dec 6, 2018
LICENSE-APACHE initial commit Oct 22, 2017
LICENSE-MIT initial commit Oct 22, 2017
README.md update examples in README.md Dec 7, 2018
appveyor.yml add appveyor config Dec 1, 2018
dev-env.nix use stable rust in dev-env.nix Dec 7, 2018

README.md

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