Skip to main content

MultiUnitManager

Struct MultiUnitManager 

Source
pub struct MultiUnitManager { /* private fields */ }
Expand description

Manager for multiple Modbus units.

Provides centralized management of multiple Modbus slave units, each with its own register store, word order configuration, and statistics.

§Thread Safety

MultiUnitManager is thread-safe and can be shared across threads. All operations are lock-free using DashMap.

§Example

use mabi_modbus::unit::{MultiUnitManager, UnitConfig, UnitManagerConfig};

let manager = MultiUnitManager::new(UnitManagerConfig::default());

// Add units
manager.add_unit(1, UnitConfig::new("Unit 1")).unwrap();
manager.add_unit(2, UnitConfig::new("Unit 2")).unwrap();

// Access unit
{
    let unit = manager.get_unit(1).unwrap();
    assert_eq!(unit.name(), "Unit 1");
}

Implementations§

Source§

impl MultiUnitManager

Source

pub fn new(config: UnitManagerConfig) -> Self

Create a new multi-unit manager.

Source

pub fn with_defaults() -> Self

Create with default configuration.

Source

pub fn config(&self) -> &UnitManagerConfig

Get the manager configuration.

Source

pub fn unit_count(&self) -> usize

Get the number of registered units.

Source

pub fn unit_ids(&self) -> Vec<u8>

Get all unit IDs.

Source

pub fn has_unit(&self, unit_id: u8) -> bool

Check if a unit exists.

Source

pub fn add_unit(&self, unit_id: u8, config: UnitConfig) -> ModbusResult<()>

Add a new unit.

§Arguments
  • unit_id - Unit ID (1-247)
  • config - Unit configuration
§Returns

Ok(()) if the unit was added, Err if:

  • Unit ID is 0 (reserved for broadcast)
  • Unit already exists
  • Maximum units reached
Source

pub fn remove_unit(&self, unit_id: u8) -> Option<UnitInfo>

Remove a unit.

§Returns

The removed unit info if it existed.

Source

pub fn get_unit(&self, unit_id: u8) -> Option<Ref<'_, u8, UnitInfo>>

Get a unit by ID.

If auto_create_units is enabled and the unit doesn’t exist, it will be created with default configuration.

Source

pub fn get_unit_mut(&self, unit_id: u8) -> Option<RefMut<'_, u8, UnitInfo>>

Get a mutable reference to a unit.

Source

pub fn update_unit<F>(&self, unit_id: u8, update_fn: F) -> ModbusResult<()>
where F: FnOnce(&mut UnitConfig),

Update a unit’s configuration.

Source

pub fn read_holding_registers( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<u16>>

Read holding registers from a unit.

Source

pub fn read_input_registers( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<u16>>

Read input registers from a unit.

Source

pub fn read_coils( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<bool>>

Read coils from a unit.

Source

pub fn read_discrete_inputs( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<bool>>

Read discrete inputs from a unit.

Source

pub fn write_holding_register( &self, unit_id: u8, address: u16, value: u16, ) -> ModbusResult<()>

Write a single holding register to a unit.

Source

pub fn write_holding_registers( &self, unit_id: u8, address: u16, values: &[u16], ) -> ModbusResult<()>

Write multiple holding registers to a unit.

Source

pub fn write_coil( &self, unit_id: u8, address: u16, value: bool, ) -> ModbusResult<()>

Write a single coil to a unit.

Source

pub fn write_coils( &self, unit_id: u8, address: u16, values: &[bool], ) -> ModbusResult<()>

Write multiple coils to a unit.

Source

pub fn broadcast_write_holding_register( &self, address: u16, value: u16, ) -> ModbusResult<()>

Broadcast write holding register to all applicable units.

Source

pub fn broadcast_write_holding_registers( &self, address: u16, values: &[u16], ) -> ModbusResult<()>

Broadcast write multiple holding registers to all applicable units.

Source

pub fn broadcast_write_coil( &self, address: u16, value: bool, ) -> ModbusResult<()>

Broadcast write coil to all applicable units.

Source

pub fn broadcast_write_coils( &self, address: u16, values: &[bool], ) -> ModbusResult<()>

Broadcast write multiple coils to all applicable units.

Source

pub fn total_requests(&self) -> u64

Get total request count.

Source

pub fn broadcast_count(&self) -> u64

Get total broadcast count.

Source

pub fn unit_statistics(&self) -> Vec<UnitStatistics>

Get statistics for all units.

Source

pub fn reset_statistics(&self)

Reset all statistics.

Trait Implementations§

Source§

impl Debug for MultiUnitManager

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for MultiUnitManager

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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