Skip to content

Commit

Permalink
Merge 9a9e479 into 070ccb2
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Dec 2, 2018
2 parents 070ccb2 + 9a9e479 commit 686005f
Show file tree
Hide file tree
Showing 13 changed files with 1,369 additions and 1,266 deletions.
15 changes: 13 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/target/
**/*.rs.bk
# Cargo artefacts
Cargo.lock

# Build directory (or a symbolic link to it)
/target

# Backup files
**/*~
**/*.rs.bk

# macOS
.DS_Store

# vi files
*.swp
63 changes: 40 additions & 23 deletions src/client.rs → src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use crate::frame::*;
use crate::service::*;

use futures::prelude::*;
use crate::service;
use std::io::{Error, ErrorKind, Result};
use std::net::SocketAddr;
use tokio_core::reactor::{Core, Handle};
use std::io::{Error, ErrorKind};
use tokio_core::reactor::Handle;
use tokio_service::Service;

#[cfg(feature = "rtu")]
use std::io::Result;

#[cfg(feature = "rtu")]
use tokio_serial::{Serial, SerialPortSettings};
use tokio_service::Service;

#[cfg(feature = "rtu")]
use tokio_core::reactor::Core;

#[cfg(feature = "tcp")]
use std::net::SocketAddr;

/// A transport independent asynchronous client trait.
pub trait ModbusClient {
Expand All @@ -17,7 +27,8 @@ pub trait ModbusClient {
_: Quantity,
) -> Box<Future<Item = Vec<Coil>, Error = Error>>;
fn write_single_coil(&self, _: Address, _: Coil) -> Box<Future<Item = (), Error = Error>>;
fn write_multiple_coils(&self, _: Address, _: &[Coil]) -> Box<Future<Item = (), Error = Error>>;
fn write_multiple_coils(&self, _: Address, _: &[Coil])
-> Box<Future<Item = (), Error = Error>>;
fn read_input_registers(
&self,
_: Address,
Expand All @@ -29,7 +40,11 @@ pub trait ModbusClient {
_: Quantity,
) -> Box<Future<Item = Vec<Word>, Error = Error>>;
fn write_single_register(&self, _: Address, _: Word) -> Box<Future<Item = (), Error = Error>>;
fn write_multiple_registers(&self, _: Address, _: &[Word]) -> Box<Future<Item = (), Error = Error>>;
fn write_multiple_registers(
&self,
_: Address,
_: &[Word],
) -> Box<Future<Item = (), Error = Error>>;
fn read_write_multiple_registers(
&self,
_: Address,
Expand Down Expand Up @@ -84,7 +99,7 @@ impl Client {
addr: &SocketAddr,
handle: &Handle,
) -> Box<Future<Item = Client, Error = Error>> {
let t = service::tcp::Client::connect(addr, handle).map(|c| Client {
let t = tcp::Client::connect(addr, handle).map(|c| Client {
transport: Box::new(c),
});
Box::new(t)
Expand All @@ -95,7 +110,7 @@ impl Client {
address: u8,
handle: &Handle,
) -> Box<Future<Item = Client, Error = Error>> {
let t = service::rtu::Client::connect(serial, address, handle).map(|c| Client {
let t = rtu::Client::connect(serial, address, handle).map(|c| Client {
transport: Box::new(c),
});
Box::new(t)
Expand Down Expand Up @@ -294,21 +309,23 @@ impl ModbusClient for Client {
write_addr: Address,
write_data: &[Word],
) -> Box<Future<Item = Vec<Word>, Error = Error>> {
Box::new(self.call(Request::ReadWriteMultipleRegisters(
read_addr,
read_cnt,
write_addr,
write_data.to_vec(),
)).and_then(move |res| {
if let Response::ReadWriteMultipleRegisters(res) = res {
if res.len() != read_cnt as usize {
return Err(Error::new(ErrorKind::InvalidData, "invalid response"));
Box::new(
self.call(Request::ReadWriteMultipleRegisters(
read_addr,
read_cnt,
write_addr,
write_data.to_vec(),
)).and_then(move |res| {
if let Response::ReadWriteMultipleRegisters(res) = res {
if res.len() != read_cnt as usize {
return Err(Error::new(ErrorKind::InvalidData, "invalid response"));
}
Ok(res)
} else {
Err(Error::new(ErrorKind::InvalidData, "unexpected response"))
}
Ok(res)
} else {
Err(Error::new(ErrorKind::InvalidData, "unexpected response"))
}
}))
}),
)
}
}

Expand Down

0 comments on commit 686005f

Please sign in to comment.