Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- Merged `modbus_core::rtu::FrameLocation` and `modbus_core::tcp::FrameLocation` and moved it to `modbus_core::FrameLocation`
- Return a `FrameLocation` in addition to the parsed frame in `rtu::server::decode_response`,
`rtu::client::decode_response`, `tcp::server::decode_response` and `tcp::client::decode_request`
- Added `FrameLocation::end` helper.
- Added `FrameLocation::end` helper
- Fix `WriteSingleCoil` responses not including the output value

## v0.2.0 (2025-09-30)

Expand Down
39 changes: 30 additions & 9 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ impl<'r> TryFrom<&'r [u8]> for Response<'r> {
_ => unreachable!(),
}
}
F::WriteSingleCoil => Self::WriteSingleCoil(BigEndian::read_u16(&bytes[1..])),
F::WriteSingleCoil => Self::WriteSingleCoil(
BigEndian::read_u16(&bytes[1..3]),
u16_coil_to_bool(BigEndian::read_u16(&bytes[3..5]))?,
),

F::WriteMultipleCoils | F::WriteSingleRegister | F::WriteMultipleRegisters => {
let addr = BigEndian::read_u16(&bytes[1..]);
Expand Down Expand Up @@ -304,8 +307,9 @@ impl Encode for Response<'_> {
buf[1] = (registers.len() * 2) as u8;
registers.copy_to(&mut buf[2..]);
}
Self::WriteSingleCoil(address) => {
Self::WriteSingleCoil(address, value) => {
BigEndian::write_u16(&mut buf[1..], *address);
BigEndian::write_u16(&mut buf[3..], bool_to_u16_coil(*value));
}
Self::WriteMultipleCoils(address, payload)
| Self::WriteMultipleRegisters(address, payload)
Expand Down Expand Up @@ -385,8 +389,10 @@ const fn min_response_pdu_len(fn_code: FunctionCode) -> usize {
| F::ReadInputRegisters
| F::ReadHoldingRegisters
| F::ReadWriteMultipleRegisters => 2,
F::WriteSingleCoil => 3,
F::WriteMultipleCoils | F::WriteSingleRegister | F::WriteMultipleRegisters => 5,
F::WriteSingleCoil
| F::WriteMultipleCoils
| F::WriteSingleRegister
| F::WriteMultipleRegisters => 5,
_ => 1,
}
}
Expand Down Expand Up @@ -444,7 +450,7 @@ mod tests {
assert_eq!(min_response_pdu_len(ReadCoils), 2);
assert_eq!(min_response_pdu_len(ReadDiscreteInputs), 2);
assert_eq!(min_response_pdu_len(ReadInputRegisters), 2);
assert_eq!(min_response_pdu_len(WriteSingleCoil), 3);
assert_eq!(min_response_pdu_len(WriteSingleCoil), 5);
assert_eq!(min_response_pdu_len(ReadHoldingRegisters), 2);
assert_eq!(min_response_pdu_len(WriteSingleRegister), 5);
assert_eq!(min_response_pdu_len(WriteMultipleCoils), 5);
Expand Down Expand Up @@ -806,12 +812,23 @@ mod tests {

#[test]
fn write_single_coil() {
let res = Response::WriteSingleCoil(0x33);
let bytes = &mut [0, 0, 0];
let res = Response::WriteSingleCoil(0x33, true);
let bytes = &mut [0, 0, 0, 0, 0];
res.encode(bytes).unwrap();
assert_eq!(bytes[0], 5);
assert_eq!(bytes[1], 0x00);
assert_eq!(bytes[2], 0x33);
assert_eq!(bytes[3], 0xFF);
assert_eq!(bytes[4], 0x00);

let res = Response::WriteSingleCoil(0x33, false);
let bytes = &mut [0, 0, 0, 0, 0];
res.encode(bytes).unwrap();
assert_eq!(bytes[0], 5);
assert_eq!(bytes[1], 0x00);
assert_eq!(bytes[2], 0x33);
assert_eq!(bytes[3], 0x00);
assert_eq!(bytes[4], 0x00);
}

#[test]
Expand Down Expand Up @@ -959,9 +976,13 @@ mod tests {

#[test]
fn write_single_coil() {
let bytes: &[u8] = &[5, 0x00, 0x33];
let bytes: &[u8] = &[5, 0x00, 0x33, 0xFF, 0x00];
let rsp = Response::try_from(bytes).unwrap();
assert_eq!(rsp, Response::WriteSingleCoil(0x33, true));

let bytes: &[u8] = &[5, 0x00, 0x33, 0x00, 0x00];
let rsp = Response::try_from(bytes).unwrap();
assert_eq!(rsp, Response::WriteSingleCoil(0x33));
assert_eq!(rsp, Response::WriteSingleCoil(0x33, false));

let broken_bytes: &[u8] = &[5, 0x00];
assert!(Response::try_from(broken_bytes).is_err());
Expand Down
8 changes: 4 additions & 4 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ type MessageCount = u16;
pub enum Response<'r> {
ReadCoils(Coils<'r>),
ReadDiscreteInputs(Coils<'r>),
WriteSingleCoil(Address),
WriteSingleCoil(Address, Coil),
WriteMultipleCoils(Address, Quantity),
ReadInputRegisters(Data<'r>),
ReadHoldingRegisters(Data<'r>),
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<'r> From<Response<'r>> for FunctionCode {
match r {
R::ReadCoils(_) => Self::ReadCoils,
R::ReadDiscreteInputs(_) => Self::ReadDiscreteInputs,
R::WriteSingleCoil(_) => Self::WriteSingleCoil,
R::WriteSingleCoil(_, _) => Self::WriteSingleCoil,
R::WriteMultipleCoils(_, _) => Self::WriteMultipleCoils,
R::ReadInputRegisters(_) => Self::ReadInputRegisters,
R::ReadHoldingRegisters(_) => Self::ReadHoldingRegisters,
Expand Down Expand Up @@ -402,7 +402,7 @@ impl Response<'_> {
pub const fn pdu_len(&self) -> usize {
match *self {
Self::ReadCoils(coils) | Self::ReadDiscreteInputs(coils) => 2 + coils.packed_len(),
Self::WriteSingleCoil(_) => 3,
Self::WriteSingleCoil(_, _) => 5,
Self::WriteMultipleCoils(_, _)
| Self::WriteMultipleRegisters(_, _)
| Self::WriteSingleRegister(_, _) => 5,
Expand Down Expand Up @@ -505,7 +505,7 @@ mod tests {
}),
2,
),
(WriteSingleCoil(0x0), 5),
(WriteSingleCoil(0x0, false), 5),
(WriteMultipleCoils(0x0, 0x0), 0x0F),
(
ReadInputRegisters(Data {
Expand Down