Skip to content

Commit 3892dc6

Browse files
committed
Fix Request::pdu_len
1 parent 6955cce commit 3892dc6

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Add `Coils::from_iter`
1515
- Removed `modbus_core::packed_coils_len`, it is now implemented via `CoilQuantity::packed_len`.
1616
- `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`.
17+
- Fixed `Request::pdu_len` for `Request::WriteMultipleRegisters` and `Request::ReadWriteMultipleRegisters`
1718

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

src/frame/mod.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ impl Request<'_> {
387387
| Self::WriteSingleRegister(_, _)
388388
| Self::WriteSingleCoil(_, _) => 5,
389389
Self::WriteMultipleCoils(_, coils) => 6 + coils.packed_len(),
390-
Self::WriteMultipleRegisters(_, words) => 6 + words.data.len(),
391-
Self::ReadWriteMultipleRegisters(_, _, _, words) => 10 + words.data.len(),
390+
Self::WriteMultipleRegisters(_, data) => 6 + data.len() * 2,
391+
Self::ReadWriteMultipleRegisters(_, _, _, data) => 10 + data.len() * 2,
392392
Self::Custom(_, data) => 1 + data.len(),
393393
#[cfg(feature = "rtu")]
394394
_ => todo!(), // TODO
@@ -406,9 +406,9 @@ impl Response<'_> {
406406
Self::WriteMultipleCoils(_, _)
407407
| Self::WriteMultipleRegisters(_, _)
408408
| Self::WriteSingleRegister(_, _) => 5,
409-
Self::ReadInputRegisters(words)
410-
| Self::ReadHoldingRegisters(words)
411-
| Self::ReadWriteMultipleRegisters(words) => 2 + words.len() * 2,
409+
Self::ReadInputRegisters(data)
410+
| Self::ReadHoldingRegisters(data)
411+
| Self::ReadWriteMultipleRegisters(data) => 2 + data.len() * 2,
412412
Self::Custom(_, data) => 1 + data.len(),
413413
#[cfg(feature = "rtu")]
414414
Self::ReadExceptionStatus(_) => 2,
@@ -548,6 +548,25 @@ mod tests {
548548
.pdu_len(),
549549
7
550550
);
551+
let mut big_buffer = [0; 200];
552+
assert_eq!(
553+
Request::WriteMultipleRegisters(
554+
0,
555+
Data::from_words(&[0x23, 0x24], &mut big_buffer).unwrap()
556+
)
557+
.pdu_len(),
558+
10
559+
);
560+
assert_eq!(
561+
Request::ReadWriteMultipleRegisters(
562+
0,
563+
3,
564+
2,
565+
Data::from_words(&[0x23, 0x24], &mut big_buffer).unwrap()
566+
)
567+
.pdu_len(),
568+
14
569+
);
551570
// TODO: extend test
552571
}
553572

0 commit comments

Comments
 (0)