Skip to main content

Adu

Struct Adu 

Source
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

Source

pub const MAX_LEN: usize = 256

The largest a Modbus RTU frame may be, in bytes.

Source

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.

Source

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.

Source

pub fn address(&self) -> u8

Returns the unit address, the first byte of the frame.

§Returns

The unit (slave) address.

Source

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.

Source

pub fn pdu(&self) -> &[u8]

Returns the PDU: the frame without its address and CRC.

§Returns

The protocol data unit as a byte slice.

Source

pub fn as_bytes(&self) -> &[u8]

Returns the whole frame, CRC included, ready for the wire.

§Returns

The frame as a byte slice.

Source

pub fn exception(&self) -> Option<Exception>

Returns the exception a device reported, if this frame is an exception response.

§Returns

The Exception if the function code’s high bit is set and an exception byte follows it, otherwise None (including for a defined-but-unknown exception code).

Source

pub fn response(&self) -> Response<'_>

Returns a reader over this frame’s PDU for decoding a response.

§Returns

A Response borrowing the PDU.

Trait Implementations§

Source§

impl Clone for Adu

Source§

fn clone(&self) -> Adu

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Adu

Source§

impl Debug for Adu

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Adu

Source§

impl PartialEq for Adu

Source§

fn eq(&self, other: &Adu) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Adu

Auto Trait Implementations§

§

impl Freeze for Adu

§

impl RefUnwindSafe for Adu

§

impl Send for Adu

§

impl Sync for Adu

§

impl Unpin for Adu

§

impl UnsafeUnpin for Adu

§

impl UnwindSafe for Adu

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.