ModBus Client Library (modbusgw-client)
A companion library for interacting with ModBus devices - either directly or through the Modbus Gateway - using a consistent, class-based API. The package mirrors the PDUs used on the gateway side, exposes pluggable transports (serial RTU, Modbus/TCP over TCP or Unix sockets, optional (m)TLS), and wraps everything behind a convenience dependency-light client.
Features
- Unified transport abstraction:
BaseClientdefines the lifecycle contract, whileSerialClientandTcpClientimplement retryable RTU / ModBus-TCP access with context-manager support and sync/async entry points. - High-level helper API:
ModbusClientoffers typed helpers for reading coils, holding/input registers, and writing coils/registers with struct-based encoding/decoding helpers for binary payloads. - Raw PDU access:
ModbusRequest/ModbusResponseclasses mirror the gateway side. - TLS & Unix socket support: ModBus/TCP connections can be wrapped in TLS (including mTLS) or redirected over Unix domain sockets for local-only communication.
- Error model: Client-specific exceptions distinguish transport, protocol, and server-side failures, making it easy to hook into retry/backoff logic in higher-level applications.
Installation
Install from PyPI:
pip install modbusgw-client
or from this repository while hacking on both the gateway and client in tandem:
pip install -e ./modbusgw_client/
Usage Examples
Serial RTU session:
from modbusgw_client import api
with api.ModbusClient.serial("/dev/ttyUSB0", baudrate=9600, unit_id=1) as client:
coils = client.read_coils(address=0, quantity=8)
client.write_register(address=10, value=0x1234)
Modbus/TCP (with optional TLS) session:
from modbusgw_client import api
from modbusgw_client.tcp_client import TLSConfig
with api.ModbusClient.tcp("192.0.2.15", port=1502, tls=TLSConfig(ca_file="/etc/ssl/certs/root.pem")) as client:
temps = client.read_input_registers(address=100, quantity=2, fmt=">f")
client.write_coils(address=20, values=[True, False, True])
Both open the transport during the context manager block and close it automatically. In addition to context management one can call client.connect() / client.close() manually.
Advanced Usage
- Transaction batching:
BaseClient.bulk_execute()accepts an iterable of pre-built PDUs and returns decoded responses synchronously. - Structured payloads: The helper can pack/unpack register payloads using Python'"'"'s
structsyntax (fmt="<f", etc.). Validation ensures the byte lengths match full registers before transmission. - Unix socket mode:
ModbusClient.unix()is a thin wrapper aroundTcpClient(unix_socket=...), ideal when the gateway exposes its Modbus/TCP frontend via a local socket with stricter filesystem ACLs than network ACLs.
Error Handling
All errors derive from ModbusClientError:
TransportError: socket/serial open failures, I/O timeouts, CRC/MBAP framing issues, TLS negotiation problems.ConnectionClosed: attempts toexecute()without callingconnect()first.ProtocolError: decoded response does not match the expected type or violates struct/payload expectations.ModbusServerError: server-side exception responses (function code ORed with0x80); exposes both the function and Modbus exception code for logging or policy decisions.
The high-level helpers raise these exceptions directly, so callers can implement retries or alerting around the specific failure domains.
Relationship to the Gateway
While the library can talk to any ModBus device, it is primarily intended as the canonical client for the Modbus Gateway itself. Using the shared PDU classes keeps the gateway routing tests deterministic, makes MQTT mirroring semantics easier to validate, and ensures configuration changes in the daemon can be rehearsed via this standalone package before deploying to hardware.
Metadata
Release files for modbusgw-client 0.0.2
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 | |
|---|---|---|---|---|
| modbusgw_client-0.0.2.tar.gz | 12.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modbusgw_client-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.3 kB
Release files / modbusgw_client-0.0.2.tar.gz
| Download URL | modbusgw_client-0.0.2.tar.gz |
|---|---|
| Size | 12.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d9adf8a816df00ba02a4aa9a1ea9567b453050627702380c30f30e267e6fb7b
|
|
BLAKE2b-256 checksum How to use checksums |
fec95fa15865df7da185e08e60071f273fc6a12d59add214d7ce3b523fdbbb92
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.13
|
Release files / modbusgw_client-0.0.2-py3-none-any.whl
| Download URL | modbusgw_client-0.0.2-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d60788125bf6276b8d3018cff1a102da866c75816f97f1c514651f4c6968fd54
|
|
BLAKE2b-256 checksum How to use checksums |
6d8677a49a6824ef6a28de66fa6cec7cfecb650f5625b6ceb36367ebf5ba95f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.13
|
Security for modbusgw-client 0.0.2
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.