Example Code for Python-RS485 Relay Control

Last revision 2025/12/26

The article demonstrates the use of Python's ModBus library to control a DFR0290 8-channel relay, providing step-by-step guidance and example code for effective relay management through the RS485 protocol.

Hardware Preparation

Wiring Diagram

  1. Plug the shield into LattePanda Alpha & Delta:

Connection graph

You need to use nylon columns and nylon screws to fix the expansion board and LattePanda.

Connection2

Operation Steps

At present, the mainstream third-party libraries for Modbus protocol in Python is shown below:

  • Modbus_tk
  • Pymodbus
  • Minimalmodbus
  • Umodbus
Library name Source code 3rd party dependency Main functions
modbus_tk https://github.com/ljean/modbus-tk pyserial Support modbus TCP and RTU, both master and slave. Don't support modbus ASCII. Heavyweight.
pymodbus https://github.com/riptideio/pymodbus/ pyserial Support modbus TCP/RTU/ASCII, master and slave. Heavyweight.
minimalmodbus https://github.com/pyhys/minimalmodbus pyserial Only support Modbus RTU and ASCII, and only work in master(clinet). Very lightweight.
uModbus https://github.com/AdvancedClimateSystems/uModbus/ pyserial Support both Modbus client amd server (both TCP and RTU). Don't support Modbus ASCII. Lightweight.

Here we take the use of modbus_tk as an example.

  1. After connecting Lattepanda Alpha & Delta and RS485 interface extension board, boot up
  2. Power on RLY-8-PoE-RS485 8-channel network control relay module
  3. Install the Python environment, modbus_tk, and other dependent packages on LattePanda Alpha & Delta
  4. According to the ModBus protocol rules of the 8-channel network control relay module, write python code, then control the relay module.

Result

As shown in the figure below, run the Python script on the command line, obtain the device name and control the device turn on and off all the relays in turn

Result

Result

Additional Information

Was this article helpful?

TOP