Repository navigation
The pdu_len(&self) function returns an error len issue #33
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Sorry, English is not my main language. The following content will be translated and modified by AI
Excuse me, is this issue a feature or a bug:
I passed in a 200-byte buff, but the function returned the len of the buff
let mut ret = Vec::new();
let (event, pack) = self.extract_pack();
+ let mut wirte_buff: [u8; 200] = [0; 200];
let handler_ret = match event {
CmdTableWidgetEvent::Read => self.modbus_encoder(
modbus_client,
modbus_addr,
RequestPdu(modbus_core::Request::ReadHoldingRegisters(
pack[0].addr,
pack.len() as u16,
)),
),
CmdTableWidgetEvent::Write => self.modbus_encoder(
modbus_client,
modbus_addr,
RequestPdu(if pack.len() > 1 {
let addr = pack[0].addr;
let words: Vec<u16> = pack.into_iter().map(|it| it.value).collect();
// TODO: unwrap() Risk
modbus_core::Request::WriteMultipleRegisters(
addr,
+ Data::from_words(words.as_slice(), &mut wirte_buff).unwrap(),
)
} else {
modbus_core::Request::WriteSingleRegister(pack[0].addr, pack[0].value)
}),
),
};
The problem lies here
# src/frame/mod.rs : 378
impl Request<'_> {
/// Number of bytes required for a serialized PDU frame.
#[must_use]
pub const fn pdu_len(&self) -> usize {
match *self {
Self::ReadCoils(_, _)
| Self::ReadDiscreteInputs(_, _)
| Self::ReadInputRegisters(_, _)
| Self::ReadHoldingRegisters(_, _)
| Self::WriteSingleRegister(_, _)
| Self::WriteSingleCoil(_, _) => 5,
Self::WriteMultipleCoils(_, coils) => 6 + coils.packed_len(),
- Self::WriteMultipleRegisters(_, words) => 6 + words.data.len(),
+ Self::WriteMultipleRegisters(_, words) => 6 + words.len(),
- Self::ReadWriteMultipleRegisters(_, _, _, words) => 10 + words.data.len(),
+ Self::ReadWriteMultipleRegisters(_, _, _, words) => 10 + words.len(),
Self::Custom(_, data) => 1 + data.len(),
#[cfg(feature = "rtu")]
_ => todo!(), // TODO
}
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
flosse commentedon Oct 30, 2025
It looks like it is a bug 🫢
flosse commentedon Oct 30, 2025
@ThinkCodeStudio would you mind to test #34?
ThinkCodeStudio commentedon Oct 31, 2025
OK! It's working properly in my APP.