Comprehensive Guide To Connecting Raspberry Pi 4 Via Modbus To Mango Automation

Comprehensive Guide To Connecting Raspberry Pi 4 Via Modbus To Mango Automation

WAGO 750-8001 Modbus Connection - CODESYS - WAGO Automation Community

Integrating a Raspberry Pi 4 with Mango Automation through the Modbus protocol enables robust industrial monitoring and control by leveraging the Pi’s GPIO and serial capabilities. Successful deployment requires the precise configuration of RS-485 serial parameters or TCP/IP socket listeners, ensuring that register mappings align with the standard Modbus function codes for seamless data polling and command execution.


Hardware Integration and Software Prerequisites

Before initiating the technical configuration, it is essential to establish a stable hardware foundation. The Raspberry Pi 4 is a highly capable single-board computer, but its native GPIO pins communicate at TTL levels (3.3V). For industrial Modbus RTU communication, which typically utilizes RS-485 differential signaling, an external converter is mandatory. This ensures signal integrity over long distances and provides protection against common-mode noise.

The scope of this integration covers both Modbus RTU (serial) and Modbus TCP (Ethernet/Wi-Fi). While Modbus TCP is simpler to implement due to the Pi’s built-in Ethernet port, Modbus RTU remains the standard for legacy industrial sensors and power meters.

Essential Equipment and Material Checklist:



  • Raspberry Pi 4 Model B: Minimum 4GB RAM recommended for hosting Mango Automation locally, though 2GB suffices if the Pi acts strictly as a remote I/O node.
  • USB-to-RS485 Industrial Converter: Seek adapters featuring the FTDI or CP2102 chipset for superior driver stability under Linux environments.
  • Shielded Twisted Pair (STP) Cabling: Essential for RS-485 runs to mitigate Electromagnetic Interference (EMI).
  • Termination Resistors: 120-ohm resistors for the start and end of the RS-485 bus to prevent signal reflection.
  • Mango Automation Software: Version 3.x or 4.x (Radix IoT) installed either on the Pi itself or a centralized server.
  • Power Supply: A stable 5.1V, 3.0A USB-C power source to prevent undervoltage events during high CPU load.
  • Knowledge Requirements: Familiarity with Linux terminal operations, understanding of hexadecimal register addressing, and knowledge of Modbus Function Codes (01 through 04).

Step-by-Step Modbus Integration Workflow

The process of connecting these two entities involves preparing the Pi’s operating system, configuring the communication driver, and finally mapping the data points within the Mango Automation environment.



Step 1: Raspberry Pi Serial Interface Preparation

To use Modbus RTU, the Raspberry Pi's hardware serial port must be released from system console duties. By default, Raspberry Pi OS uses the serial port for a login shell, which will interfere with Modbus packets.

Begin by accessing the system configuration tool via the command line interface. Navigate to the Interfacing Options and locate the Serial port settings. You must disable the "Serial Login Shell" while keeping the "Serial Port Hardware" enabled. This allows the operating system to recognize the UART hardware without attempting to send system messages through it. Once this change is applied, a system reboot is necessary to reinitialize the hardware registers. If you are using a USB-to-RS485 adapter, the device will typically appear as a specific TTYUSB device in the system directory, which does not require the hardware UART modification but does require proper user permissions. Ensure your user account is a member of the "dialout" group to allow the Mango application to access the serial hardware without requiring root privileges.



Step 2: Selecting and Installing the Modbus Driver Layer

Depending on whether your Raspberry Pi 4 will act as a Modbus Master (Client) or a Modbus Slave (Server), you must install the appropriate service. Mango Automation generally acts as the Master, polling data from various Slaves. However, if the Pi 4 is gathering local sensor data (e.g., via I2C or GPIO), it must run a Modbus Slave daemon to "expose" that data to Mango.

For a Modbus Slave setup on the Pi, you should utilize a robust library such as Python-based MinimalModbus or a dedicated C-based daemon. This software will map the Pi’s internal memory or GPIO states to specific Modbus registers. For example, a temperature sensor connected to the Pi might have its value written to Holding Register 40001.

Pro-Tip: When setting up Modbus RTU, always ensure the Baud Rate, Data Bits, Stop Bits, and Parity match exactly across all devices on the bus. A common industrial standard is 9600 Baud, 8 Data Bits, 1 Stop Bit, and No Parity.



Step 3: Configuring the Data Source in Mango Automation

With the Raspberry Pi ready to communicate, you must now configure Mango Automation to recognize it as a valid data source. Access the Mango web interface and navigate to the Data Sources section. Select "Modbus IP" if you are using an Ethernet connection, or "Modbus Serial" if you are using an RS-485 adapter.

In the Data Source configuration screen, assign a unique name to the connection. For Serial connections, specify the port path (such as the TTYUSB location identified in Step 1) and the exact serial parameters. For IP connections, enter the static IP address of the Raspberry Pi and the standard Modbus TCP port, which is 502. It is highly recommended to set a "Timeout" value of at least 500 milliseconds to account for network latency or processing time on the Pi 4.



