Current Location: Home> Latest Articles> PHP Guide: How to Read, Filter, and Analyze Modbus TCP Data in Real Projects

PHP Guide: How to Read, Filter, and Analyze Modbus TCP Data in Real Projects

M66 2025-05-31

Practical PHP Guide to Reading and Analyzing Modbus TCP Data

Modbus TCP is a widely adopted communication protocol in the industrial automation field, allowing data exchange between devices over Ethernet. In PHP development, establishing communication with Modbus TCP devices and processing the returned data is often a critical part of many engineering applications. This article walks you through a complete implementation using PHP, covering connection setup, data requests, and smart filtering techniques.

Establishing a Modbus TCP Connection

To initiate communication with Modbus TCP devices in PHP, you can use the phpmodbus library. First, install it via Composer:

composer require modbus/tcp-php

Once installed, you can use the following code to connect to the device:

<?php
require __DIR__ . '/vendor/autoload.php';

use ModbusTcpClient\Network\BinaryStreamConnection;

$ip = '192.168.1.100';
$port = 502;

$connection = new BinaryStreamConnection($ip, $port);
$connection->connect();

// Communication with the Modbus TCP device goes here

$connection->close();
?>

Sending Modbus TCP Read Requests

After establishing the connection, you can send requests using the library’s classes and methods. Here's an example that reads holding registers from the device:

use ModbusTcpClient\Request\ReadHoldingRegistersRequest;
use ModbusTcpClient\Utils\Types;

$request = new ReadHoldingRegistersRequest(0, 10);
$response = $connection->sendRequest($request);

if ($response->isOk()) {
    $data = Types::parseByteArray($response->getData());
    print_r($data);
} else {
    echo 'Request failed: ' . $response->getErrorMessage();
}

This code sends a request to read 10 holding registers starting from address 0, and then parses the returned byte array.

Filtering and Analyzing Returned Data

In real-world scenarios, you may not need all the raw data. For example, you might only want to process values greater than 100. Here's how to filter the data accordingly:

if ($response->isOk()) {
    $data = Types::parseByteArray($response->getData());

    $filteredData = array_filter($data, function($value) {
        return $value > 100;
    });

    print_r($filteredData);
} else {
    echo 'Request failed: ' . $response->getErrorMessage();
}

The array_filter function allows you to apply custom conditions to extract only relevant data, making it easier to perform targeted analysis or reporting.

Conclusion

This article has demonstrated how to build a PHP-based solution for communicating with Modbus TCP devices, reading holding register values, and applying filtering logic to process the results. With the phpmodbus library, PHP developers can integrate Modbus communication into industrial web applications, which is especially useful for automation, remote monitoring, and control systems.

Mastering these techniques will greatly enhance your ability to develop efficient, real-time industrial applications with PHP.