Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application protocol function codes: PDU length #12

Open
Tipgeber opened this issue Oct 6, 2024 · 2 comments
Open

Application protocol function codes: PDU length #12

Tipgeber opened this issue Oct 6, 2024 · 2 comments

Comments

@Tipgeber
Copy link

Tipgeber commented Oct 6, 2024

I believe there is an issue in src/codec/rtu/mod.rs in fn request_pdu_len.
You may want to check the Modbus application protocol spec, sections 6.11 and 6.12, to make sure.
IMHO, the function should look like this:

pub const fn request_pdu_len(adu_buf: &[u8]) -> Result<Option<usize>> {
    if adu_buf.len() < 2 {
        return Ok(None);
    }
    let fn_code = adu_buf[1];
    let len = match fn_code {
        0x01..=0x06 => Some(5),
        0x07 | 0x0B | 0x0C | 0x11 => Some(1),
        0x0F => {
            if adu_buf.len() > 6
                && u16::from_be_bytes([adu_buf[4], adu_buf[5]]) == adu_buf[6] as u16
            {
                Some(6 + (adu_buf[6] as usize))
            } else {
                // incomplete frame
                None
            }
        }
        0x10 => {
            if adu_buf.len() > 6
                && u16::from_be_bytes([adu_buf[4], adu_buf[5]]).saturating_mul(2)
                    == adu_buf[6] as u16
            {
                Some(6 + (adu_buf[6] as usize))
            } else {
                // incomplete frame
                None
            }
        }
        0x16 => Some(7),
        0x18 => Some(3),
        0x17 => {
            if adu_buf.len() > 10 {
                Some(10 + adu_buf[10] as usize)
            } else {
                // incomplete frame
                None
            }
        }
        _ => {
            return Err(Error::FnCode(fn_code));
        }
    };
    Ok(len)
}

Specifically, for function codes 0x0F and 0x10, adu_buf[6], not adu_buf[4], should be used to determine the PDU length.
This change fixed an issue in my application. I'd appreciate it if you integrate it in your crate.

@flosse
Copy link
Member

flosse commented Oct 7, 2024

@Tipgeber thanks for reporting!

Would you mind to open a PR?

@domenicquirl
Copy link

Shouldn't the check for 0x0F (Write Multiple Coils) be divided by 8 (plus or minus 1), to reflect how the following values are bitpacked to 1 byte per 8 coils (with the 9th coil bumping the data part up to 2 bytes)?

So something like

            let quantity = u16::from_be_bytes([adu_buf[4], adu_buf[5]]);
            let expected_count = if quantity % 8 == 0 {
                quantity.saturating_div(8)
            } else {
                quantity.saturating_div(8) + 1
            };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants