Skip to main content

Modbus RTU helpers for FT231XS-based RS-485 HIL setups

Project description

lr-modbus

Lightweight Modbus RTU helpers tailored for FTDI-based hardware-in-the-loop setups. The package powers the CLI and automation utilities that ship with the UART<->RS-485 development board, but the components stand on their own and can be embedded into any Modbus RTU workflow.

Features

  • Blocking client with helpers for holding-register reads, single-register writes, and raw function dispatch
  • Reference server with a thread-safe in-memory data model, optional listen-only mode, and deterministic shutdown hooks
  • CRC-safe frame codec plus transactional serial transport tuned for half-duplex FTDI adapters
  • Batteries-included CLI (lr-modbus) for quick experiments, scripting, and test automation

Installation

python -m pip install lr-modbus

For development work inside this repository, install in editable mode together with the extra tooling:

python -m pip install -e python/lr_modbus[dev]

Quick Start

CLI

# Launch a server that seeds two holding registers and exits after 50 requests
lr-modbus --port /dev/ttyUSB0 server --holding 0=0x2A,1=0x1337 --max-requests 50

# Read two registers from another adapter
lr-modbus --port /dev/ttyUSB1 client --action read-holding --address 0 --count 2

# Write a single register
lr-modbus --port /dev/ttyUSB1 client --action write-single --address 5 --value 1234

The CLI targets the FT231XS + MAX1487CSA+ stack on the dev board, defaults to 115200-8N1, enforces Modbus RTU t3.5 frame gaps, and gives each transaction a 2 s deadline. That means two adapters attached to the same host can verify their RS-485 link with no extra flags, while the --timeout option lets you shrink or grow the overall window. Common overrides include:

lr-modbus --port /dev/ttyUSB0 --baudrate 19200 --stopbits 1 --timeout 2 client --action read-holding --address 0 --count 4

Use lr-modbus --help for the full option matrix, including how to seed holding registers or stop the server after a fixed number of requests for HIL scripting.

Library usage

from lr_modbus import ModbusClient, ModbusServer, InMemoryDataModel
from lr_modbus.transport import SerialDeviceConfig, create_serial_transport

config = SerialDeviceConfig(port="/dev/ttyUSB0")
transport = create_serial_transport(config)
client = ModbusClient(transport, unit_id=1)

with transport:
    registers = client.read_holding_registers(0, 4)
    client.write_single_register(10, 0xBEEF)

To host a test server in the same script:

data_model = InMemoryDataModel({0: 0x2A})
server = ModbusServer(transport, unit_id=1, data_model=data_model)
server.serve_forever(max_requests=10)

The transport exposes transact(), read_frame(), and write_frame() helpers for advanced use-cases such as bit-banging DE/RE GPIO hooks.

Testing

make test

The target wires up pytest with coverage against the lr_modbus and lr_dmx packages. Tests rely on transport factories, so no physical hardware is required.

Versioning & publishing

Package metadata lives in python/lr_modbus/pyproject.toml alongside the version marker in python/lr_modbus/lr_modbus/__init__.py. To publish a release from this repository root:

python -m pip install -e python/lr_modbus[dev]
python -m build python/lr_modbus -o dist/lr_modbus
python -m twine upload dist/lr_modbus/*

License

Released under the MIT License. Copyright (c) 2026 LumenRadio AB.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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

lr_modbus-0.2.2.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

lr_modbus-0.2.2-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file lr_modbus-0.2.2.tar.gz.

File metadata

  • Download URL: lr_modbus-0.2.2.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lr_modbus-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1ecec1bbe5f6c7ac1883e4db5644c835e9a9bc78b08f61efcdadbb2160fe93f4
MD5 7102e5f9fb348d4ca95968fe56bac8c7
BLAKE2b-256 adf5b5199bb51374ecc9bdd461d1f38582bd400eab08a6f7907ae6bf686642a8

See more details on using hashes here.

File details

Details for the file lr_modbus-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: lr_modbus-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lr_modbus-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70d4e89874bd9a18c631a3f74fa5ad16b83358eb3f9dcfd142f0404c5223b7d4
MD5 9fa6f631d5dc0d8f95ea10c42c104888
BLAKE2b-256 17252a6483197cd4df8b6efb68b2527e1cc1f01446b025b0f5eda22ddf5a9cca

See more details on using hashes here.

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