Skip to main content
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 https://img.shields.io/pypi/pyversions/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 (both TCP and RTU) and a Modbus server (both TCP and RTU). 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 TCP server is easy:

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

from umodbus import conf
from umodbus.server.tcp import RequestHandler, get_server
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_tcp_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 functions have been implemented for Modbus TCP and Modbus RTU:

  • 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.

License

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

Release files for uModbus 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uModbus 1.0.4
File Size Uploaded
uModbus-1.0.4.tar.gz 19.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uModbus 1.0.4
File Interpreter ABI Platform
uModbus-1.0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 50.9 kB

Release files / uModbus-1.0.4.tar.gz

Download URL uModbus-1.0.4.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
26bbbeff02d6d8a3e29bb0f9d9044c672d55fc1687afe4297a2f7d68175103a7
BLAKE2b-256 checksum
How to use checksums
c1135eb635dd6ab67fedcfe5147209135a737d33c1220fdf4ad735a224a8a684
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0

Release files / uModbus-1.0.4-py2.py3-none-any.whl

Download URL uModbus-1.0.4-py2.py3-none-any.whl
Size 31.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
aab3e61488d8bef638466687b360192ddf046a23b61a9ba3734b4f48d31efe16
BLAKE2b-256 checksum
How to use checksums
d1b9664b226d34cc5154dfd0f92ccfaa6cb03dd3d2f77951c0d67eedb74ace5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page