Package: micropython-modbus
MicroPython Modbus libraryMicroPython ModBus TCP and RTU library supporting client and host modeGeneralForked fromExo Sense Py, based onPyCom Modbusand extended with other functionalities to
become a powerfull MicroPython library📚 The latest documentation is available atMicroPython Modbus ReadTheDocs📚QuickstartInstall package on board with mip or upipRequest coil statusTCPRTUInstall additional MicroPython packagesUsageSupported Modbus functionsCreditsQuickstartThis is a quickstart to install themicropython-modbuslibrary on a
MicroPython board.A more detailed guide of the development environment can be found inSETUP, further details about the usage can be found inUSAGE, descriptions for testing can be found inTESTINGand several examples inEXAMPLESpython3-mvenv.venvsource.venv/bin/activate

pipinstall'rshell>=0.0.30,<1.0.0'pipinstall'mpremote>=0.4.0,<1'Install package on board with mip or upiprshell-p/dev/tty.SLAB_USBtoUART--editornanoInside thershellopen a REPL and execute these
commands inside the REPLimportmachineimportnetworkimporttimeimportmipstation=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 theupippackage instead ofmipimportmachineimportnetworkimporttimeimportupipstation=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 statusAfter a successful installation of the package and reboot of the system as
described in theinstallation sectionthe following commands can be used to request a coil state of a target/client
device. Further usage examples can be found in theexamples folderand in theUSAGE chapterTCPfromummodbus.tcpimportModbusTCPMastertcp_device=ModbusTCPMaster(slave_ip='172.24.0.2',# IP address of the target/client/slave deviceslave_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 busslave_addr=10coil_address=123coil_qty=1coil_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 latestMicroPython Modbus TCP documentation exampleRTUfromumodbus.serialimportSerialasModbusRTUMasterhost=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 busslave_addr=10coil_address=123coil_qty=1coil_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 latestMicroPython Modbus RTU documentation exampleInstall additional MicroPython packagesTo use this package with the providedboot.pyandmain.pyfile, 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 viaupipas follows# with MicroPython version 1.19.1 or newerimportmipmip.install('github:brainelectronics/micropython-modules')# before MicroPython version 1.19.1importupipupip.install('micropython-brainelectronics-helpers')Check also the README of thebrainelectronics MicroPython modules, theINSTALLATIONand theSETUPguides.UsageSeeUSAGEandDOCUMENTATIONSupported Modbus functionsRefer to the following table for the list of supported Modbus functions.IDDescription1Read coils2Read discrete inputs3Read holding registers4Read input registers5Write single coil6Write single register15Write multiple coils16Write multiple registersCreditsBig thank you togiampiero7for the initial implementation
of this library.sfera-labs-Initial work-giampiero7pycom-Initial Modbus work-pycom-modbuspfalcon-Initial MicroPython unittest module-micropython-unittest