[−][src]Crate tokio_modbus
A pure Rust Modbus library based on tokio.
Modbus is based on a master/slave model. To avoid confusions with the tokio terminology the master is called client and the slave is called server in this library.
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; use tokio_modbus::prelude::*; pub fn main() { let mut core = Core::new().unwrap(); let handle = core.handle(); let socket_addr = "192.168.0.222:502".parse().unwrap(); let task = tcp::connect(&handle, socket_addr).and_then(|ctx| { ctx .read_input_registers(0x1000, 7) .and_then(move |data| { println!("Response is '{:?}'", data); Ok(()) }) }); core.run(task).unwrap(); }
Sync TCP client
use tokio_modbus::prelude::*; pub fn main() { let socket_addr = "192.168.0.222:502".parse().unwrap(); let mut client = client::sync::tcp::connect(socket_addr).unwrap(); let data = client.read_input_registers(0x1000, 7).unwrap(); println!("Response is '{:?}'", data); }
RTU client
use tokio_core::reactor::Core; use futures::Future; use tokio_serial::{Serial, SerialPortSettings}; use tokio_modbus::prelude::*; pub fn main() { let mut core = Core::new().unwrap(); let handle = core.handle(); let tty_path = "/dev/ttyUSB0"; let slave = Slave(0x17); let mut settings = SerialPortSettings::default(); settings.baud_rate = 19200; let port = Serial::from_path_with_handle(tty_path, &settings, &handle.new_tokio_handle()).unwrap(); let task = rtu::connect_slave(&handle, port, slave).and_then(|ctx| { println!("Reading a sensor value"); ctx .read_holding_registers(0x082B, 2) .and_then(move |rsp| { println!("Sensor value is: {:?}", rsp); Ok(()) }) }); core.run(task).unwrap(); }
More examples can be found in the examples folder.
Protocol-Specification
Modules
client | |
prelude | |
server | |
slave |