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 moreSource§impl Debug for ModbusLayer
impl Debug for ModbusLayer
Source§impl From<ModbusLayer> for LayerEnum
impl From<ModbusLayer> for LayerEnum
Source§fn from(v: ModbusLayer) -> LayerEnum
fn from(v: ModbusLayer) -> LayerEnum
Source§impl Layer for ModbusLayer
impl Layer for ModbusLayer
Source§fn header_len(&self, data: &[u8]) -> usize
fn header_len(&self, data: &[u8]) -> usize
Source§fn field_names(&self) -> &'static [&'static str]
fn field_names(&self) -> &'static [&'static str]
Source§impl LayerDispatch for ModbusLayer
impl LayerDispatch for ModbusLayer
Source§fn dispatch_kind(&self) -> LayerKind
fn dispatch_kind(&self) -> LayerKind
Source§fn dispatch_index(&self) -> &LayerIndex
fn dispatch_index(&self) -> &LayerIndex
Source§fn dispatch_summary(&self, buf: &[u8]) -> String
fn dispatch_summary(&self, buf: &[u8]) -> String
Source§fn dispatch_header_len(&self, buf: &[u8]) -> usize
fn dispatch_header_len(&self, buf: &[u8]) -> usize
Source§fn dispatch_field_names(&self) -> &'static [&'static str]
fn dispatch_field_names(&self) -> &'static [&'static str]
Source§fn dispatch_get_field(
&self,
buf: &[u8],
name: &str,
) -> Option<Result<FieldValue, FieldError>>
fn dispatch_get_field( &self, buf: &[u8], name: &str, ) -> Option<Result<FieldValue, FieldError>>
Source§fn dispatch_set_field(
&self,
buf: &mut [u8],
name: &str,
value: FieldValue,
) -> Option<Result<(), FieldError>>
fn dispatch_set_field( &self, buf: &mut [u8], name: &str, value: FieldValue, ) -> Option<Result<(), FieldError>>
Auto Trait Implementations§
impl Freeze for ModbusLayer
impl RefUnwindSafe for ModbusLayer
impl Send for ModbusLayer
impl Sync for ModbusLayer
impl Unpin for ModbusLayer
impl UnsafeUnpin for ModbusLayer
impl UnwindSafe for ModbusLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more