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
impl MultiUnitManager
Sourcepub fn new(config: UnitManagerConfig) -> Self
pub fn new(config: UnitManagerConfig) -> Self
Create a new multi-unit manager.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create with default configuration.
Sourcepub fn config(&self) -> &UnitManagerConfig
pub fn config(&self) -> &UnitManagerConfig
Get the manager configuration.
Sourcepub fn unit_count(&self) -> usize
pub fn unit_count(&self) -> usize
Get the number of registered units.
Sourcepub fn add_unit(&self, unit_id: u8, config: UnitConfig) -> ModbusResult<()>
pub fn add_unit(&self, unit_id: u8, config: UnitConfig) -> ModbusResult<()>
Sourcepub fn remove_unit(&self, unit_id: u8) -> Option<UnitInfo>
pub fn remove_unit(&self, unit_id: u8) -> Option<UnitInfo>
Sourcepub fn get_unit(&self, unit_id: u8) -> Option<Ref<'_, u8, UnitInfo>>
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.
Sourcepub fn get_unit_mut(&self, unit_id: u8) -> Option<RefMut<'_, u8, UnitInfo>>
pub fn get_unit_mut(&self, unit_id: u8) -> Option<RefMut<'_, u8, UnitInfo>>
Get a mutable reference to a unit.
Sourcepub fn update_unit<F>(&self, unit_id: u8, update_fn: F) -> ModbusResult<()>where
F: FnOnce(&mut UnitConfig),
pub fn update_unit<F>(&self, unit_id: u8, update_fn: F) -> ModbusResult<()>where
F: FnOnce(&mut UnitConfig),
Update a unit’s configuration.
Sourcepub fn read_holding_registers(
&self,
unit_id: u8,
address: u16,
quantity: u16,
) -> ModbusResult<Vec<u16>>
pub fn read_holding_registers( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<u16>>
Read holding registers from a unit.
Sourcepub fn read_input_registers(
&self,
unit_id: u8,
address: u16,
quantity: u16,
) -> ModbusResult<Vec<u16>>
pub fn read_input_registers( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<u16>>
Read input registers from a unit.
Sourcepub fn read_coils(
&self,
unit_id: u8,
address: u16,
quantity: u16,
) -> ModbusResult<Vec<bool>>
pub fn read_coils( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<bool>>
Read coils from a unit.
Sourcepub fn read_discrete_inputs(
&self,
unit_id: u8,
address: u16,
quantity: u16,
) -> ModbusResult<Vec<bool>>
pub fn read_discrete_inputs( &self, unit_id: u8, address: u16, quantity: u16, ) -> ModbusResult<Vec<bool>>
Read discrete inputs from a unit.
Sourcepub fn write_holding_register(
&self,
unit_id: u8,
address: u16,
value: u16,
) -> ModbusResult<()>
pub fn write_holding_register( &self, unit_id: u8, address: u16, value: u16, ) -> ModbusResult<()>
Write a single holding register to a unit.
Sourcepub fn write_holding_registers(
&self,
unit_id: u8,
address: u16,
values: &[u16],
) -> ModbusResult<()>
pub fn write_holding_registers( &self, unit_id: u8, address: u16, values: &[u16], ) -> ModbusResult<()>
Write multiple holding registers to a unit.
Sourcepub fn write_coil(
&self,
unit_id: u8,
address: u16,
value: bool,
) -> ModbusResult<()>
pub fn write_coil( &self, unit_id: u8, address: u16, value: bool, ) -> ModbusResult<()>
Write a single coil to a unit.
Sourcepub fn write_coils(
&self,
unit_id: u8,
address: u16,
values: &[bool],
) -> ModbusResult<()>
pub fn write_coils( &self, unit_id: u8, address: u16, values: &[bool], ) -> ModbusResult<()>
Write multiple coils to a unit.
Sourcepub fn broadcast_write_holding_register(
&self,
address: u16,
value: u16,
) -> ModbusResult<()>
pub fn broadcast_write_holding_register( &self, address: u16, value: u16, ) -> ModbusResult<()>
Broadcast write holding register to all applicable units.
Sourcepub fn broadcast_write_holding_registers(
&self,
address: u16,
values: &[u16],
) -> ModbusResult<()>
pub fn broadcast_write_holding_registers( &self, address: u16, values: &[u16], ) -> ModbusResult<()>
Broadcast write multiple holding registers to all applicable units.
Sourcepub fn broadcast_write_coil(
&self,
address: u16,
value: bool,
) -> ModbusResult<()>
pub fn broadcast_write_coil( &self, address: u16, value: bool, ) -> ModbusResult<()>
Broadcast write coil to all applicable units.
Sourcepub fn broadcast_write_coils(
&self,
address: u16,
values: &[bool],
) -> ModbusResult<()>
pub fn broadcast_write_coils( &self, address: u16, values: &[bool], ) -> ModbusResult<()>
Broadcast write multiple coils to all applicable units.
Sourcepub fn total_requests(&self) -> u64
pub fn total_requests(&self) -> u64
Get total request count.
Sourcepub fn broadcast_count(&self) -> u64
pub fn broadcast_count(&self) -> u64
Get total broadcast count.
Sourcepub fn unit_statistics(&self) -> Vec<UnitStatistics>
pub fn unit_statistics(&self) -> Vec<UnitStatistics>
Get statistics for all units.
Sourcepub fn reset_statistics(&self)
pub fn reset_statistics(&self)
Reset all statistics.