diff --git a/README.md b/README.md index 9616cd4f..13a9ac65 100644 --- a/README.md +++ b/README.md @@ -62,16 +62,29 @@ pub fn main() { let task = TcpClient::connect(&addr, &handle).and_then(|client| { client .read_input_registers(0x1000, 7) - .and_then(move |buff| { - println!("Response is '{:?}'", buff); + .and_then(move |data| { + println!("Response is '{:?}'", data); Ok(()) }) }); - core.run(task).unwrap(); } ``` +### Sync TCP client + +```rust +extern crate tokio_modbus; +use tokio_modbus::*; + +pub fn main() { + let addr = "192.168.0.222:502".parse().unwrap(); + let client = SyncClient::connect_tcp(&addr).unwrap(); + let buff = client.read_input_registers(0x1000, 7).unwrap(); + println!("Response is '{:?}'", buff); +} +``` + ### RTU client ```rust @@ -110,21 +123,6 @@ pub fn main() { } ``` -### Sync TCP client - -```rust -extern crate tokio_modbus; - -use tokio_modbus::*; - -pub fn main() { - let addr = "192.168.0.222:502".parse().unwrap(); - let client = SyncClient::connect_tcp(&addr).unwrap(); - let buff = client.read_input_registers(0x1000, 7).unwrap(); - println!("Response is '{:?}'", buff); -} -``` - More examples can be found in the [examples](https://github.com/slowtec/tokio-modbus/tree/master/examples) folder. ## Protocol-Specification diff --git a/examples/tcp-client-sync.rs b/examples/tcp-client-sync.rs new file mode 100644 index 00000000..ec834f71 --- /dev/null +++ b/examples/tcp-client-sync.rs @@ -0,0 +1,16 @@ +extern crate tokio_modbus; +use tokio_modbus::*; + +#[cfg(all(feature = "tcp", feature = "sync"))] +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); +} + +#[cfg(not(all(feature = "tcp", feature = "sync")))] +pub fn main() { + println!("features `tcp` and `sync` are required to run this example"); + ::std::process::exit(1); +} diff --git a/src/lib.rs b/src/lib.rs index 733b4eb9..384197e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,19 +59,32 @@ //! let task = Client::connect_tcp(&addr, &handle).and_then(|client| { //! client //! .read_input_registers(0x1000, 7) -//! .and_then(move |buff| { -//! println!("Response is '{:?}'", buff); +//! .and_then(move |data| { +//! println!("Response is '{:?}'", data); //! Ok(()) //! }) //! }); -//! //! core.run(task).unwrap(); //! } //! ``` //! +//! ## Sync TCP client +//! +//! ```rust,no_run +//! extern crate tokio_modbus; +//! use tokio_modbus::*; +//! +//! pub fn main() { +//! let addr = "192.168.0.222:502".parse().unwrap(); +//! let mut client = SyncClient::connect_tcp(&addr).unwrap(); +//! let data = client.read_input_registers(0x1000, 7).unwrap(); +//! println!("Response is '{:?}'", data); +//! } +//! ``` +//! //! ## RTU client //! -//! ```rust +//! ```rust,no_run //! extern crate futures; //! extern crate tokio_core; //! extern crate tokio_modbus; @@ -107,20 +120,6 @@ //! } //! ``` //! -//! ## Sync TCP client -//! -//! ```rust -//! extern crate tokio_modbus; -//! use tokio_modbus::*; -//! -//! pub fn main() { -//! let addr = "192.168.0.222:502".parse().unwrap(); -//! let client = SyncClient::connect_tcp(&addr).unwrap(); -//! let buff = client.read_input_registers(0x1000, 7).unwrap(); -//! println!("Response is '{:?}'", buff); -//! } -//! ``` -//! //! More examples can be found in the [examples](https://github.com/slowtec/tokio-modbus/tree/master/examples) folder. //! //! # Protocol-Specification