modbus_rtu/error/
response_packet.rs

1/// Errors that can occur while validating and decoding a Modbus RTU response packet.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum ResponsePacketError {
4    /// The response frame is shorter than the minimum Modbus RTU length.
5    TooShort(usize),
6
7    /// Calculated CRC does not match the CRC bytes present in the frame.
8    CRCMismatch { expected: u16, received: u16 },
9
10    /// The response came from a different Modbus slave than the request targeted.
11    UnexpectedResponder(u8),
12
13    /// The payload failed structural validation (unexpected function code,
14    /// byte count mismatch, etc.).
15    InvalidFormat,
16}
17
18impl core::fmt::Display for ResponsePacketError {
19    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
20        write!(
21            f,
22            "{}",
23            match self {
24                Self::TooShort(len) => format!(
25                    "response packet too short; expected at least 5 bytes but received {len}."
26                ),
27                Self::CRCMismatch { expected, received } => format!(
28                    "response CRC mismatch: expected 0x{expected:04X}, received 0x{received:04X}."
29                ),
30                Self::UnexpectedResponder(id) =>
31                    format!("response came from unexpected Modbus slave id 0x{id:02X}."),
32                Self::InvalidFormat => format!("response payload format is invalid."),
33            }
34        )
35    }
36}
37
38impl core::error::Error for ResponsePacketError {}