Current Location: Home> Latest Articles> Detailed Guide to PHP and Modbus Protocol Communication for Industrial Devices

Detailed Guide to PHP and Modbus Protocol Communication for Industrial Devices

M66 2025-07-22

Introduction to Modbus Protocol

The Modbus protocol is a widely used communication protocol in the field of industrial automation. It defines a set of communication rules that enable data exchange between devices from different manufacturers, ensuring interoperability of industrial equipment.

The most common variants are Modbus TCP and Modbus RTU. Modbus TCP is based on Ethernet communication and is suitable for scenarios requiring real-time data exchange, while Modbus RTU uses serial communication and is widely applied in traditional industrial environments.

Principles of PHP and Modbus Communication

PHP, as a server-side scripting language, is capable of handling network communication efficiently. The main steps to implement communication between PHP and the Modbus protocol include:

  • Establishing communication connection: Use PHP to create either a TCP connection or a serial port connection depending on the communication method.
  • Constructing the Modbus request: Generate request packets containing device address, function code, and register address according to the protocol format.
  • Sending and receiving data: Send requests via TCP or serial port and wait for device responses.
  • Parsing response data: Extract useful data by parsing responses according to Modbus protocol rules.
  • Closing connection: Release resources and close the connection after communication ends.

Example of PHP Implementing Modbus TCP Communication

The following example demonstrates how to use PHP to establish a Modbus TCP connection and read register data:

<?php
$serverIP = '192.168.0.1';  // Modbus TCP server IP
$serverPort = 502;  // Modbus TCP server port

// Create TCP connection
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, $serverIP, $serverPort);
if ($result === false) {
    die("Unable to connect to Modbus TCP server");
}

// Construct Modbus request packet (read registers)
$request = pack('nnnn', 0x0001, 0x0004, 0x0000, 0x0008);

// Send request packet
socket_write($socket, $request);

// Receive response packet
$response = socket_read($socket, 1024);

// Process response packet
$data = unpack('n*', $response);
print_r($data);

// Close TCP connection
socket_close($socket);
?>

This code creates a TCP connection using PHP, sends a register read request to the Modbus server, receives and parses the response, achieving basic industrial device data communication.

Conclusion

This article introduced the basics of the Modbus protocol and its application in industrial device communication, focusing on how to implement Modbus TCP communication using PHP. The example code helps readers understand and practice data interaction with industrial devices, promoting development and realization of industrial automation projects.