Unified API for Modbus communication
Project description
ModbusAPI
Unified API for Modbus communication with multiple interfaces: Shell CLI, REST API, and MQTT.
Features
- Modbus RTU Client - Core functionality for communicating with Modbus devices
- Auto-detection - Automatically detect Modbus devices on serial ports
- Multiple APIs:
- Shell CLI - Command line interface for direct Modbus operations
- REST API - HTTP API for web applications
- MQTT API - MQTT interface for IoT applications
- Interactive Mode - Interactive shell for manual Modbus operations
- JSON Output - Structured JSON output for easy parsing
Installation
This project uses Poetry for dependency management.
-
Install Poetry if you haven't already:
curl -sSL https://install.python-poetry.org | python3 -
-
Clone the repository and install dependencies:
git clone https://github.com/yourusername/modbusapi.git cd modbusapi poetry install # Install all dependencies # Or install with specific groups: poetry install --only main,rest # Only REST API poetry install --only main,mqtt # Only MQTT API poetry install --with dev # Development tools
-
Activate the virtual environment:
poetry shell
Development
-
Install development dependencies:
poetry install --with dev
-
Run tests:
poetry run pytest
-
Run with coverage:
poetry run pytest --cov=modapi tests/
Building and Publishing
-
Build the package:
poetry build -
Publish to PyPI:
poetry publish --build
Usage
Shell CLI
# Basic commands
modbusapi rc 0 8 # Read 8 coils starting at address 0
modbusapi wc 0 1 # Write value 1 to coil at address 0
modbusapi rh 0 5 # Read 5 holding registers starting at address 0
# With options
modbusapi -v rc 0 8 # Verbose mode
modbusapi -p /dev/ttyACM0 wc 0 1 # Specify port
modbusapi --scan # Scan for Modbus devices
# Interactive mode
modbusapi --interactive
REST API
from modbusapi import create_rest_app
# Create and run Flask app
app = create_rest_app(port='/dev/ttyACM0', api_port=5000)
app.run_server()
REST API Endpoints
GET /api/status- Get Modbus connection statusGET /api/coils/<address>- Read single coilGET /api/coils/<address>/<count>- Read multiple coilsPOST /api/coils/<address>- Write single coilPOST /api/toggle/<address>- Toggle coil stateGET /api/discrete_inputs/<address>/<count>- Read discrete inputsGET /api/holding_registers/<address>/<count>- Read holding registersPOST /api/holding_registers/<address>- Write holding registerGET /api/input_registers/<address>/<count>- Read input registersGET /api/scan- Scan for Modbus devicesGET /api/docs- Get API documentation
MQTT API
from modbusapi import start_mqtt_broker
# Start MQTT client
client = start_mqtt_broker(
port='/dev/ttyACM0',
mqtt_broker='localhost',
mqtt_port=1883,
mqtt_topic_prefix='modbus'
)
MQTT Topics
modbus/command/read_coil/<address>/<count>- Read coilsmodbus/command/write_coil/<address>- Write coilmodbus/command/toggle_coil/<address>- Toggle coilmodbus/command/read_discrete_input/<address>/<count>- Read discrete inputsmodbus/command/read_holding_register/<address>/<count>- Read holding registersmodbus/command/write_holding_register/<address>- Write holding registermodbus/command/read_input_register/<address>/<count>- Read input registersmodbus/status- Connection status
Configuration
ModbusAPI can be configured using environment variables or directly in code:
# .env file
MODBUS_PORT=/dev/ttyACM0
MODBUS_BAUDRATE=9600
MODBUS_TIMEOUT=1.0
MODBUS_DEVICE_ADDRESS=1
Development
# Install development dependencies
pip install -e .[dev]
# Run tests
pytest
License
Apache 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modapi-0.1.5.tar.gz.
File metadata
- Download URL: modapi-0.1.5.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.8-200.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9835c5618693a92196db81814555b8f662a8a0c94fcfe7186be02f3c8e985e5b
|
|
| MD5 |
77b5130399e21ccb778be1405dcaee27
|
|
| BLAKE2b-256 |
a1cabde9fcfea00f1bc05a1fd6946f52df719130b47d1d63e97b9e9983fdaaef
|
File details
Details for the file modapi-0.1.5-py3-none-any.whl.
File metadata
- Download URL: modapi-0.1.5-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.15.8-200.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c5bb3d8353396f2eb09b0d0f3da8bc9e19f7570106f830abdd51c9452155591
|
|
| MD5 |
dc22f13fba78fa3b9a1009c6da36081a
|
|
| BLAKE2b-256 |
3437d7c7932d9e3f4040a87e7bc1f8e77d4124e95dd6c3163230b371cdf45ad0
|