pub enum Response {
Status(Box<[bool]>),
Value(Box<[u16]>),
Success,
Exception(Exception),
}Expand description
Represents the outcome of a Modbus RTU request, covering data reads, write acknowledgements, and protocol exceptions.
Variants§
Status(Box<[bool]>)
A collection of coil/discrete input states returned by the device.
Value(Box<[u16]>)
A collection of register values returned by the device.
Success
Confirmation that a write request completed successfully.
Exception(Exception)
A Modbus application exception reported by the device.
Implementations§
Source§impl Response
impl Response
Sourcepub fn from_bytes(
request: &Request<'_>,
bytes: &[u8],
) -> Result<Self, ResponsePacketError>
pub fn from_bytes( request: &Request<'_>, bytes: &[u8], ) -> Result<Self, ResponsePacketError>
Decodes a Modbus RTU response frame into a Response value.
§Arguments
request: The originating request used to validate address, function, and quantity semantics.bytes: Raw response frame including slave id, function code, payload, and CRC.
§Errors
Returns ResponsePacketError when
the frame does not pass validation (CRC mismatch, unexpected responder,
malformed payload, etc.).
§Examples
use modbus_rtu::{Function, Request, Response};
let function = Function::ReadInputRegisters { starting_address: 0x0000, quantity: 2 };
let request = Request::new(0x01, &function, std::time::Duration::from_millis(100));
let frame = [0x01, 0x04, 0x04, 0x00, 0x10, 0x00, 0x20, 0xFB, 0x99];
let response = Response::from_bytes(&request, &frame).unwrap();
match response {
Response::Value(values) => assert_eq!(&values[..], &[0x0010, 0x0020]),
_ => panic!("unexpected response variant"),
}Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Returns true when the response indicates that the request succeeded.
The method treats the Modbus Acknowledge (0x05) exception as success
because it signals that the device accepted the request and will complete
it asynchronously.
§Examples
use modbus_rtu::{Exception, Response};
assert!(Response::Success.is_success());
assert!(Response::Exception(Exception::Acknowledge).is_success());
assert!(!Response::Exception(Exception::IllegalFunction).is_success());