Skip to main content

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.

  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/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
    
  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
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 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 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 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

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


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.5.tar.gz (21.3 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.5-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

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

Hashes for modapi-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9835c5618693a92196db81814555b8f662a8a0c94fcfe7186be02f3c8e985e5b
MD5 77b5130399e21ccb778be1405dcaee27
BLAKE2b-256 a1cabde9fcfea00f1bc05a1fd6946f52df719130b47d1d63e97b9e9983fdaaef

See more details on using hashes here.

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

Hashes for modapi-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9c5bb3d8353396f2eb09b0d0f3da8bc9e19f7570106f830abdd51c9452155591
MD5 dc22f13fba78fa3b9a1009c6da36081a
BLAKE2b-256 3437d7c7932d9e3f4040a87e7bc1f8e77d4124e95dd6c3163230b371cdf45ad0

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