Skip to main content

Programming the RevPi DIO using Modbus and Python

Introduction

This tutorial explains how to configure the RevPi Connect 4 as a Modbus TCP Slave and control an LED through the digital output of the RevPi DIO module. The Modbus TCP Master is simulated using QModMaster, and a Python script leveraging the RevPiModIO library is developed to transfer Modbus data to the digital outputs.

Prerequisites

Hardware

  • RevPi base module (eg. RevPi Connect 4)
  • Master device or software: For example, “qModMaster” running on a Windows PC.
  • Matching cables with RJ45 connectors
  • Power supply for RevPi Connect

Software

  • A modern web browser (e.g., Google Chrome or Mozilla Firefox).
  • qModMaster: Downloadable from SourceForge for use in this example.

System Setup

Ensure that:

  1. The RevPi base module and master device are located in the same network.
  2. IP addresses are properly configured, and the devices can communicate with each other.

Step 1: Hardware Setup

  1. Connect the RevPi Connect to the master device using an RJ45 cable.
  2. Power on the RevPi Connect by connecting it to a suitable power supply.

Step 2: Configure Modbus TCP Slave in PiCtory

Start pictory.

Webstatus login

Add Base Module and Add the virtual Modbus TCP Slave to your configuration.

  • Drag the base module from the Device Catalog onto the virtual DIN rail.

  • Open the folder Virtual Devices in the Device Catalog.

    • Drag Modbus TCP Slave to the base module on the virtual DIN rail.
      ➜ The Modbus TCP Slave will now appear in the configuration

Configure Modbus TCP Slave

  • Select the Modbus TCP Slave in the configuration.

PiCtory config

  • Set the following parameters in the Value Editor:
    • TCP Port: '502' (default value according to the Modbus specification).
    • Max. Modbus TCP Connections: '10' (or other suitable value).
    • Input_1ModbusInput1
    • Output_1ModbusOutput1

PiCtory config

Pictory Value editor modbus

Save the configuration and restart the driver to apply the changes.

Step 1: Prepare the Hardware

  1. Wiring:

▷ Connect the RevPi Connect 4 to the RevPi DIO module via the PiBridge.

  1. Connect LED:

▷ Attach an LED to one of the digital outputs of the RevPi DIO module (e.g., DIO Output 1 oder O_1).

Step 3: Set Up QModMaster

Install QModMaster

▷ Download QModMaster from its official website and install it on your computer.

Establish Connection

  1. Start QModMaster.

  2. Navigate to Options → Modbus TCP and enter the following details:

  • IP Address: IP address of the RevPi Connect 4
  • Port: 502 (default for Modbus TCP)
  1. Confirm the settings and establish the connection.

Send Modbus Data

  1. In QModMaster, select the function Write Single Register (0x06).

  2. Enter the address of the first Modbus register (e.g., address 0).

  3. Write the value 7 (equivalent to 0b0000111) to set the first three digital outputs (DIO) to HIGH.

RevPi commander starten

Step 4: Create and Upload a Python Script Using RevPiModIO

Create the Python Script

Write the following Python script to process Modbus data and control the LED:

import revpimodio2
import time

# Initialize the RevPiModIO object
rpi = revpimodio2.RevPiModIO(autorefresh=True)

while True:
# Set ModbusOutput1 value to the value of I_1
rpi.io.ModbusOutput_1.value = int(rpi.io.I_1.value)
# Set O_1 value to the value of ModbusInput1
rpi.io.O_1.value = int(rpi.io.ModbusInput_1.value)

# Print current values for verification
print(f"ModbusOutput_1: {rpi.io.ModbusOutput_1.value}, O_1: {rpi.io.O_1.value}")

time.sleep(0.02)

Transfer the Script to the RevPi

There are two ways to upload the script:

Option 1: Using RevPiCommander

Option 2: Manually via Terminal

  1. Open a terminal on the RevPi or connect via SSH.

  2. Create the script using the following command:

    sudo nano Revpi_DIO_Modbus.py
  3. Paste the script code and save the file.

  4. Run the script with:

    python3 Revpi_DIO_Modbus.py

Summary

In this tutorial, you learned how to configure the RevPi Connect 4 as a Modbus TCP Slave, control an LED using the RevPi DIO module, and send data via QModMaster. The Python script utilizes the RevPiModIO library to transfer Modbus data to the DIO outputs.

Now, the first three digital outputs should activate when the value 7 is sent to the Modbus register

– making the LED light up.