This page covers Python usage for the modbus-rs stack through the mbus-ffi crate.
Package name on PyPI: modbus-rs
Import name in Python: modbus_rs
pip install modbus-rsFor local development from this repository:
cd mbus-ffi
maturin develop --features python,fullimport modbus_rs
with modbus_rs.TcpClient("127.0.0.1", port=502, unit_id=1) as client:
client.connect()
regs = client.read_holding_registers(0, 5)
print(regs)import asyncio
import modbus_rs
async def main():
async with modbus_rs.AsyncTcpClient("127.0.0.1", port=502, unit_id=1) as client:
regs = await client.read_holding_registers(0, 5)
print(regs)
asyncio.run(main())import asyncio
import modbus_rs
class App(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", App(), port=5020, unit_id=1)
await server.serve_forever()
asyncio.run(main())Python bindings support RTU and ASCII serial transports.
Typical serial constructor parameters include:
- port
- baud_rate
- unit_id
- mode (rtu or ascii, or SerialMode enum)
- timeout_ms
- data_bits
- parity
- stop_bits
- retry_attempts
Primary exception hierarchy:
- ModbusError
- ModbusTimeout
- ModbusConnectionError
- ModbusProtocolError
- ModbusDeviceException
- ModbusConfigError
- ModbusInvalidArgument
Use ModbusDeviceException when handling device-returned Modbus exception codes.
Complete runnable examples:
- mbus-ffi/examples/python_client/python_client.py
- mbus-ffi/examples/python_async_client/async_client.py
- mbus-ffi/examples/python_server/python_server.py
Example guides:
- mbus-ffi/examples/python_client/README.md
- mbus-ffi/examples/python_async_client/README.md
- mbus-ffi/examples/python_server/README.md
From repository root:
cd mbus-ffi
maturin develop --features python,fullStart the server in terminal 1:
cd mbus-ffi/examples/python_server
python3 python_server.py --host 127.0.0.1 --port 5020 --unit-id 1Run the sync client in terminal 2:
cd mbus-ffi/examples/python_client
python3 python_client.py --host 127.0.0.1 --port 5020 --unit-id 1Run the async client in terminal 2:
cd mbus-ffi/examples/python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --unit-id 1Optional multi-server async demo:
cd mbus-ffi/examples/python_async_client
python3 async_client.py --host 127.0.0.1 --port 5020 --multiRun Python tests from repository root:
cd mbus-ffi && maturin develop --features python,full
cd ..
pytest mbus-ffi/tests/python/ -qNote: Running pytest without installing the extension first will fail collection with an import error for modbus_rs._modbus_rs.
- mbus-ffi/README.md for packaging details and release workflow
- mbus-ffi/src/python for binding source implementation