modbus_mapping/
lib.rs

1//! A high-level API for Modbus Register Maps based on [tokio-modbus](https://github.com/slowtec/tokio-modbus).
2
3//! ## Traits
4//!
5//! The library defines
6//!
7//! - [`core::InputRegisterMap`] and [`core::HoldingRegisterMap`] traits to read from (and write to) the Modbus registers in batch, and
8//! - [`simulator::InputRegisterModel`] and [`simulator::HoldingRegisterModel`] traits to simulate a Modbus device
9//!
10//! ## Derive macro
11//!
12//! For convenience it provides derive macros to implement the traits automatically. The derive macros depends on `modbus` field attribute.
13//!
14//! See [examples/](https://github.com/vladimirvrabely/modbus-mapping/tree/main/modbus-mapping/examples) for simple usage.
15//!
16//! The `modbus` attributes **can** be added to struct fields to link them with modbus register mapping entries.
17//! Then, the field `modbus` **must** contain the following key-values pairs:
18//! - `addr` - input or holding register start address, `u16` integer,
19//! - `ty` - modbus data type, one of `"i16"`, `"i32"`, `"i64"`, `"u16"`, `"u32"`, `"u64"`, `"f16"`, `"f32"` or `"raw(size)"`,
20//! - `ord` - word order, either `"be"` for big-endian or `"le"` for little-endian,
21//! - `x` - scale factor; multiply the stored value by it to get the actual value
22//! - `unit` - measurement unit of the actual value (i.e. actual value = stored value x scale factor)
23//!
24//! The struct `modbus` attribute is optional and provides configuration for `InputRegisterMap` and `HoldingRegisterMap` traits when reading registers.
25//! It  **can only** contain these key-value pairs:
26//! - `max_cnt_per_request` - maximum number of registers to read in a single Modbus request; default value is `123` which is the maximumum allowed value
27//! - `allow_register_gaps` - an optimization flag to allow Modbus client to read longer register blocks which possibly contain unrequested (or undefined) registers in between the required ones.
28//!   If `true`, the client makes less requests but read more data. Otherwise, if `false`, the client makes more requests but read only the necessary data.
29//!
30//! The `modbus_doc` attribute is to create documentation (by adding doc attribute) from `modbus` field attributes information.
31
32/// Utilities for encoding from and decoding to Modbus registers
33pub mod codec;
34/// Core traits to read from and write to Modbus registers
35pub mod core;
36
37/// Traits and utilities to create device simulator (based on tokio-modbus [servers examples](https://github.com/slowtec/tokio-modbus/tree/main/examples))
38#[cfg(feature = "simulator")]
39pub mod simulator;
40
41pub mod derive {
42    /// Re-export.
43    pub use modbus_mapping_derive::{
44        modbus_doc, HoldingRegisterMap, HoldingRegisterModel, InputRegisterMap, InputRegisterModel,
45    };
46}