Skip to content

Commit

Permalink
add sync rtu-client
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jan 26, 2018
1 parent 8949cce commit 2d060df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/rtu-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ extern crate tokio_modbus;
#[cfg(feature = "rtu")]
extern crate tokio_serial;
extern crate tokio_service;
use tokio_modbus::*;

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

Expand Down
10 changes: 8 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use tokio_service::Service;
use service;
use std::net::SocketAddr;
use tokio_core::reactor::{Core, Handle};
use tokio_serial::Serial;
#[cfg(feature = "rtu")]
use tokio_serial::{Serial,SerialPortSettings};

/// A transport independent asynchronous client trait.
pub trait ModbusClient {
Expand Down Expand Up @@ -78,6 +79,7 @@ pub struct SyncClient {
}

impl Client {
#[cfg(feature = "tcp")]
pub fn connect_tcp(
addr: &SocketAddr,
handle: &Handle,
Expand All @@ -87,6 +89,7 @@ impl Client {
});
Box::new(t)
}
#[cfg(feature = "rtu")]
pub fn connect_rtu(
serial: Serial,
address: u8,
Expand All @@ -105,15 +108,18 @@ impl Client {

#[cfg(feature = "sync")]
impl SyncClient {
#[cfg(feature = "tcp")]
pub fn connect_tcp(addr: &SocketAddr) -> Result<SyncClient> {
let mut core = Core::new()?;
let handle = core.handle();
let client = core.run(Client::connect_tcp(addr, &handle))?;
Ok(SyncClient { client, core })
}
pub fn connect_rtu(serial: Serial, address: u8) -> Result<SyncClient> {
#[cfg(feature = "rtu")]
pub fn connect_rtu(tty_path: &str, settings: &SerialPortSettings, address: u8) -> Result<SyncClient> {
let mut core = Core::new()?;
let handle = core.handle();
let serial = Serial::from_path(tty_path, settings, &handle)?;
let client = core.run(Client::connect_rtu(serial, address, &handle))?;
Ok(SyncClient { client, core })
}
Expand Down

0 comments on commit 2d060df

Please sign in to comment.