Skip to content

Commit

Permalink
Merge branch 'v0.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Dec 3, 2018
2 parents b304e28 + ee35f65 commit a9dcb8a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 47 deletions.
31 changes: 17 additions & 14 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,24 @@ 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
32 changes: 18 additions & 14 deletions src/codec/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ impl From<Request> for Bytes {
data.put_u16_be(w);
}
}
Custom(_, custom_data) => for d in custom_data {
data.put_u8(d);
},
Custom(_, custom_data) => {
for d in custom_data {
data.put_u8(d);
}
}
}
data.freeze()
}
Expand Down Expand Up @@ -102,9 +104,11 @@ impl From<Response> for Bytes {
data.put_u16_be(address);
data.put_u16_be(word);
}
Custom(_, custom_data) => for d in custom_data {
data.put_u8(d);
},
Custom(_, custom_data) => {
for d in custom_data {
data.put_u8(d);
}
}
}
data.freeze()
}
Expand Down Expand Up @@ -491,7 +495,8 @@ mod tests {
let bytes: Bytes = ExceptionResponse {
function: 0x03,
exception: Exception::IllegalDataAddress,
}.into();
}
.into();
assert_eq!(bytes[0], 0x83);
assert_eq!(bytes[1], 0x02);
}
Expand All @@ -518,7 +523,8 @@ mod tests {
let ex_pdu: Bytes = Pdu::Result(Err(ExceptionResponse {
function: 0x03,
exception: Exception::ServerDeviceFailure,
})).into();
}))
.into();

assert_eq!(req_pdu[0], 0x01);
assert_eq!(req_pdu[1], 0x00);
Expand Down Expand Up @@ -791,12 +797,10 @@ mod tests {

#[test]
fn read_write_multiple_registers() {
assert!(
Request::try_from(Bytes::from(vec![
0x17, 0x00, 0x05, 0x00, 0x33, 0x00, 0x03, 0x00, 0x02, 0x05, 0xAB, 0xCD, 0xEF,
0x12,
])).is_err()
);
assert!(Request::try_from(Bytes::from(vec![
0x17, 0x00, 0x05, 0x00, 0x33, 0x00, 0x03, 0x00, 0x02, 0x05, 0xAB, 0xCD, 0xEF, 0x12,
]))
.is_err());
let bytes = Bytes::from(vec![
0x17, 0x00, 0x05, 0x00, 0x33, 0x00, 0x03, 0x00, 0x02, 0x04, 0xAB, 0xCD, 0xEF, 0x12,
]);
Expand Down
12 changes: 6 additions & 6 deletions src/codec/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ mod tests {
0x01, // device address
0x03, // function code
0x04, // byte count
0x89,
0x02,
0x42,
0xC7,
0x89, //
0x02, //
0x42, //
0xC7, //
0x00, // crc
0x9D, // crc
0x00,
Expand All @@ -368,9 +368,9 @@ mod tests {
fn decode_exception_message() {
let mut codec = Codec::client();
let mut buf = BytesMut::from(vec![
0x66,
0x66, //
0x82, // exception = 0x80 + 0x02
0x03,
0x03, //
0xB1, // crc
0x7E, // crc
]);
Expand Down
24 changes: 12 additions & 12 deletions src/codec/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {
0x00, // length high HI
0x03, // length low LO
0x66, // unit id
0x02,
0x02, //
]);
let res = codec.decode(&mut buf).unwrap();
assert!(res.is_none());
Expand All @@ -157,16 +157,16 @@ mod tests {
fn decode_exception_message() {
let mut codec = Codec::client();
let mut buf = BytesMut::from(vec![
0x00,
0x00,
0x00,
0x00,
0x00,
0x03,
0x66,
0x00, //
0x00, //
0x00, //
0x00, //
0x00, //
0x03, //
0x66, //
0x82, // exception = 0x80 + 0x02
0x03,
0x00,
0x03, //
0x00, //
]);

let TcpAdu { header, pdu } = codec.decode(&mut buf).unwrap().unwrap();
Expand All @@ -185,8 +185,8 @@ mod tests {
fn decode_with_invalid_protocol_id() {
let mut codec = Codec::client();
let mut buf = BytesMut::from(vec![
0x00,
0x00,
0x00, //
0x00, //
0x33, // protocol id HI
0x12, // protocol id LO
0x00, // length HI
Expand Down
3 changes: 2 additions & 1 deletion src/service/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl Service for Client {

let pdu = Pdu::Request(req);

let result = self.service
let result = self
.service
.call(TcpAdu { header, pdu })
.and_then(move |adu| {
if adu.header.transaction_id != t_id {
Expand Down

0 comments on commit a9dcb8a

Please sign in to comment.