pub struct ModbusLayer {
pub index: LayerIndex,
pub frame_type: ModbusFrameType,
}Expand description
Modbus layer – a zero-copy view into a packet buffer.
By default assumes Modbus/TCP (MBAP) framing since that is what appears on the wire over TCP port 502.
Fields§
§index: LayerIndex§frame_type: ModbusFrameTypeImplementations§
Source§impl ModbusLayer
impl ModbusLayer
Sourcepub fn new(index: LayerIndex) -> Self
pub fn new(index: LayerIndex) -> Self
Create a new Modbus layer from a layer index (defaults to TCP framing).
Sourcepub fn with_frame_type(index: LayerIndex, frame_type: ModbusFrameType) -> Self
pub fn with_frame_type(index: LayerIndex, frame_type: ModbusFrameType) -> Self
Create a Modbus layer with explicit frame type.
Sourcepub fn trans_id(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn trans_id(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Transaction ID (MBAP bytes 0-1).
Sourcepub fn proto_id(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn proto_id(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Protocol ID (MBAP bytes 2-3; should be 0x0000).
Sourcepub fn length(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn length(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Length field (MBAP bytes 4-5).
Sourcepub fn func_code(&self, buf: &[u8]) -> Result<u8, FieldError>
pub fn func_code(&self, buf: &[u8]) -> Result<u8, FieldError>
Get the Function Code (MBAP byte 7).
Sourcepub fn is_error(&self, buf: &[u8]) -> bool
pub fn is_error(&self, buf: &[u8]) -> bool
Check if this is an exception response (function code has bit 7 set).
Sourcepub fn except_code(&self, buf: &[u8]) -> Result<u8, FieldError>
pub fn except_code(&self, buf: &[u8]) -> Result<u8, FieldError>
Get the exception code (byte 8, only valid for error responses).
Sourcepub fn start_addr(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn start_addr(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Start Address (bytes 8-9 for request PDUs like 0x01-0x06).
Sourcepub fn quantity(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn quantity(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Quantity field (bytes 10-11 for request PDUs like 0x01-0x04).
Sourcepub fn byte_count(&self, buf: &[u8]) -> Result<u8, FieldError>
pub fn byte_count(&self, buf: &[u8]) -> Result<u8, FieldError>
Get the Byte Count field (byte 8 for response PDUs like 0x01-0x04).
Sourcepub fn output_value(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn output_value(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Output Value for Write Single Coil/Register (bytes 10-11).
Sourcepub fn register_val(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn register_val(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Register Value for Write Single Register (bytes 10-11).
Sourcepub fn sub_func(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn sub_func(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Sub-function code for Diagnostics (0x08) (bytes 8-9).
Sourcepub fn ref_addr(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn ref_addr(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the Reference Address for Mask Write Register (0x16) (bytes 8-9).
Sourcepub fn and_mask(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn and_mask(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the AND mask for Mask Write Register (0x16) (bytes 10-11).
Sourcepub fn or_mask(&self, buf: &[u8]) -> Result<u16, FieldError>
pub fn or_mask(&self, buf: &[u8]) -> Result<u16, FieldError>
Get the OR mask for Mask Write Register (0x16) (bytes 12-13).
Sourcepub fn data(&self, buf: &[u8]) -> Result<Vec<u8>, FieldError>
pub fn data(&self, buf: &[u8]) -> Result<Vec<u8>, FieldError>
Get the raw data bytes after the function code (bytes 8..end).
Sourcepub fn set_trans_id(&self, buf: &mut [u8], value: u16) -> Result<(), FieldError>
pub fn set_trans_id(&self, buf: &mut [u8], value: u16) -> Result<(), FieldError>
Set the Transaction ID.
Sourcepub fn set_unit_id(&self, buf: &mut [u8], value: u8) -> Result<(), FieldError>
pub fn set_unit_id(&self, buf: &mut [u8], value: u8) -> Result<(), FieldError>
Set the Unit ID.
Sourcepub fn set_func_code(&self, buf: &mut [u8], value: u8) -> Result<(), FieldError>
pub fn set_func_code(&self, buf: &mut [u8], value: u8) -> Result<(), FieldError>
Set the Function Code.
Sourcepub fn set_start_addr(
&self,
buf: &mut [u8],
value: u16,
) -> Result<(), FieldError>
pub fn set_start_addr( &self, buf: &mut [u8], value: u16, ) -> Result<(), FieldError>
Set the Start Address (bytes 8-9).
Sourcepub fn set_quantity(&self, buf: &mut [u8], value: u16) -> Result<(), FieldError>
pub fn set_quantity(&self, buf: &mut [u8], value: u16) -> Result<(), FieldError>
Set the Quantity (bytes 10-11).
Sourcepub fn field_names() -> &'static [&'static str]
pub fn field_names() -> &'static [&'static str]
Get the field names for this layer.
Sourcepub fn get_field(
&self,
buf: &[u8],
name: &str,
) -> Option<Result<FieldValue, FieldError>>
pub fn get_field( &self, buf: &[u8], name: &str, ) -> Option<Result<FieldValue, FieldError>>
Get a field value by name.
Sourcepub fn set_field(
&self,
buf: &mut [u8],
name: &str,
value: FieldValue,
) -> Option<Result<(), FieldError>>
pub fn set_field( &self, buf: &mut [u8], name: &str, value: FieldValue, ) -> Option<Result<(), FieldError>>
Set a field value by name.
Trait Implementations§
Source§impl Clone for ModbusLayer
impl Clone for ModbusLayer
Source§fn clone(&self) -> ModbusLayer
fn clone(&self) -> ModbusLayer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more