diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f65807..f0e9108 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Add `Coils::from_iter` - Removed `modbus_core::packed_coils_len`, it is now implemented via `CoilQuantity::packed_len`. - `modbus_core::pack_coils` now takes an iterator over coils instead of a slice of coils, and it returns a `CoilQuantity` instead of a `usize`. +- Fixed `Request::pdu_len` for `Request::WriteMultipleRegisters` and `Request::ReadWriteMultipleRegisters` ## v0.2.0 (2025-09-30) diff --git a/src/frame/mod.rs b/src/frame/mod.rs index 4158a23..c99c986 100644 --- a/src/frame/mod.rs +++ b/src/frame/mod.rs @@ -387,8 +387,8 @@ impl Request<'_> { | Self::WriteSingleRegister(_, _) | Self::WriteSingleCoil(_, _) => 5, Self::WriteMultipleCoils(_, coils) => 6 + coils.packed_len(), - Self::WriteMultipleRegisters(_, words) => 6 + words.data.len(), - Self::ReadWriteMultipleRegisters(_, _, _, words) => 10 + words.data.len(), + Self::WriteMultipleRegisters(_, data) => 6 + data.len() * 2, + Self::ReadWriteMultipleRegisters(_, _, _, data) => 10 + data.len() * 2, Self::Custom(_, data) => 1 + data.len(), #[cfg(feature = "rtu")] _ => todo!(), // TODO @@ -406,9 +406,9 @@ impl Response<'_> { Self::WriteMultipleCoils(_, _) | Self::WriteMultipleRegisters(_, _) | Self::WriteSingleRegister(_, _) => 5, - Self::ReadInputRegisters(words) - | Self::ReadHoldingRegisters(words) - | Self::ReadWriteMultipleRegisters(words) => 2 + words.len() * 2, + Self::ReadInputRegisters(data) + | Self::ReadHoldingRegisters(data) + | Self::ReadWriteMultipleRegisters(data) => 2 + data.len() * 2, Self::Custom(_, data) => 1 + data.len(), #[cfg(feature = "rtu")] Self::ReadExceptionStatus(_) => 2, @@ -548,6 +548,25 @@ mod tests { .pdu_len(), 7 ); + let mut big_buffer = [0; 200]; + assert_eq!( + Request::WriteMultipleRegisters( + 0, + Data::from_words(&[0x23, 0x24], &mut big_buffer).unwrap() + ) + .pdu_len(), + 10 + ); + assert_eq!( + Request::ReadWriteMultipleRegisters( + 0, + 3, + 2, + Data::from_words(&[0x23, 0x24], &mut big_buffer).unwrap() + ) + .pdu_len(), + 14 + ); // TODO: extend test }