Current Location: Home> Latest Articles> Implementing MODBUS TCP Communication in Industrial Systems with PHP

Implementing MODBUS TCP Communication in Industrial Systems with PHP

M66 2025-06-06

Introduction to the MODBUS TCP Protocol

MODBUS TCP is a communication protocol based on TCP/IP, widely used in industrial automation for data exchange between devices such as PLCs, remote I/Os, and sensors. It operates over Ethernet and supports reading and writing registers, transmitting data, and controlling devices.

The protocol typically follows a master-slave architecture, where the master initiates communication and the slave responds to requests. This structure allows centralized control of multiple devices, enabling efficient data acquisition and remote monitoring in industrial environments.

Building MODBUS TCP Communication in PHP

Although PHP is predominantly known for web development, it is also capable of handling certain industrial communication tasks, especially in middleware or edge computing environments. Below is a PHP implementation for MODBUS TCP communication:
<?php
class ModbusTCP {
    private $socket;

    public function __construct($ip, $port) {
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_connect($this->socket, $ip, $port);
    }

    public function readRegisters($slaveId, $registerAddress, $registerCount) {
        $buffer = pack("nnnn", $slaveId, 0x03, $registerAddress, $registerCount);
        socket_send($this->socket, $buffer, strlen($buffer), 0);

        $response = '';
        $bytes = socket_recv($this->socket, $response, 2048, MSG_WAITALL);

        $registers = unpack("n*", substr($response, 9));
        return $registers;
    }

    public function writeRegister($slaveId, $registerAddress, $registerValue) {
        $buffer = pack("nnn", $slaveId, 0x06, $registerAddress, $registerValue);
        socket_send($this->socket, $buffer, strlen($buffer), 0);
    }

    public function __destruct() {
        socket_close($this->socket);
    }
}

// Usage example
$modbus = new ModbusTCP('192.168.0.1', 502);
$registers = $modbus->readRegisters(1, 0, 10);

foreach ($registers as $address => $value) {
    echo "Register $address: $value\n";
}

$modbus->writeRegister(1, 0, 100);

This code defines a ModbusTCP class in PHP that encapsulates the key functionalities of MODBUS TCP communication. It includes methods to create and close socket connections, read registers, and write values to them. Developers can use this class to connect to a MODBUS device over a specified IP and port, perform register operations, and process the response.

Use Case Analysis

In the example above, the system connects to a MODBUS slave at `192.168.0.1` on port `502`. Using the `readRegisters` method, it fetches the first 10 register values and prints them. Finally, it writes the value `100` to the first register via `writeRegister`. These two operations — reading and writing registers — represent the core tasks in MODBUS TCP-based communication.

Conclusion

This article demonstrated how PHP can be used to implement communication with industrial systems via the MODBUS TCP protocol. By understanding the protocol's architecture and using PHP's socket functions, developers can build lightweight yet effective communication modules for automation projects. Although PHP isn't traditionally used in industrial control, it can still serve specific roles in edge devices or integration systems.

Reference:
[1] MODBUS Application Protocol Specification v1.1b3 — https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf