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, Function, Pdu};
// A read of three holding registers, addressed to unit 17.
let pdu = Pdu::read_holding_registers(107, 3);
let frame = Adu::from_pdu(17, pdu.as_bytes()).unwrap();
assert_eq!(frame.address(), 17);
assert_eq!(frame.function_code(), Function::ReadHoldingRegisters.code());
// A receiver checks the same bytes against the CRC they carry, and finds the PDU.
let received = Adu::parse(frame.as_bytes()).unwrap();
assert_eq!(received.pdu(), pdu.as_bytes());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.