1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! A pure [Rust](https://www.rust-lang.org)
//! [Modbus](https://en.wikipedia.org/wiki/Modbus) library
//! based on [tokio](https://tokio.rs).
//!
//! Modbus is based on a [master/slave](https://en.wikipedia.org/wiki/Master/slave_(technology))
//! 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`:
//!
//! ```toml
//! [dependencies]
//! tokio-modbus = "*"
//! ```
//! If you like to use Modbus TCP only:
//!
//! ```toml
//! [dependencies]
//! tokio-modbus = { version = "*", default-features = false, features = ["tcp"] }
//! ```
//!
//! If you like to use Modbus RTU only:
//!
//! ```toml
//! [dependencies]
//! tokio-modbus = { version = "*", default-features = false, features = ["rtu"] }
//! ```
//!
//! If you like to build a TCP server:
//!
//! ```toml
//! [dependencies]
//! tokio-modbus = { version = "*", default-features = false, features = ["tcp", "server"] }
//! ```
//!
//! # Examples
//!
//! ## TCP client
//!
//! ```rust,no_run
//! #[cfg(feature = "tcp")]
//! #[tokio::main]
//! pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     use std::future::Future;
//!     use tokio::runtime::Runtime;
//!     use tokio_modbus::prelude::*;
//!
//!     let socket_addr = "192.168.0.222:502".parse().unwrap();
//!
//!     let mut ctx = tcp::connect(socket_addr).await?;
//!     let data = ctx.read_input_registers(0x1000, 7).await?;
//!     println!("Response is '{:?}'", data);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Sync TCP client
//!
//! ```rust,no_run
//! #[cfg(all(feature = "tcp", feature = "sync"))]
//! pub fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     use tokio_modbus::prelude::*;
//!
//!     let socket_addr = "192.168.0.222:502".parse()?;
//!     let mut client = client::sync::tcp::connect(socket_addr)?;

//!     let data = client.read_input_registers(0x1000, 7)?;
//!     println!("Response is '{:?}'", data);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## RTU client
//!
//! ```rust,no_run
//! #[cfg(feature = "rtu")]
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     use std::future::Future;
//!     use tokio::runtime::Runtime;
//!     use tokio_serial::{Serial, SerialPortSettings};
//!
//!     use tokio_modbus::prelude::*;
//!
//!     let tty_path = "/dev/ttyUSB0";
//!     let slave = Slave(0x17);
//!
//!     let mut settings = SerialPortSettings::default();
//!     settings.baud_rate = 19200;
//!     let port = Serial::from_path(tty_path, &settings).unwrap();
//!
//!     let mut ctx = rtu::connect_slave(port, slave).await?;
//!     println!("Reading a sensor value");
//!     let rsp = ctx.read_holding_registers(0x082B, 2).await?;
//!     println!("Sensor value is: {:?}", rsp);
//!
//!     Ok(())
//! }
//! ```
//!
//! More examples can be found in the [examples](https://github.com/slowtec/tokio-modbus/tree/master/examples) folder.
//!
//! # Protocol-Specification
//!
//! - [MODBUS Application Protocol Specification v1.1b3 (PDF)](http://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf)
//! - [MODBUS over serial line specification and implementation guide v1.02 (PDF)](http://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf)
//! - [MODBUS Messaging on TCP/IP Implementation Guide v1.0b (PDF)](http://modbus.org/docs/Modbus_Messaging_Implementation_Guide_V1_0b.pdf)

pub mod prelude;

pub mod client;
pub mod slave;

#[cfg(feature = "server")]
pub mod server;

mod codec;
mod frame;
mod service;