Skip to content

Commit

Permalink
support 'tcp' and 'rtu' features
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jan 21, 2018
1 parent fe59e25 commit a7961a0
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ tokio-service = "0.1"
tokio-serial = { version = "*", optional = true }

[features]
default = ["rtu"]
default = ["tcp", "rtu"]
rtu = ["tokio-serial"]
tcp = []

[badges]
travis-ci = { repository = "slowtec/tokio-modbus" }
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ Add this to your `Cargo.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"] }
```

## TCP client example

Expand Down
18 changes: 13 additions & 5 deletions examples/rtu-client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
extern crate futures;
extern crate tokio_core;
extern crate tokio_modbus;
#[cfg(feature = "rtu")]
extern crate tokio_serial;
extern crate tokio_service;

use tokio_core::reactor::Core;
use tokio_serial::{BaudRate, Serial, SerialPortSettings};
use tokio_modbus::{Client, RtuClient};
use futures::future::Future;

#[cfg(feature = "rtu")]
pub fn main() {
use tokio_core::reactor::Core;
use tokio_modbus::{Client, RtuClient};
use futures::future::Future;
use tokio_serial::{BaudRate, Serial, SerialPortSettings};

let mut core = Core::new().unwrap();
let handle = core.handle();
let tty_path = "/dev/ttyUSB0";
Expand All @@ -33,3 +35,9 @@ pub fn main() {

core.run(task).unwrap();
}

#[cfg(not(feature = "rtu"))]
pub fn main() {
println!("feature `rtu` is required to run this example");
::std::process::exit(1);
}
17 changes: 12 additions & 5 deletions examples/tcp-client-custom-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ extern crate tokio_core;
extern crate tokio_modbus;
extern crate tokio_service;

use tokio_core::reactor::Core;
use futures::future::Future;
use tokio_service::Service;
use tokio_modbus::{Request, Response, TcpClient};

#[cfg(feature = "tcp")]
pub fn main() {
use tokio_core::reactor::Core;
use futures::future::Future;
use tokio_service::Service;
use tokio_modbus::{Request, Response, TcpClient};

let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = "192.168.0.222:502".parse().unwrap();
Expand All @@ -32,3 +33,9 @@ pub fn main() {

core.run(task).unwrap();
}

#[cfg(not(feature = "tcp"))]
pub fn main() {
println!("feature `tcp` is required to run this example");
::std::process::exit(1);
}
15 changes: 11 additions & 4 deletions examples/tcp-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ extern crate futures;
extern crate tokio_core;
extern crate tokio_modbus;

use tokio_core::reactor::Core;
use futures::future::Future;
use tokio_modbus::{Client, TcpClient};

#[cfg(feature = "tcp")]
pub fn main() {
use tokio_core::reactor::Core;
use futures::future::Future;
use tokio_modbus::{Client, TcpClient};

let mut core = Core::new().unwrap();
let handle = core.handle();
let addr = "192.168.0.222:502".parse().unwrap();
Expand All @@ -29,3 +30,9 @@ pub fn main() {

core.run(task).unwrap();
}

#[cfg(not(feature = "tcp"))]
pub fn main() {
println!("feature `tcp` is required to run this example");
::std::process::exit(1);
}
2 changes: 2 additions & 0 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod common;
#[cfg(feature = "tcp")]
pub mod tcp;
#[cfg(feature = "rtu")]
pub mod rtu;
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
//! [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"] }
//! ```
//!
//! # TCP client example
//!
//! ```rust,no_run
Expand Down Expand Up @@ -77,5 +91,7 @@ mod client;

pub use frame::*;
pub use client::Client;
#[cfg(feature = "tcp")]
pub use service::tcp::Client as TcpClient;
#[cfg(feature = "rtu")]
pub use service::rtu::Client as RtuClient;
2 changes: 2 additions & 0 deletions src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "tcp")]
pub mod tcp {

use frame::TcpAdu;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub mod tcp {
}
}

#[cfg(feature = "rtu")]
pub mod rtu {

use frame::RtuAdu;
Expand Down
2 changes: 2 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "tcp")]
pub mod tcp {

use frame::*;
Expand Down Expand Up @@ -72,6 +73,7 @@ pub mod tcp {
}
}

#[cfg(feature = "rtu")]
pub mod rtu {

use frame::*;
Expand Down

0 comments on commit a7961a0

Please sign in to comment.