Skip to main content

MicroPython Modbus library

Downloads Release MicroPython License: MIT CI Test Python package Documentation Status

MicroPython ModBus TCP and RTU library supporting client and host mode


General

Forked from Exo Sense Py, based on PyCom Modbus and extended with other functionalities to become a powerfull MicroPython library

📚 The latest documentation is available at MicroPython Modbus ReadTheDocs 📚

Quickstart

This is a quickstart to install the micropython-modbus library on a MicroPython board.

A more detailed guide of the development environment can be found in SETUP, further details about the usage can be found in USAGE, descriptions for testing can be found in TESTING and several examples in EXAMPLES

python3 -m venv .venv
source .venv/bin/activate

pip install 'rshell>=0.0.30,<1.0.0'
pip install 'mpremote>=0.4.0,<1'

Install package on board with mip or upip

rshell -p /dev/tty.SLAB_USBtoUART --editor nano

Inside the rshell open a REPL and execute these commands inside the REPL

import machine
import network
import time
import mip
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect('SSID', 'PASSWORD')
time.sleep(1)
print('Device connected to network: {}'.format(station.isconnected()))
mip.install('github:brainelectronics/micropython-modbus')
print('Installation completed')
machine.soft_reset()

For MicroPython versions below 1.19.1 use the upip package instead of mip

import machine
import network
import time
import upip
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect('SSID', 'PASSWORD')
time.sleep(1)
print('Device connected to network: {}'.format(station.isconnected()))
upip.install('micropython-modbus')
print('Installation completed')
machine.soft_reset()

Request coil status

After a successful installation of the package and reboot of the system as described in the installation section the following commands can be used to request a coil state of a target/client device. Further usage examples can be found in the examples folder and in the USAGE chapter

TCP

from ummodbus.tcp import ModbusTCPMaster

tcp_device = ModbusTCPMaster(
    slave_ip='172.24.0.2',  # IP address of the target/client/slave device
    slave_port=502,         # TCP port of the target/client/slave device
    # timeout=5.0           # optional, timeout in seconds, default 5.0
)

# address of the target/client/slave device on the bus
slave_addr = 10
coil_address = 123
coil_qty = 1

coil_status = host.read_coils(
    slave_addr=slave_addr,
    starting_addr=coil_address,
    coil_qty=coil_qty)
print('Status of coil {}: {}'.format(coil_status, coil_address))

For further details check the latest MicroPython Modbus TCP documentation example

RTU

from umodbus.serial import Serial as ModbusRTUMaster

host = ModbusRTUMaster(
    pins=(25, 26),      # given as tuple (TX, RX), check MicroPython port specific syntax
    # baudrate=9600,    # optional, default 9600
    # data_bits=8,      # optional, default 8
    # stop_bits=1,      # optional, default 1
    # parity=None,      # optional, default None
    # ctrl_pin=12,      # optional, control DE/RE
    # uart_id=1         # optional, see port specific documentation
)

# address of the target/client/slave device on the bus
slave_addr = 10
coil_address = 123
coil_qty = 1

coil_status = host.read_coils(
    slave_addr=slave_addr,
    starting_addr=coil_address,
    coil_qty=coil_qty)
print('Status of coil {}: {}'.format(coil_address, coil_status))

For further details check the latest MicroPython Modbus RTU documentation example

Install additional MicroPython packages

To use this package with the provided boot.py and main.py file, additional modules are required, which are not part of this repo/package. To install these modules on the device, connect to a network and install them via upip as follows

# with MicroPython version 1.19.1 or newer
import mip
mip.install('github:brainelectronics/micropython-modules')

# before MicroPython version 1.19.1
import upip
upip.install('micropython-brainelectronics-helpers')

Check also the README of the brainelectronics MicroPython modules, the INSTALLATION and the SETUP guides.

Usage

See USAGE and DOCUMENTATION

Supported Modbus functions

Refer to the following table for the list of supported Modbus functions.

ID  Description
1 Read coils
2 Read discrete inputs
3 Read holding registers
4 Read input registers
5 Write single coil
6 Write single register
15 Write multiple coils
16 Write multiple registers

Credits

Big thank you to giampiero7 for the initial implementation of this library.

Metadata

Release files for micropython-modbus 2.3.7

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

Source distribution (sdist)

Source distribution for micropython-modbus 2.3.7
File Size Uploaded
micropython-modbus-2.3.7.tar.gz 26.3 kB Details

Release files / micropython-modbus-2.3.7.tar.gz

Download URL micropython-modbus-2.3.7.tar.gz
Size 26.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b9351a66af0ff29dd585900e53827a989dedcb02ec63b1041358165089652271
BLAKE2b-256 checksum
How to use checksums
a400ac6cd7ec350915f14cb0caf8fe590f181b629dcd227adc8682678e5803a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.17

Release history Release notifications | RSS feed

This release

2.3.7 This release

1 release file

2.3.6

1 release file

2.3.5

1 release file

2.3.4

1 release file

2.3.3

1 release file

2.3.2

1 release file

2.3.1

1 release file

2.3.0

1 release file

2.2.0

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.2.0

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

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