Skip to main content

Fast, full-featured Modbus TCP + Serial bindings for Python — powered by Rust

Project description

modbus-rs (Python)

Fast Modbus TCP + Serial bindings for Python, powered by Rust.

  • PyPI package: modbus-rs
  • Import name: modbus_rs

Licensing

This package is available under GNU GPL v3.0 for open-source use.

Commercial licenses are also available for proprietary/closed-source use. Contact: ch.raghava44@gmail.com

Install

pip install modbus-rs

Quick Start

Example codes: modbus-rs/blob/main/mbus-ffi/python/examples

Synchronous TCP client

import modbus_rs

with modbus_rs.TcpTransport.connect("192.168.1.10", port=502) as transport:
    client = transport.create_client(unit_id=1)
    regs = client.read_holding_registers(0, 10)
    print(regs)

Async TCP client

import asyncio
import modbus_rs

async def main():
    async with await modbus_rs.AsyncTcpTransport.connect("192.168.1.10") as transport:
        client = transport.create_client(unit_id=1)
        regs = await client.read_holding_registers(0, 10)
        print(regs)

asyncio.run(main())

Serial client (RTU)

import modbus_rs

with modbus_rs.RtuTransport.open("/dev/ttyUSB0", baud_rate=9600) as transport:
    client = transport.create_client(unit_id=1)
    regs = client.read_holding_registers(0, 5)
    print(regs)

Async TCP server

import asyncio
import modbus_rs

class MyApp(modbus_rs.ModbusApp):
    def handle_read_holding_registers(self, address, count):
        return [address + i for i in range(count)]

async def main():
    server = modbus_rs.AsyncTcpServer("0.0.0.0", MyApp(), port=5020, unit_id=1)
    await server.serve_forever()

asyncio.run(main())

Exceptions

  • ModbusError
  • ModbusTimeout
  • ModbusConnectionError
  • ModbusProtocolError
  • ModbusDeviceException
  • ModbusConfigError
  • ModbusInvalidArgument

Build and Test Locally

To develop the Python bindings locally, create a virtual environment, activate it, build the bindings using Maturin, and run the tests.

1) Set up a virtual environment

Create a Python virtual environment at the repository root to isolate dependencies:

# Create the virtual environment
python3 -m venv .venv

# Activate the virtual environment
# On macOS / Linux:
source .venv/bin/activate
# On Windows (Command Prompt):
.venv\Scripts\activate.bat
# On Windows (PowerShell):
.venv\Scripts\Activate.ps1

Once activated, your terminal prompt will be prefixed with (.venv). To deactivate the virtual environment when you are done, run:

deactivate

2) Install build/test dependencies

pip install --upgrade pip
pip install maturin pytest pytest-asyncio

3) Compile and install the bindings in development mode

From the repository root, change to the mbus-ffi directory and compile the package.

The Python bindings features are modular:

  • python-client — Enables Modbus client transports and clients.
  • python-server — Enables Modbus server classes and apps.
  • python-gateway — Enables TCP gateway classes (requires python-client).
  • python-full — Convenience alias that enables all client, server, and gateway features.

To compile with all features enabled:

cd mbus-ffi
maturin develop --features python-full

4) Run Python tests

Run pytest:

pytest tests/python/ -v

Run Python Examples

The examples live in this repository under mbus-ffi/python/examples/. Before running them, make sure your virtual environment is activated and the extension is built.

1) Build/install the extension from source

Ensure you are in the repository root, activate the virtual environment, and compile the package:

source .venv/bin/activate   # or Windows equivalent
cd mbus-ffi
maturin develop --features python-full

2) Start the example server (terminal 1)

Run the server from the repository root (make sure the virtual environment is active):

source .venv/bin/activate
cd mbus-ffi/python/examples/python_server
python3 python_server.py --host 127.0.0.1 --port 5020 --unit-id 1

3) Run the sync client (terminal 2)

Run the client from the repository root:

source .venv/bin/activate
cd mbus-ffi/python/examples/python_client
python3 python_client.py --host 127.0.0.1 --port 5020 --unit-id 1

4) Run the async client (terminal 2)

Run the async client from the repository root:

source .venv/bin/activate
cd mbus-ffi/python/examples/python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --unit-id 1

5) Run multi-unit examples (terminal 2)

Verify the new transport/client split by running one of the multi-unit/transport examples from the repository root:

source .venv/bin/activate
cd mbus-ffi/python/examples
python3 11-tcp-transport-multi-unit.py --host 127.0.0.1 --port 5020

Optional: multi-server async demo

Start 3 servers on ports 5020, 5021, and 5022, then run:

