Juniconnlib Modbus Core
Modbus Core is a Python library for reading and decoding Modbus registers from devices and forwarding the resulting values as FIWARE-compatible payloads. It is designed to work in agent-based ecosystems using agentlib and juniconnlib-core, while also providing a local simulator and regression tests for device-level behavior. Including the functions for reading, writing, handling exceptions, and managing the Modbus connection, it is designed to be used with various Modbus devices and is designed to forward the read and write registers to another juniconnlib module.
The project focuses on:
- discovering devices from a JSON device registry
- loading per-device register mappings from JSON
- connecting to Modbus TCP endpoints with retry logic
- decoding register values into typed Python values
- generating FIWARE payloads for downstream publishing
- supporting a local simulated Modbus server for testing
Key implementation modules:
src/juniconnlib_modbus_core/modules/modbus_device.py: device abstraction, register reading, decoding, and write supportsrc/juniconnlib_modbus_core/modules/modbus_core_communicator.py: multi-device orchestration, coordinator, reconnection logic, and FIWARE payload dispatchtests/test_module/test_module.py: lightweight utility for polling FIWARE provisioned entitiessimulator/run_simulator.py: local Modbus simulator runner
Features
Device registry support
The communicator reads a device list from a JSON file and creates one ModbusDevice per entry.
Register mapping support
Each device type can specify its attribute map and register blocks in a JSON configuration. The library supports multiple register ranges and multiple data types.
Typed decoding
Supported data types in ModbusAttributeConfig include:
uint16int16uint32int32float32float16timeStampbitmask16
FIWARE-ready payloads
Each read cycle creates one or more AttributeUpdate payloads, with entity_id, entity_type, and a decoded attribute dictionary suitable for FIWARE or downstream agent publishing.
Reconnect handling
The core communicator tracks disconnected devices, retries according to configured limits, and continues polling only for valid connected/provisioned devices.
Local simulator
A Modbus simulator is included so you can validate register reads/writes without a live device.
Installation
This project is packaged with setuptools and requires Python 3.12 or newer.
Install from PyPI
To install the latest published release into your active Python environment:
pip install juniconnlib-modbus-core
Install from source
Use an editable installation when developing the project or working with the repository checkout:
git clone git@jugit.fz-juelich.de:iek-10/public/ict-platform/fiware-applications/juniconnlib-modbus-core.git
cd juniconnlib-modbus-core
python -m venv .venv
Activate the virtual environment, then install the package:
# Linux/macOS
source .venv/bin/activate
# Windows PowerShell
.venv\Scripts\Activate.ps1
# Windows Command Prompt
.venv\Scripts\activate.bat
pip install -U pip
pip install -e .
To include the test dependencies in the editable installation:
pip install -e .[tests]
Runtime dependencies
The project depends on:
agentlibfilipopenpyxlpandaspydanticpymodbusjuniconnlib-core
These are declared in pyproject.toml.
Configuration
1. Device registry JSON file
The communicator expects a JSON file with either a single object or a list of objects, each containing at least these keys:
device_iddevice_typemodbus_hostmodbus_port
Example:
[
{
"device_id": 1,
"device_type": "SensorType",
"modbus_host": "192.0.2.10",
"modbus_port": 502
},
{
"device_id": 2,
"device_type": "SensorType",
"modbus_host": "192.0.2.11",
"modbus_port": 502
}
]
Entries without a device_id or modbus_host are dropped. Duplicate device_id values raise a ValueError.
2. Register configuration JSON file
Each device type is described in a JSON object keyed by the device type string.
Example:
{
"device_config": {
"SensorType": {
"modbus_attributes": {
"voltage_u1_l1_n": {
"address": 0,
"data_type": "float32",
"ref_scale_factor": 1.0
},
"frequency": {
"address": 2,
"data_type": "float32",
"ref_scale_factor": 0.1
},
"status_bits": {
"address": 4,
"data_type": "bitmask16",
"enum_bitfield_mapping": {
"0": "ok",
"1": "warning",
"2": "alarm"
}
}
},
"modbus_registers": [[0, 5]],
"create_timestamp": true,
"byteorder": "BIG",
"wordorder": "BIG"
}
}
}
Field explanations:
address: starting register address for the attributedata_type: Modbus register type to decoderef_scale_factor: optional multiplier applied after decodingis_static: marks static attributes if neededenum_bitfield_mapping: used withbitmask16to map bit positions to labelsmodbus_registers: range list of register intervals to read per cyclecreate_timestamp: adds a timestamp near the read timebyteorder: endian handling within each 16-bit registerwordorder: endian ordering for 32-bit values
3. Agentlib module configuration
The communicator is intended to be used as an agentlib module. A typical configuration looks like this:
{
"id": "sensor_agent",
"modules": [
{
"module_id": "modbus_core_communicator",
"type": "juniconnlib_modbus_core.modbus_core_communicator",
"device_file": "/path/to/devices.json",
"register_file": "/path/to/registers.json",
"device_fiware_header": {
"service": "my_service",
"service_path": "/"
},
"read_interval": 1,
"max_reconnect_attempts": 3,
"reconnect_interval": 60,
"resource": "/iot/json",
"protocol": "json"
}
]
}
This is consistent with ModbusCoreConfig.
Architecture
ModbusDevice
ModbusDevice is the core unit representing a single physical Modbus endpoint.
Responsibilities:
- connect/disconnect a
pymodbusTCP client - read holding registers from configured ranges
- decode values according to configured attribute types
- create FIWARE-style payloads from decoded data
- optionally write individual or multiple registers
ModbusCoreCommunicator
ModbusCoreCommunicator acts as the coordinator for many devices.
Responsibilities:
- load device entries from the JSON registry
- Create a
ModbusDevicefor each device entry - connect each device
- detect disconnected devices
- attempt reconnection with bounded retry logic
- publish each device payload to the agent broker
- keep track of provisioned FIWARE entities
This logic is implemented in ModbusCoreCommunicator.process() and related helpers.
Data decoding behavior
The library reads contiguous ranges of registers and then slices them by attribute address offsets. For example, if a range is [10, 15] and an attribute is configured at address 12, it will read the correct subset from the raw registers.
ModbusDevice.decode_modbus_registers() applies:
- register-range filtering
- register count validation
- decode-by-type mapping
- byte/endian normalization
- optional scale factor multiplication
- payload assembly
ref_scale_factor
If ref_scale_factor is set on an attribute, the decoded value is multiplied by that factor before publication.
Example:
{"address": 1, "data_type": "int16", "ref_scale_factor": 0.1}
This is useful for engineering values represented as scaled integers in the device.
Simulator
The repo contains a simple local simulator for testing Modbus register reads and writes.
Start the simulator
python simulator/run_simulator.py
The simulator configuration is located here.
It defines a test server and some example registers, including a few writeable and invalid addresses. This is useful for validating the Modbus protocol layer without connecting to real hardware.
Example quick start
1. Prepare a device registry
Create devices.json:
[
{
"device_id": 1,
"device_type": "SensorType",
"modbus_host": "192.0.2.10",
"modbus_port": 502
},
{
"device_id": 2,
"device_type": "SensorType",
"modbus_host": "192.0.2.11",
"modbus_port": 502
}
]
2. Prepare a register definition
{
"device_config": {
"SensorType": {
"modbus_attributes": {
"frequency": {
"address": 0,
"data_type": "float32"
},
"voltage": {
"address": 2,
"data_type": "float32"
}
},
"modbus_registers": [[0, 3]],
"create_timestamp": true,
"byteorder": "BIG",
"wordorder": "BIG"
}
}
}
3. Run the module in an agent context
Use the agent configuration shown earlier and start your agent process. The communicator will read the device list, connect to each device, and publish decoded payloads.
Local test harness
The repo includes a simple FIWARE test module in tests/test_module/test_module.py.
It reads entities from an Orion context broker and prints common values like voltage, current, frequency, and power. You can run it with a config file such as tests/configs/config_test_module.json via:
MODBUS_CONFIG=tests/configs/config_test_module.json python -m tests.test_module.main
This is primarily a validation utility; the main functional code is the Modbus communicator and device modules.
Testing
The project ships with pytest-based unit tests under tests/.
Run the suite with:
pytest -q
Examples of coverage include:
- device connection and reconnection behavior
- register read errors and warnings
- payload creation
- endianness and scale handling
- utility validation for IP/url handling
tests/manual_sim.py and tests/manual_sim_modbus.py are manual scripts and require a running simulator.
Notes and caveats
- The code expects a valid Modbus TCP server at the configured host/port.
modbus_registersshould be sized carefully to avoid oversized frames.byteorderandwordordermust currently be given in upper case ("BIG"/"LITTLE") in the register configuration.max_reconnect_attemptsandreconnect_intervalare enforced by the coordinator.- Some legacy examples in the repo are simplified and serve as reference patterns rather than the only supported configuration style.
If you are integrating this into an agent environment, start from the device registry and register JSON examples, then validate against the simulator before connecting to physical hardware.
Metadata
Release files for juniconnlib-modbus-core 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| juniconnlib_modbus_core-0.1.0.tar.gz | 21.9 kB | Without provenance | Details for juniconnlib_modbus_core-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| juniconnlib_modbus_core-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for juniconnlib_modbus_core-0.1.0-py3-none-any.whl |
Total release size: 39.5 kB
Release files / juniconnlib_modbus_core-0.1.0.tar.gz
| Download URL | juniconnlib_modbus_core-0.1.0.tar.gz |
|---|---|
| Size | 21.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bf8cd3e14cf90767edf6c65c15229ec121331576aaf298e329b7670ae4db2c13
|
|
BLAKE2b-256 checksum How to use checksums |
7fa2e7ee9974ecd78df0d62a5d9ee91ec7a4ab3531ec58c0aaa11e7edfd39a92
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.15.2
|
Release files / juniconnlib_modbus_core-0.1.0-py3-none-any.whl
| Download URL | juniconnlib_modbus_core-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5c2b3f482260cac8fb731370a017b12c65c7826d31bd2d2b7e5959f95d35ac00
|
|
BLAKE2b-256 checksum How to use checksums |
3f2c92bcdc1ac845b4830382714278a479b3cc93f7153e29ffb9da138c44b711
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.15.2
|
Security for juniconnlib-modbus-core 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.