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

Synchronous TCP client

import modbus_rs

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

Async TCP client

import asyncio
import modbus_rs

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

asyncio.run(main())

Serial client (RTU)

import modbus_rs

with modbus_rs.SerialClient("/dev/ttyUSB0", baud_rate=9600, unit_id=1) as client:
    client.connect()
    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 from Source

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

Run Python Tests

cd mbus-ffi
maturin develop --features python,full
cd ..
pytest mbus-ffi/tests/python/ -q

Run Python Examples

The examples live in this repository under mbus-ffi/examples.

1) Build/install the extension from source

git clone https://github.com/Raghava-Ch/modbus-rs.git
cd modbus-rs/mbus-ffi
maturin develop --features python,full

2) Start the example server (terminal 1)

cd ../examples/python_server
python3 python_server.py --host 127.0.0.1 --port 5020 --unit-id 1

3) Run the sync client (terminal 2)

cd ../python_client
python3 python_client.py --host 127.0.0.1 --port 5020 --unit-id 1

4) Run the async client (terminal 2)

cd ../python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --unit-id 1

Optional: multi-server async demo

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

cd ../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:

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

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.12.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.12.0-cp311-cp311-win_amd64.whl (469.5 kB view details)

Uploaded CPython 3.11Windows x86-64

modbus_rs-0.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (615.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

modbus_rs-0.12.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (656.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

modbus_rs-0.12.0-cp311-cp311-macosx_11_0_arm64.whl (537.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

modbus_rs-0.12.0-cp311-cp311-macosx_10_12_x86_64.whl (517.7 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: modbus_rs-0.12.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.12.0.tar.gz
Algorithm Hash digest
SHA256 a3334a5daa9e61a13ffab17ef68974fdd18419a4725cc9f45385e1195073ac71
MD5 0c0e9c444b8d3604b556910cc9ccd752
BLAKE2b-256 8dbc029a12179151d445e0a441e54d98480b43833639c77671b878687a4747b8

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: modbus_rs-0.12.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 469.5 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.12.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a86b3e09663dd94f186717fdb35ab0a363bd1e33c18a3edfd9937134a381815e
MD5 7feadde342fbfa58b7d3f5e7ed279b58
BLAKE2b-256 f719f9f97ec3db2d9de6376ef33fb323e4148f0b246f3351c8002daf322b0a87

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for modbus_rs-0.12.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9026eaf464ac6744dad4b68edaa7aaf404ec44af6571fab1812f7ef32885b8d2
MD5 1f28411f97d18f3b9142d5bff35504a4
BLAKE2b-256 0ed5098e4341c96741124616bb85b52d806d540f533a0011deab7392feee075c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for modbus_rs-0.12.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 aab556bcfebd1033324e826c230884035308cfba7b0bf66bd5ec6b23a21cddb4
MD5 174b5aff749ac74c693c9bf1906216b6
BLAKE2b-256 3151dd8049166318e1f9021505904c5a5d7efaeb6dda3d2aa76b54f8a0f3da63

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for modbus_rs-0.12.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c48825cd660ef64d3e98704b5e62ef7388dc2f2b6c5b2313e35a4452c854cad2
MD5 d05d7cff10dd001aa3c0070127334cb3
BLAKE2b-256 ce8b16f5c71dc907eb7fc79055ef02fb66d88b909817bd24b8be63f7ec3b64db

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for modbus_rs-0.12.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9672bbb77f013f9741543584be62da319287d082c90b5eeaf58dbb0970cb4336
MD5 56add875958db561b7b49b6b91524ad2
BLAKE2b-256 d9fc7119987c8519105a4aeb54d240dc8eb533f22d62c872ac3b8f9e57da380c

See more details on using hashes here.

Provenance

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