Giter Site home page Giter Site logo

tokio-modbus's Introduction

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

tokio-modbus's People

Contributors

flosse avatar uklotzde avatar zzeroo avatar

Watchers

James Cloos avatar Jack avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.