Skip to main content

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.

  1. Install Poetry if you haven't already:

    curl -sSL https://install.python-poetry.org | python3 -
    
  2. 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
    
  3. 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 status
  • GET /api/coils/<address> - Read single coil
  • GET /api/coils/<address>/<count> - Read multiple coils
  • POST /api/coils/<address> - Write single coil
  • POST /api/toggle/<address> - Toggle coil state
  • GET /api/discrete_inputs/<address>/<count> - Read discrete inputs
  • GET /api/holding_registers/<address>/<count> - Read holding registers
  • POST /api/holding_registers/<address> - Write holding register
  • GET /api/input_registers/<address>/<count> - Read input registers
  • GET /api/scan - Scan for Modbus devices
  • GET /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 coils
  • modbus/command/write_coil/<address> - Write coil
  • modbus/command/toggle_coil/<address> - Toggle coil
  • modbus/command/read_discrete_input/<address>/<count> - Read discrete inputs
  • modbus/command/read_holding_register/<address>/<count> - Read holding registers
  • modbus/command/write_holding_register/<address> - Write holding register
  • modbus/command/read_input_register/<address>/<count> - Read input registers
  • modbus/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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modapi-0.1.8.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

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

modapi-0.1.8-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

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

Hashes for modapi-0.1.8.tar.gz
Algorithm Hash digest
SHA256 2258174a4084502c7dfc2f96615bb7a88a52695b4eb2abdf34068473e17a1bfe
MD5 16373c5175e611e3c1323aa85d36bb74
BLAKE2b-256 415ddead7b7bc8b0e31a2d5c4c7077c1f203405d1bf91c2b0ce31ef5d615137c

See more details on using hashes here.

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

Hashes for modapi-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3fafd5849881540fb0bda034b07c1edb9f5010205865219658960eeb3e898958
MD5 4f1b75e2d210fde0e5d82575d081598d
BLAKE2b-256 d6e7f56a357e65444f4a61856efa1af4515f2680de7de38cdd53fd97fad717f0

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