Metadata-Version: 2.1
Name: micropython-modbus
Version: 1.2.0
Summary: MicroPython ModBus TCP and RTU library supporting client and host mode
Home-page: https://github.com/brainelectronics/micropython-modbus
Author: brainelectronics
Author-email: info@brainelectronics.de
License: MIT
Project-URL: Bug Reports, https://github.com/brainelectronics/micropython-modbus/issues
Project-URL: Source, https://github.com/brainelectronics/micropython-modbus
Keywords: micropython,modbus,tcp,rtu,client,host,library
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Description-Content-Type: text/markdown
License-File: LICENSE

# MicroPython Modbus library

[![Downloads](https://pepy.tech/badge/micropython-modbus)](https://pepy.tech/project/micropython-modbus)
![Release](https://img.shields.io/github/v/release/brainelectronics/micropython-modbus?include_prereleases&color=success)
![MicroPython](https://img.shields.io/badge/micropython-Ok-green.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![CI](https://github.com/brainelectronics/micropython-modbus/actions/workflows/release.yml/badge.svg)](https://github.com/brainelectronics/micropython-modbus/actions/workflows/release.yml)

MicroPython ModBus TCP and RTU library supporting client and host mode

---------------

## General

Forked from [Exo Sense Py][ref-sferalabs-exo-sense], based on
[PyCom Modbus][ref-pycom-modbus] and extended with other functionalities to
become a powerfull MicroPython library

<!-- MarkdownTOC -->

- [Quickstart](#quickstart)
    - [Install package on board with pip](#install-package-on-board-with-pip)
    - [Install additional MicroPython packages](#install-additional-micropython-packages)
- [Usage](#usage)
    - [Master implementation](#master-implementation)
    - [Slave implementation](#slave-implementation)
    - [Register configuration](#register-configuration)
- [Supported Modbus functions](#supported-modbus-functions)
- [Credits](#credits)

<!-- /MarkdownTOC -->

## 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](SETUP.md)

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

pip install 'rshell>=0.0.30,<1.0.0'
```

### Install package on board with pip

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

Inside the rshell

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

Inside the REPL

```python
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`](boot.py) and
[`main.py`](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

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

or check the README of the
[brainelectronics MicroPython modules][ref-github-be-mircopython-modules]

## Usage

See also [USAGE](USAGE.md)

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`](main.py).

### Master implementation

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

```python
# 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](examples/tcp_client_example.py) and
[Modbus RTU Client example](examples/rtu_client_example.py)

Both examples are using [example register definitions](examples/example.json)

Use the provided example scripts [read RTU](examples/read_registers_rtu.sh) or
[read TCP](examples/read_registers_tcp.sh) to read the data from the devices.
This requires the [modules submodule][ref-github-be-python-modules] to be
cloned as well and the required packages being installed as described in the
modules README file. For further details read the [SETUP](SETUP.md) 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`](main.py).

As an [example the registers](registers/modbusRegisters-MyEVSE.json) of a
[brainelectronics MyEVSE][ref-myevse-be], [MyEVSE on Tindie][ref-myevse-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][ref-giampiero7] for the initial implementation
of this library.

* **sfera-labs** - *Initial work* - [giampiero7][ref-sferalabs-exo-sense]
* **pycom** - *Initial Modbus work* - [pycom-modbus][ref-pycom-modbus]

<!-- Links -->
[ref-sferalabs-exo-sense]: https://github.com/sfera-labs/exo-sense-py-modbus
[ref-pycom-modbus]: https://github.com/pycom/pycom-modbus
[ref-remote-upy-shell]: https://github.com/dhylands/rshell
[ref-github-be-mircopython-modules]: https://github.com/brainelectronics/micropython-modules
[ref-github-be-python-modules]: https://github.com/brainelectronics/python-modules
[ref-myevse-be]: https://brainelectronics.de/
[ref-myevse-tindie]: https://www.tindie.com/stores/brainelectronics/
[ref-giampiero7]: https://github.com/giampiero7


