Skip to main content

ModbusMessage

Struct ModbusMessage 

Source
pub struct ModbusMessage {
    pub additional_address: AdditionalAddress,
    pub pdu: Pdu,
}
Expand description

Represents a complete Modbus message, including the additional address, PDU and Error check.

Fields§

§additional_address: AdditionalAddress

The MBAP header for Modbus TCP messages.

§pdu: Pdu

The Protocol Data Unit (PDU) containing the function code and data.

Implementations§

Source§

impl ModbusMessage

Source

pub fn new(additional_address: AdditionalAddress, pdu: Pdu) -> Self

Creates a new ModbusMessage instance.

§Arguments
  • additional_address - The additional address information (MBAP header or slave address).
  • pdu - The Protocol Data Unit containing the function code and data.
§Returns

A new ModbusMessage instance.

Source

pub fn additional_address(&self) -> &AdditionalAddress

Accessor for the additional address.

Source

pub fn pdu(&self) -> &Pdu

Accessor for the Protocol Data Unit (PDU).

The PDU contains the function code and the data payload, which are independent of the underlying transport layer (TCP, RTU, or ASCII).

Source

pub fn unit_id_or_slave_addr(&self) -> UnitIdOrSlaveAddr

Extracts the target device identifier from the message.

This method abstracts the difference between TCP (Unit ID) and Serial (Slave Address) addressing, returning a unified UnitIdOrSlaveAddr type.

§Returns

A UnitIdOrSlaveAddr representing the destination or source device.

Source

pub fn transaction_id(&self) -> u16

Retrieves the transaction identifier for the message.

For TCP messages, this returns the ID from the MBAP header. For Serial (RTU/ASCII) messages, this returns 0 as they are inherently synchronous.

Source

pub fn function_code(&self) -> FunctionCode

Accessor for the function code from the PDU.

Source

pub fn data(&self) -> &Vec<u8, MAX_PDU_DATA_LEN>

Accessor for the data payload from the PDU.

Source

pub fn data_len(&self) -> u8

Accessor for the actual length of the data payload.

Source

pub fn to_bytes(&self) -> Result<Vec<u8, MAX_ADU_FRAME_LEN>, MbusError>

Converts the ModbusMessage into its byte representation.

This method serializes the additional address (MBAP header or slave address) followed by the PDU.

§Returns

Ok(Vec<u8, MAX_ADU_LEN>) containing the ADU bytes, or an MbusError if the message cannot be serialized.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, MbusError>

Creates a ModbusMessage from its byte representation (ADU).

This method parses the MBAP header and the PDU from the given byte slice.

§Arguments
  • bytes - A byte slice containing the complete Modbus TCP ADU.
§Returns

Ok((ModbusMessage, usize)) containing the parsed message and the number of consumed bytes.

Source

pub fn to_ascii_bytes(&self) -> Result<Vec<u8, MAX_ADU_FRAME_LEN>, MbusError>

Converts the ModbusMessage into its ASCII ADU byte representation.

This method serializes the message to binary, calculates the LRC, and then encodes the result into Modbus ASCII format (Start ‘:’, Hex, End CR LF).

§Returns

Ok(Vec<u8, MAX_ADU_FRAME_LEN>) containing the ASCII ADU bytes.

§Errors

Returns MbusError::BufferTooSmall if the resulting ASCII frame exceeds MAX_ADU_FRAME_LEN.

Source

pub fn from_rtu_bytes(frame: &[u8]) -> Result<Self, MbusError>

Creates a ModbusMessage from a raw Modbus RTU byte slice.

This method validates the RTU frame by checking the minimum length and verifying the 16-bit CRC (Cyclic Redundancy Check).

§Arguments
  • frame - A byte slice containing the complete Modbus RTU ADU.
§Returns
  • Ok(ModbusMessage) if the CRC is valid and the PDU is correctly parsed.
  • Err(MbusError) if the frame is too short, the checksum fails, or the PDU is invalid.
Source

pub fn from_ascii_bytes(frame: &[u8]) -> Result<Self, MbusError>

Creates a ModbusMessage from a raw Modbus ASCII byte slice.

This method performs the following validation and transformation steps:

  1. Validates the frame structure (starts with ‘:’, ends with “\r\n”).
  2. Decodes the hexadecimal ASCII representation into binary data.
  3. Verifies the Longitudinal Redundancy Check (LRC) checksum.
  4. Parses the resulting binary into a SlaveAddress and Pdu.
§Arguments
  • frame - A byte slice containing the complete Modbus ASCII ADU.
§Returns
  • Ok(ModbusMessage) if the frame is valid and checksum matches.
  • Err(MbusError) for invalid length, malformed hex, or checksum failure.

Trait Implementations§

Source§

impl Clone for ModbusMessage

Source§

fn clone(&self) -> ModbusMessage

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ModbusMessage

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.