source .venv/bin/activate
cd mbus-ffi/python/examples/python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --multi

Modbus TCP Gateway (python-gateway feature)

The python-gateway feature exposes a thread-safe sync gateway and an asyncio-friendly async gateway that forward inbound Modbus/TCP requests to one or more downstream Modbus/TCP servers based on a unit-id routing table.

Build with the gateway feature enabled (or use the complete python-full suite):

cd mbus-ffi
maturin develop --features python-client,python-gateway

Sync gateway

import modbus_rs

gw = modbus_rs.TcpGateway("0.0.0.0:5020")
ch = gw.add_tcp_downstream("192.168.1.10", 502)
gw.add_unit_route(unit=1, channel=ch)
gw.serve_forever()  # blocks; call gw.stop() from another thread to exit

Async gateway

import asyncio
import modbus_rs

async def main():
    gw = modbus_rs.AsyncTcpGateway("0.0.0.0:5020")
    ch = await gw.add_tcp_downstream("192.168.1.10", 502)
    await gw.add_unit_route(unit=1, channel=ch)
    await gw.serve_forever()  # cancel the task or call gw.stop() to exit

asyncio.run(main())

Note: The optional event_handler= constructor argument accepts a GatewayEventHandler subclass to receive telemetry callbacks for routing, forwarding, and errors. See event_handler_demo.py for a complete example of logging telemetry events.

More Docs

  • Project docs: documentation/python_bindings.md
  • Full crate README (C/WASM/Python): mbus-ffi/README.md

License

Copyright (C) 2026 Raghava Challari

This project is licensed under GNU GPL v3.0. See LICENSE for details.

Commercial licenses for proprietary use are available via ch.raghava44@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modbus_rs-0.14.0.tar.gz (5.4 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

modbus_rs-0.14.0-cp311-cp311-win_amd64.whl (479.1 kB view details)

Uploaded CPython 3.11Windows x86-64

modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (620.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (657.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

modbus_rs-0.14.0-cp311-cp311-macosx_11_0_arm64.whl (535.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

modbus_rs-0.14.0-cp311-cp311-macosx_10_12_x86_64.whl (520.4 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file modbus_rs-0.14.0.tar.gz.

File metadata

  • Download URL: modbus_rs-0.14.0.tar.gz
  • Upload date:
  • Size: 5.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_rs-0.14.0.tar.gz
Algorithm Hash digest
SHA256 23ee0927bbee8ad5b178f1b0d47d74848d7d57b2d6ea3177093895bacc18cdf2
MD5 0115cacb7721b0cd37213a1a9a5c9751
BLAKE2b-256 679151251ef50fbac764e4261384a3b28f4f73c410b9cd65eccc5c051ca99ecc

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.14.0.tar.gz:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modbus_rs-0.14.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: modbus_rs-0.14.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 479.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_rs-0.14.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 04df0c651cf0f1af52f64da5bd4b859388d0aedf2ab3a63465ea44171ae5f450
MD5 31d1ed5aaab87e0419a6813718057ef0
BLAKE2b-256 511c0dca093cecee8967a5fe0f344c101b4e8501fe73ed77672132df718e01dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.14.0-cp311-cp311-win_amd64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1e4251b8bb0532bf1e74db8b65b81b5c8ec146bd9af553dca247e56192fc7f1b
MD5 fe46706de352933bb53080a74c8b2066
BLAKE2b-256 76314e24d6029d47ec031f0bdd27cb689cc5f05ff222f9bb36faf090fbd2c311

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f174f1b03db66ce9625d17fa74effa980ab4688c70697b6df0c1be2598d6f650
MD5 515fc0236193dea1b5bd49f4099e7b25
BLAKE2b-256 ebfec0bfdd17281b27ce989fc8795ac5595e81992fe2b85447debe37e43a8b76

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.14.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modbus_rs-0.14.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.14.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 612dae0e38bceedeedabe40d6d5d48508e04cf1152e7c70c5d64bb69ee220fa9
MD5 2a179e19d538c7d502d35568a4adbeaa
BLAKE2b-256 b2a65431757914437ad307e3d1b263bd723ceea958fcccff4a9a3367bb7f6262

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.14.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modbus_rs-0.14.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.14.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 785645c32dd5af1c7561a1d8ac13a39851f9797356b5d0782f2192e4f27ae050
MD5 f27be6ec5c104970f6e2bab7c54987ea
BLAKE2b-256 5cd836f3de384265658dd207c780c30b7ad95b620b0a1b0e86b6cf3ef2a3a63b

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.14.0-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: release-python.yml on Raghava-Ch/modbus-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page