2 unstable releases

0.2.0 Mar 5, 2024
0.1.3 Feb 29, 2024
0.1.2 Feb 29, 2024
0.1.1 Feb 28, 2024
0.1.0 Feb 28, 2024

#753 in Hardware support

Download history 327/week @ 2024-02-26 79/week @ 2024-03-04 125/week @ 2024-03-11 37/week @ 2024-04-01

212 downloads per month

MIT license

24KB
402 lines

modbus-mapping

An API for Modbus Register Mapppings based on tokio-modbus

Docs.rs crates.io

Repo URL

Usage

Check examples folder for usage.

The device and client examples pair is to be run at the same time in two different terminals. The RTU examples assume that virtual serial port has been created beforehand

socat -d -d pty,raw,echo=0,link=/tmp/ttys001 pty,raw,echo=0,link=/tmp/ttys002

Development

Check justfile.


lib.rs:

A high-level API for Modbus Register Maps based on tokio-modbus.

Traits

The library defines

Derive macro

For convenience it provides derive macros to implement the traits automatically. The derive macros depends on modbus field attribute.

The modbus attributes can be added to struct fields to link them with modbus register mapping entries. Then, the field modbus must contain the following key-values pairs:

  • addr - input or holding register start address, u16 integer,
  • ty - modbus data type, one of "i16", "i32", "i64", "u16", "u32", "u64", "f16", "f32" or "raw(size)",
  • ord - word order, either "be" for big-endian or "le" for little-endian,
  • x - scale factor; multiply the stored value by it to get the actual value
  • unit - measurement unit of the actual value (i.e. actual value = stored value x scale factor)

The struct modbus attribute is optional and provides configuration for InputRegisterMap and HoldingRegisterMap traits when reading registers. It can only contain these key-value pairs:

  • 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
  • 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. 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.

The modbus_doc attribute is to create documentation (by adding doc attribute) from modbus field attributes information.

Dependencies

~3–13MB
~146K SLoC