Skip to main content

Implementation of the Modbus protocol in pure Python.

Project description

https://travis-ci.org/AdvancedClimateSystems/uModbus.svg https://coveralls.io/repos/AdvancedClimateSystems/uModbus/badge.svg?service=github https://img.shields.io/pypi/v/uModbus.svg

uModbus

uModbus or (μModbus) is a pure Python implementation of the Modbus protocol as described in the MODBUS Application Protocol Specification V1.1b3. uModbus implements both a Modbus client (or master in Modbus language) and a Modbus server (or slave). The “u” or “μ” in the name comes from the the SI prefix “micro-”. uModbus is very small and lightweight. The source can be found on GitHub. Documentation is available at Read the Docs.

Quickstart

Creating a Modbus server is easy:

#!/usr/bin/env python
# scripts/examples/simple_data_store.py
import logging
from socketserver import TCPServer
from collections import defaultdict

from umodbus import get_server, RequestHandler, conf
from umodbus.utils import log_to_stream

# Add stream handler to logger 'uModbus'.
log_to_stream(level=logging.DEBUG)

# A very simple data store which maps addresss against their values.
data_store = defaultdict(int)

# Enable values to be signed (default is False).
conf.SIGNED_VALUES = True

TCPServer.allow_reuse_address = True
app = get_server(TCPServer, ('localhost', 502), RequestHandler)


@app.route(slave_ids=[1], function_codes=[3, 4], addresses=list(range(0, 10)))
def read_data_store(slave_id, function_code, address):
    """" Return value of address. """
    return data_store[address]


@app.route(slave_ids=[1], function_codes=[6, 16], addresses=list(range(0, 10)))
def write_data_store(slave_id, function_code, address, value):
    """" Set value for address. """
    data_store[address] = value

if __name__ == '__main__':
    try:
        app.serve_forever()
    finally:
        app.shutdown()
        app.server_close()

Doing a Modbus request requires even less code:

#!/usr/bin/env python
# scripts/examples/simple_client.py
import socket

from umodbus import conf
from umodbus.client import tcp

# Enable values to be signed (default is False).
conf.SIGNED_VALUES = True

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 502))

# Returns a message or Application Data Unit (ADU) specific for doing
# Modbus TCP/IP.
message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1])

# Response depends on Modbus function code. This particular returns the
# amount of coils written, in this case it is.
response = tcp.send_message(message, sock)

sock.close()

Features

The following Modbus functions have been implemented:

  • 01: Read Coils

  • 02: Read Discrete Inputs

  • 03: Read Holding Registers

  • 04: Read Input Registers

  • 05: Write Single Coil

  • 06: Write Single Register

  • 15: Write Multiple Coils

  • 16: Write Multiple Registers

Other featues:

  • Support for signed and unsigned register values.

Roadmap

uModbus is far from complete. The next, unordered list shows what is going to be implemented in the future:

  • Support for all Modbus functions

  • Modbus RTU

  • Use asyncio for handling of requests

  • Other Modbus ‘flavours’, so uModbus is able to handle 32 bit values.

License

uModbus software is licensed under Mozilla Public License. © 2015 Advanced Climate Systems.

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

uModbus-0.5.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

uModbus-0.5.0-py2.py3-none-any.whl (24.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file uModbus-0.5.0.tar.gz.

File metadata

  • Download URL: uModbus-0.5.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for uModbus-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e9ae722d499995d939da3abfeb5725d818a904af99fd0970c5d55dc4f8674225
MD5 8863558439df438c6e4e3c8e81181a00
BLAKE2b-256 85ebfdcbdeb4443cbe0a8615085c27aed519351771d76c311a8fe1514ef73978

See more details on using hashes here.

File details

Details for the file uModbus-0.5.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for uModbus-0.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 85b4dd2b0ae6b2a74b9bced1b99d9434d0d8e9591492fe7468e17d60e11936a7
MD5 d337a1d5a7da8841121f9a41d55c138f
BLAKE2b-256 fca3435fe883b4d9f3b7058adafbf42484c0a6ec7d3dd290d5d71ce11cb603c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page