Skip to content

The pdu_len(&self) function returns an error len issue #33

Closed

Description

@ThinkCodeStudio

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)
                    }),
                ),
            };
Image Image

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
        }
    }
}

Activity

  1. flosse commented on Oct 30, 2025

    @flosse
    Member

    Excuse me, is this issue a feature or a bug:

    It looks like it is a bug 🫢

  2. flosse commented on Oct 30, 2025

    @flosse
    Member

    @ThinkCodeStudio would you mind to test #34?

  3. ThinkCodeStudio commented on Oct 31, 2025

    @ThinkCodeStudio
    Author

    OK! It's working properly in my APP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions