pub struct Adu { /* private fields */ }Expand description
A Modbus RTU frame: a unit address, a PDU, and a trailing CRC.
This is the complete unit of bytes an RTU transmitter puts on the bus and a receiver
pulls off it. from_pdu builds one to send by appending the CRC;
parse reads one received, verifying the CRC so a frame corrupted in
transit never reaches the application. The frame lives in a fixed buffer, so neither
path allocates.
§Examples
use pamoja_modbus::Adu;
let frame = Adu::from_pdu(0x11, &[0x03, 0x00, 0x6B, 0x00, 0x03]).unwrap();
assert_eq!(frame.address(), 0x11);
assert_eq!(frame.function_code(), 0x03);
// A receiver validates the same bytes against the CRC they carry.
let received = Adu::parse(frame.as_bytes()).unwrap();
assert_eq!(received.pdu(), &[0x03, 0x00, 0x6B, 0x00, 0x03]);Implementations§
Source§impl Adu
impl Adu
Sourcepub fn from_pdu(address: u8, pdu: &[u8]) -> Result<Adu, ModbusError>
pub fn from_pdu(address: u8, pdu: &[u8]) -> Result<Adu, ModbusError>
Builds a frame for a unit address and PDU, appending the CRC.
§Arguments
address- the unit (slave) address the frame is for.pdu- the protocol data unit: a function code followed by its data.
§Returns
The frame ready to send.
§Errors
Returns ModbusError::FrameTooLong if pdu is longer than a PDU may be, so the
frame would exceed MAX_LEN bytes.
Sourcepub fn parse(bytes: &[u8]) -> Result<Adu, ModbusError>
pub fn parse(bytes: &[u8]) -> Result<Adu, ModbusError>
Parses a received frame, verifying its CRC.
§Arguments
bytes- the raw frame as it came off the wire, CRC included.
§Returns
The validated frame.
§Errors
Returns ModbusError::FrameTooShort if bytes is shorter than a valid frame,
ModbusError::FrameTooLong if it is longer than MAX_LEN, or
ModbusError::CrcMismatch if the trailing CRC does not match the contents.
Sourcepub fn function_code(&self) -> u8
pub fn function_code(&self) -> u8
Returns the function code, the first byte of the PDU.
§Returns
The function code. An exception response has its high bit set.
Sourcepub fn pdu(&self) -> &[u8]
pub fn pdu(&self) -> &[u8]
Returns the PDU: the frame without its address and CRC.
§Returns
The protocol data unit as a byte slice.