Skip to main content

MicroPython ModBus TCP and RTU library supporting client and host mode

Project description

MicroPython Modbus library

Downloads Release MicroPython License: MIT CI

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

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

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

pip install 'rshell>=0.0.30,<1.0.0'

Install package on board with pip

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

Inside the rshell

cp main.py /pyboard
cp boot.py /pyboard
repl

Inside the REPL

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()

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

import upip
upip.install('micropython-brainelectronics-helper')

or check the README of the brainelectronics MicroPython modules

Usage

See also USAGE

Start a REPL (may perform a soft reboot), wait for network connection and start performing Modbus requests to the device.

For further details about a TCP-RTU bridge implementation check the header comment of main.py.

Master implementation

Act as host, get Modbus data via RTU or TCP from a client device

# import modbus host classes
from umodbus.tcp import TCP as ModbusTCPMaster
from umodbus.serial import Serial as ModbusRTUMaster

# RTU Master setup
# act as host, get Modbus data via RTU from a client device
# ModbusRTUMaster can make serial requests to a client device to get/set data
rtu_pins = (25, 26)         # (TX, RX)
slave_addr = 10             # bus address of client
host = ModbusRTUMaster(
    baudrate=9600,          # optional, default 9600
    data_bits=8,            # optional, default 8
    stop_bits=1,            # optional, default 1
    parity=None,            # optional, default None
    pins=rtu_pins)

# TCP Master setup
# act as host, get Modbus data via TCP from a client device
# ModbusTCPMaster can make TCP requests to a client device to get/set data
host = ModbusTCPMaster(
    slave_ip=192.168.178.34,
    slave_port=180,
    timeout=5)              # optional, default 5

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

# WRITE COILS
new_coil_val = 0
operation_status = host.write_single_coil(
    slave_addr=slave_addr,
    output_address=coil_address,
    output_value=new_coil_val)
print('Result of setting coil {} to {}'.format(coil_address, operation_status))

# READ HREGS
hreg_address = 93
register_value = host.read_holding_registers(
    slave_addr=slave_addr,
    starting_addr=hreg_address,
    register_qty=1,
    signed=False)
print('Status of hreg {}: {}'.format(hreg_address, register_value))

# WRITE HREGS
new_hreg_val = 44
operation_status = host.write_single_register(
                    slave_addr=slave_addr,
                    register_address=hreg_address,
                    register_value=new_hreg_val,
                    signed=False)
print('Result of setting hreg {} to {}'.format(hreg_address, operation_status))

# READ ISTS
ist_address = 67
input_status = host.read_discrete_inputs(
    slave_addr=slave_addr,
    starting_addr=ist_address,
    input_qty=1)
print('Status of ist {}: {}'.format(ist_address, input_status))

# READ IREGS
ireg_address = 10
register_value = host.read_input_registers(
                    slave_addr=slave_addr,
                    starting_addr=ireg_address,
                    register_qty=2,
                    signed=False)
print('Status of ireg {}: {}'.format(ireg_address, register_value))

Slave implementation

Act as client, provide Modbus data via RTU or TCP to a host device.

See Modbus TCP Client example and Modbus RTU Client example

Both examples are using example register definitions

Use the provided example scripts read RTU or read TCP to read the data from the devices. This requires the modules submodule to be cloned as well and the required packages being installed as described in the modules README file. For further details read the SETUP guide.

Register configuration

The available registers are defined by a JSON file, placed inside the /pyboard/registers folder on the board and selected in main.py.

As an example the registers of a brainelectronics MyEVSE, MyEVSE on Tindie board is provided with this repo.

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.

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

micropython-modbus-1.2.0.tar.gz (18.2 kB view hashes)

Uploaded Source

Supported by

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