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: AdditionalAddressThe MBAP header for Modbus TCP messages.
pdu: PduThe Protocol Data Unit (PDU) containing the function code and data.
Implementations§
Source§impl ModbusMessage
impl ModbusMessage
Sourcepub fn new(additional_address: AdditionalAddress, pdu: Pdu) -> Self
pub fn new(additional_address: AdditionalAddress, pdu: Pdu) -> Self
Sourcepub fn additional_address(&self) -> &AdditionalAddress
pub fn additional_address(&self) -> &AdditionalAddress
Accessor for the additional address.
Sourcepub fn pdu(&self) -> &Pdu
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).
Sourcepub fn unit_id_or_slave_addr(&self) -> UnitIdOrSlaveAddr
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.
Sourcepub fn transaction_id(&self) -> u16
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.
Sourcepub fn function_code(&self) -> FunctionCode
pub fn function_code(&self) -> FunctionCode
Accessor for the function code from the PDU.
Sourcepub fn to_bytes(&self) -> Result<Vec<u8, MAX_ADU_FRAME_LEN>, MbusError>
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.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, MbusError>
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.
Sourcepub fn to_ascii_bytes(&self) -> Result<Vec<u8, MAX_ADU_FRAME_LEN>, MbusError>
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.
Sourcepub fn from_rtu_bytes(frame: &[u8]) -> Result<Self, MbusError>
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.
Sourcepub fn from_ascii_bytes(frame: &[u8]) -> Result<Self, MbusError>
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:
- Validates the frame structure (starts with ‘:’, ends with “\r\n”).
- Decodes the hexadecimal ASCII representation into binary data.
- Verifies the Longitudinal Redundancy Check (LRC) checksum.
- Parses the resulting binary into a
SlaveAddressandPdu.
§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
impl Clone for ModbusMessage
Source§fn clone(&self) -> ModbusMessage
fn clone(&self) -> ModbusMessage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more