Skip to main content

ModbusClient

Struct ModbusClient 

Source
pub struct ModbusClient<D: DataLink> { /* private fields */ }
Expand description

Async Modbus client that wraps a DataLink transport.

The client is cheaply cloneable — all clones share the same underlying transport, throttle state, and metrics counters.

Implementations§

Source§

impl<D: DataLink> ModbusClient<D>

Source

pub fn new(datalink: D) -> Self

Create a new client with default configuration.

Source

pub fn with_config(datalink: D, config: ClientConfig) -> Self

Create a new client with the given configuration.

Source

pub fn config(&self) -> ClientConfig

Return the current client configuration.

Source

pub fn is_connected(&self) -> bool

Check if the underlying transport is connected.

Source

pub async fn read_coils( &self, unit_id: UnitId, start: u16, quantity: u16, ) -> Result<Vec<bool>, ClientError>

Read coils (FC01) starting at start, returning quantity boolean values.

Source

pub async fn custom_request( &self, unit_id: UnitId, function_code: u8, payload: &[u8], ) -> Result<Vec<u8>, ClientError>

Send a custom (user-defined) function code request and return the raw response payload.

Source

pub async fn report_server_id( &self, unit_id: UnitId, ) -> Result<ReportServerIdResponse, ClientError>

Report Server ID (FC17) — returns the device identifier and run status.

Source

pub async fn read_device_identification( &self, unit_id: UnitId, read_device_id_code: u8, object_id: u8, ) -> Result<ReadDeviceIdentificationResponse, ClientError>

Read Device Identification (FC43/0x0E) — returns device info objects.

Source

pub async fn read_discrete_inputs( &self, unit_id: UnitId, start: u16, quantity: u16, ) -> Result<Vec<bool>, ClientError>

Read discrete inputs (FC02) starting at start, returning quantity boolean values.

Source

pub async fn read_holding_registers( &self, unit_id: UnitId, start: u16, quantity: u16, ) -> Result<Vec<u16>, ClientError>

Read holding registers (FC03) starting at start, returning quantity 16-bit values.

Source

pub async fn read_input_registers( &self, unit_id: UnitId, start: u16, quantity: u16, ) -> Result<Vec<u16>, ClientError>

Read input registers (FC04) starting at start, returning quantity 16-bit values.

Source

pub async fn write_single_coil( &self, unit_id: UnitId, address: u16, value: bool, ) -> Result<(), ClientError>

Write a single coil (FC05) at address to value.

Source

pub async fn write_single_register( &self, unit_id: UnitId, address: u16, value: u16, ) -> Result<(), ClientError>

Write a single holding register (FC06) at address to value.

Source

pub async fn mask_write_register( &self, unit_id: UnitId, address: u16, and_mask: u16, or_mask: u16, ) -> Result<(), ClientError>

Mask write register (FC22): result = (current AND and_mask) OR (or_mask AND NOT and_mask).

Source

pub async fn write_multiple_coils( &self, unit_id: UnitId, start: u16, values: &[bool], ) -> Result<(), ClientError>

Write multiple coils (FC15) starting at start.

Source

pub async fn write_multiple_registers( &self, unit_id: UnitId, start: u16, values: &[u16], ) -> Result<(), ClientError>

Write multiple holding registers (FC16) starting at start.

Source

pub async fn read_write_multiple_registers( &self, unit_id: UnitId, read_start: u16, read_quantity: u16, write_start: u16, write_values: &[u16], ) -> Result<Vec<u16>, ClientError>

Atomically read and write multiple registers (FC23).

Source

pub async fn read_coils_raw( &self, unit_id: UnitId, start: u16, quantity: u16, ) -> Result<(Vec<u8>, u16), ClientError>

Read coils returning raw packed bytes and the quantity, avoiding the 8x memory expansion of Vec<bool>.

Source

pub async fn read_discrete_inputs_raw( &self, unit_id: UnitId, start: u16, quantity: u16, ) -> Result<(Vec<u8>, u16), ClientError>

Read discrete inputs returning raw packed bytes and the quantity.

Source

pub async fn read_exception_status( &self, unit_id: UnitId, ) -> Result<u8, ClientError>

FC07 - Read Exception Status

Source

pub async fn diagnostics( &self, unit_id: UnitId, sub_function: u16, data: u16, ) -> Result<(u16, u16), ClientError>

FC08 - Diagnostics

Source

pub async fn read_fifo_queue( &self, unit_id: UnitId, address: u16, ) -> Result<Vec<u16>, ClientError>

FC24 - Read FIFO Queue

Trait Implementations§

Source§

impl<D: DataLink> Clone for ModbusClient<D>

Source§

fn clone(&self) -> Self

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

Auto Trait Implementations§

§

impl<D> Freeze for ModbusClient<D>

§

impl<D> !RefUnwindSafe for ModbusClient<D>

§

impl<D> Send for ModbusClient<D>

§

impl<D> Sync for ModbusClient<D>

§

impl<D> Unpin for ModbusClient<D>

§

impl<D> UnsafeUnpin for ModbusClient<D>

§

impl<D> !UnwindSafe for ModbusClient<D>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more