Skip to content

Commit

Permalink
add sync tcp client example
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jan 26, 2018
1 parent f153325 commit 8949cce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions examples/tcp-client-sync.rs
Original file line number Diff line number Diff line change
@@ -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);
}
35 changes: 17 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8949cce

Please sign in to comment.