Step 4: Mapping Modbus Registers to Mango Data Points

After saving the Data Source, you must add individual Data Points. This is where the physical reality of the Pi’s sensors is translated into digital values within Mango. You will need to define the "Slave ID" (or Unit ID), which is the unique address assigned to the Pi.

Choose the correct Register Range for your data. Use "Coils" or "Discrete Inputs" for Boolean values (on/off) and "Holding Registers" or "Input Registers" for numerical data. If the Pi is sending floating-point numbers, you must select the appropriate "Data Type" in Mango, such as "4-byte float." Be aware of "Endianness"—the order in which bytes are sent. If your values look like gibberish in Mango, you may need to toggle the "Swap Words" or "Swap Bytes" settings in the Data Point configuration.

Warning: Avoid polling the Raspberry Pi too frequently. Setting an update period faster than 100ms on a serial bus with multiple devices can lead to "Buffer Overrun" errors and significant data packet collisions.


Technical Specifications and Communication Standards

The following table outlines the critical parameters required to ensure a stable connection between a Raspberry Pi 4 and Mango Automation across different Modbus flavors.



Feature Modbus RTU (Serial) Modbus TCP (Ethernet)
Physical Layer RS-485 (via USB/GPIO) IEEE 802.3 (Ethernet)
Maximum Recommended Distance 1,200 Meters (4,000 ft) 100 Meters per segment
Standard Port / Baud Rate 9600 - 115200 bps TCP Port 502
Error Checking CRC (Cyclic Redundancy Check) TCP Checksum / IP Header
Addressing Limit 247 Slave Devices Virtually Unlimited (IP based)
Typical Latency 10ms - 100ms 1ms - 10ms
Ideal Use Case Legacy sensors, power meters Local Network, high-speed I/O

Diagnostic Protocols and Communication Error Resolution

Even with careful planning, industrial communication often encounters environmental or configuration hurdles. Identifying the root cause requires a systematic approach to the communication stack.



  • Failure Scenario: Mango reports "CRC Error" or "Frame Error"



    • Root Cause: This usually indicates physical layer interference or a mismatch in serial settings. It can also be caused by an incorrectly placed termination resistor or a ground loop.
    • Actionable Fix: Verify that the Baud Rate and Parity are identical on the Pi and in Mango. Ensure that a 120-ohm resistor is present at the end of the RS-485 chain and check that the shield of the STP cable is grounded at only one point to prevent loops.
  • Failure Scenario: "No Response from Slave" Timeout



    • Root Cause: The Slave ID (Unit ID) in the Mango configuration does not match the ID programmed into the Raspberry Pi's Modbus service. Alternatively, the TX/RX lines (A and B) may be swapped.
    • Actionable Fix: Double-check the Slave ID in your Modbus daemon code. On the hardware side, try swapping the A (+) and B (-) wires on the RS-485 converter, as labeling conventions are occasionally reversed between manufacturers.
  • Failure Scenario: Permission Denied on Serial Port



    • Root Cause: The user account running the Mango Automation service does not have the rights to read or write to the system serial devices.
    • Actionable Fix: Use the command line to add the service user to the "dialout" and "tty" groups. You may also need to create a UDEV rule to permanently set the permissions of the TTYUSB device to 666 upon connection.

Frequently Asked Questions



Can I run Mango Automation directly on the Raspberry Pi 4?

Yes, the Raspberry Pi 4 with 4GB or 8GB of RAM is an excellent host for Mango Automation. You must install a Java Runtime Environment (JRE) first, preferably an OpenJDK version compatible with the Mango version you are deploying. Running Mango locally reduces network latency for Modbus polling significantly.



How do I handle 32-bit integer values in Modbus?

Modbus registers are natively 16-bit. To transmit a 32-bit value, you must use two adjacent registers. In Mango Automation, configure the Data Point as a "4-byte signed integer" and point it to the first of the two registers. Ensure the "Byte Order" matches what the Pi is sending (typically Big-Endian).



Is it possible to use the Raspberry Pi 4 as a Modbus Gateway?

Absolutely. You can configure the Pi 4 to act as a Modbus TCP-to-RTU gateway. In this setup, Mango Automation talks to the Pi via Modbus TCP, and the Pi translates those requests to Modbus RTU to communicate with local serial devices. This is highly effective for extending the life of serial-only hardware.



Why are my Modbus values off by a factor of 10 or 100?

Many industrial Modbus devices do not support floating-point numbers. Instead, they send "Scaled Integers." For example, a temperature of 25.5°C might be sent as the integer 255. In the Mango Data Point settings, use the "Multiplier" field (set to 0.1) to automatically scale the value back to its original decimal form.

Optimize Your Industrial IoT Ecosystem

Mastering the connection between Raspberry Pi hardware and Mango Automation software provides a professional-grade foundation for any SCADA or IIoT project. By following these rigorous hardware standards and configuration steps, you ensure that your automation network remains scalable, reliable, and easy to maintain.


Read also: Mug Shots Tampa: A Complete Guide to Hillsborough County Arrest Records, Public Safety Trends, and Search Tools