Skip to content

Latest commit

 

History

History
215 lines (162 loc) · 6.23 KB

README.md

File metadata and controls

215 lines (162 loc) · 6.23 KB

Micropython Modbus library

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


Installation

The current implementation does only run on a board with external SPI RAM. As of now up to 300kB of RAM are required. This is more than an ESP32-D4 Pico provides by default.

esp32spiram-20220117-v1.18.bin is used as MicroPython firmware

This repo contains submodules (even sub-submodules), sorry for that. Perform the following steps to clone and update everything to gain the best usage experience:

# clone this repo with all submodules
git clone --recurse-submodules https://github.com/brainelectronics/micropython-modbus.git

# init and update all submodules
git submodules update --init --recursive

# enter micropython helper modules submodule
cd helpers
# check available tags
git tag --list
# checkout the latest non breaking tag, choose manually
git checkout x.y.z
# return to root of repo
cd ..

# enter python modules submodule
cd modules
# check available tags
git tag --list
# checkout the latest non breaking tag, choose manually
git checkout x.y.z
# return to root of repo
cd ..

# enter WiFi Manager submodule
cd wifi-manager
# check available tags
git tag --list
# checkout the latest non breaking tag, choose manually
git checkout x.y.z
# return to root of repo
cd ..

Install required tools

Python3 must be installed on your system. Check the current Python version with the following command

python --version
python3 --version

Depending on which command Python 3.x.y (with x.y as some numbers) is returned, use that command to proceed.

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

pip install -r requirements.txt

Copy files

For interaction with the filesystem of the device the Remote MicroPython shell can be used.

Open the remote shell with the following command. Additionally use -b 115200 in case no CP210x is used but a CH34x.

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

Perform the following command to copy all files and folders to the device

mkdir /pyboard/helpers
mkdir /pyboard/lib
mkdir /pyboard/primitives
mkdir /pyboard/registers
mkdir /pyboard/static/
mkdir /pyboard/templates

cp helpers/*.py /pyboard/helpers
cp -r lib/* /pyboard/lib/
cp -r wifi-manager/lib/* /pyboard/lib
cp wifi-manager/primitives/*.py /pyboard/primitives
cp registers/modbusRegisters-MyEVSE.json /pyboard/register/
cp wifi-manager/simulation/static/css/*.gz /pyboard/static/
cp wifi-manager/templates/* /pyboard/templates

cp wifi-manager/wifi_manager.py /pyboard
cp modbus.py /pyboard
cp main.py /pyboard
cp boot.py /pyboard

Usage

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

For further details check the header comment of main.py.

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.