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 = gw.add_tcp_downstream("192.168.1.10", 502)
    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.

Migration Guide

Detailed step-by-step migration guides are available in the Migration Guides directory.

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.15.2.tar.gz (5.3 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.15.2-cp311-cp311-win_amd64.whl (478.0 kB view details)

Uploaded CPython 3.11Windows x86-64

modbus_rs-0.15.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (628.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

modbus_rs-0.15.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (665.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

modbus_rs-0.15.2-cp311-cp311-macosx_11_0_arm64.whl (539.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

modbus_rs-0.15.2-cp311-cp311-macosx_10_12_x86_64.whl (521.7 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: modbus_rs-0.15.2.tar.gz
  • Upload date:
  • Size: 5.3 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.15.2.tar.gz
Algorithm Hash digest
SHA256 55fe40a3ec0bd74d6950ae8ea488a6f6719520fde465d450730a4bb652c1710a
MD5 08a83f1247d0baacc69dbe2d927fa674
BLAKE2b-256 34d190a2532dd15541628e8cf518612a2ab42d69309b1fc64974df1edbaae60e

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.15.2.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.15.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: modbus_rs-0.15.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 478.0 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.15.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ff6782be3c58855d405e982d8e41c80bcf6ff7f584faf1d4c2594142a2acf57e
MD5 8e468be7c4c2ac9acbd6b26ba5146fd0
BLAKE2b-256 3b75e26775e9d1e337cb699a07276cd44f1054ca19ba2d450913f8c9d5501f36

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.15.2-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.15.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.15.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 676eb521d14301d8367907a2f3bdc97a3aee67a0b1e6174b42b4276d8654d55c
MD5 92e15f840c4c51fca24f9ae0fc9f5672
BLAKE2b-256 42253e9cb922b228e9b72c9dfb434b0bbffd7179ba0806c0e66cca76648ea67e

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.15.2-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.15.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.15.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4c4fbf7ce27fbebb55dc5d50bbb33176d34bcd780de9b1db03f8fec5514dbd60
MD5 aaba42dd183a97420dcb748d296aed3b
BLAKE2b-256 4f9f9c235e301f68bfa8243be513912ec7ebde93286c0608e26b0a20e84514d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.15.2-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.15.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.15.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 36bff507c2142e850137412e22c8f0bee23a9ba6e1b71ae2599be88503ca18bf
MD5 78ec7053267a9aba310233fb03712f9c
BLAKE2b-256 5f6625c21b0acb3e15097e790ebff092d88a627ff3677a5d7d2f540a044096f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.15.2-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.15.2-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for modbus_rs-0.15.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 762f4973282057c0bbff6759338e011fde5caa8a8d25455e43266da8cc9e7d28
MD5 3ae3a7c9cee6c657b17dba0299e6be78
BLAKE2b-256 9d878edd4496d8c71bc14e2ce197e33f5995ac4969b8339fc8ec546b9438e853

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_rs-0.15.2-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