Unified API for Modbus communication
Project description
modapi
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/modapi.git cd modapi 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
modapi rc 0 8 # Read 8 coils starting at address 0
modapi wc 0 1 # Write value 1 to coil at address 0
modapi rh 0 5 # Read 5 holding registers starting at address 0
# With options
modapi -v rc 0 8 # Verbose mode
modapi -p /dev/ttyACM0 wc 0 1 # Specify port
modapi --scan # Scan for Modbus devices
# Interactive mode
modapi --interactive
REST API
from modapi 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 modapi 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
modapi 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.8.tar.gz.
File metadata
- Download URL: modapi-0.1.8.tar.gz
- Upload date:
- Size: 22.7 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 |
2258174a4084502c7dfc2f96615bb7a88a52695b4eb2abdf34068473e17a1bfe
|
|
| MD5 |
16373c5175e611e3c1323aa85d36bb74
|
|
| BLAKE2b-256 |
415ddead7b7bc8b0e31a2d5c4c7077c1f203405d1bf91c2b0ce31ef5d615137c
|
File details
Details for the file modapi-0.1.8-py3-none-any.whl.
File metadata
- Download URL: modapi-0.1.8-py3-none-any.whl
- Upload date:
- Size: 25.8 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 |
3fafd5849881540fb0bda034b07c1edb9f5010205865219658960eeb3e898958
|
|
| MD5 |
4f1b75e2d210fde0e5d82575d081598d
|
|
| BLAKE2b-256 |
d6e7f56a357e65444f4a61856efa1af4515f2680de7de38cdd53fd97fad717f0
|