Expand description
A library for controlling the SDM72 series energy meters via Modbus.
This crate provides two main ways to interact with the SDM72 energy meters:
-
High-Level, Safe Clients: Stateful, thread-safe clients that are easy to share and use in concurrent applications. This is the recommended approach for most users. See
tokio_sync_safe_client::SafeClient(blocking) andtokio_async_safe_client::SafeClient(async). -
Low-Level, Stateless Functions: A set of stateless functions that directly map to the device’s Modbus commands. This API offers maximum flexibility but requires manual management of the Modbus context. See the
tokio_syncandtokio_asyncmodules.
§Key Features
- Protocol Implementation: Complete implementation of the SDM72 Modbus protocol.
- Stateful, Thread-Safe Clients: For easy and safe concurrent use.
- Stateless, Low-Level Functions: For maximum flexibility and control.
- Synchronous and Asynchronous APIs: Both blocking and
async/awaitAPIs are available. - Strongly-Typed API: Utilizes Rust’s type system for protocol correctness.
§Cargo Features
This crate uses feature flags to enable different functionalities and to
select the desired tokio-modbus backend.
For library users, it is recommended to disable the default features and
enable only the ones you need. For example, in your Cargo.toml:
[dependencies.sdm72]
version = "0.2"
default-features = false
features = ["tokio-tcp-sync", "safe-client-sync"]§Available Features
tokio-rtu-sync: Enables the synchronous (blocking) RTU backend.tokio-tcp-sync: Enables the synchronous (blocking) TCP backend.tokio-rtu: Enables the asynchronous (async) RTU backend.tokio-tcp: Enables the asynchronous (async) TCP backend.safe-client-sync: Enables the high-level, thread-safe, synchronoustokio_sync_safe_client::SafeClient. Requires eithertokio-rtu-syncortokio-tcp-sync.safe-client-async: Enables the high-level, thread-safe, asynchronoustokio_async_safe_client::SafeClient. Requires eithertokio-rtuortokio-tcp.serde: Enablesserdesupport for theprotocoltypes.bin-dependencies: Enables all dependencies required for thesdm72binary. This is not intended for library users.
The default feature enables bin-dependencies.
§Quick Start
This example shows how to use the recommended high-level, synchronous SafeClient.
use sdm72_lib::{
protocol::Address,
tokio_sync_safe_client::SafeClient,
};
use tokio_modbus::client::sync::tcp;
use tokio_modbus::Slave;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to the device and create a stateful, safe client
let socket_addr = "192.168.1.100:502".parse()?;
let ctx = tcp::connect_slave(socket_addr, Slave(*Address::default()))?;
let mut client = SafeClient::new(ctx);
// Use the client to interact with the device
let values = client.read_all(&Duration::from_millis(100))?;
println!("Successfully read values: {:#?}", values);
Ok(())
}For more details, see the documentation for the specific client you wish to use.
Modules§
- protocol
- This module defines the core data structures and traits for the SDM72 Modbus protocol.
- tokio_
async tokio-rtuortokio-tcp - This module provides an asynchronous client for the SDM72 energy meter.
- tokio_
async_ safe_ client safe-client-asyncand (tokio-rtuortokio-tcp) - This module provides a thread-safe asynchronous client for the SDM72 energy meter.
- tokio_
common tokio-rtu-syncortokio-tcp-syncortokio-rtuortokio-tcp - This module provides common data structures and error types for the
tokiobased clients. - tokio_
sync tokio-rtu-syncortokio-tcp-sync - This module provides a synchronous client for the SDM72 energy meter.
- tokio_
sync_ safe_ client safe-client-syncand (tokio-rtu-syncortokio-tcp-sync) - This module provides a thread-safe synchronous client for the SDM72 energy meter.
Macros§
- decode_
subset_ item_ from_ holding_ register - A macro to decode a holding register value from a response slice.
- decode_
subset_ item_ from_ input_ register - A macro to decode an input register value from a response slice.
- get_
subset_ register_ range - A macro to get the range of a register within a larger response slice.