Home > Article > Backend Development > PHP and Modbus TCP: Discussion on methods to achieve distributed device control
PHP and Modbus TCP: Discussion on methods to achieve distributed device control
Overview:
With the rapid development of the Internet of Things, distributed device control has become an important challenge. This article will explore how to use PHP and the Modbus TCP protocol to implement distributed device control, and how to write code examples to help readers better understand the method.
Preparation:
Before we start writing code, we need to make sure that the Modbus TCP library has been installed. It can be installed in a Linux system using the following command:
sudo apt-get install php7.4-mbstring
function connectModbusDevice($ip, $port) { $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { die("无法创建套接字"); } $result = socket_connect($socket, $ip, $port); if ($result === false) { die("无法连接到Modbus设备"); } return $socket; }
Please make sure the $ip
and $port
parameters are set to the correct ones The IP address and port of the Modbus device.
function readModbusRegister($socket, $registerAddress) { $request = "" . pack('n', $registerAddress) . ""; socket_write($socket, $request, strlen($request)); $response = socket_read($socket, 2048); // 解析响应数据,获取寄存器的值 $registerValue = unpack('n', substr($response, 9, 2))[1]; return $registerValue; }
Be sure to set the $registerAddress
parameter to the address of the register you want to read.
function writeModbusRegister($socket, $registerAddress, $registerValue) { $request = "" . pack('n', $registerAddress) . pack('n', $registerValue); socket_write($socket, $request, strlen($request)); $response = socket_read($socket, 2048); // 解析响应数据,检查写入操作是否成功 $statusCode = unpack('n', substr($response, 7, 2))[1]; if ($statusCode != 6) { die("写入寄存器失败"); } }
Be sure to set the $registerAddress
parameter to the address of the register you want to write to, and $registerValue
The parameter is set to the value to be written.
function disconnectModbusDevice($socket) { socket_close($socket); }
The complete sample code is as follows:
$modbusIp = "192.168.1.100"; $modbusPort = 502; $modbusDevice = connectModbusDevice($modbusIp, $modbusPort); $registerAddress = 100; $registerValue = readModbusRegister($modbusDevice, $registerAddress); echo "寄存器 $registerAddress 的值为: $registerValue" . PHP_EOL; $registerAddress = 200; $registerValue = 500; writeModbusRegister($modbusDevice, $registerAddress, $registerValue); echo "成功将值 $registerValue 写入寄存器 $registerAddress" . PHP_EOL; disconnectModbusDevice($modbusDevice);
Summary:
This article introduces how to use PHP and Modbus TCP protocol to Implement distributed device control. We provide code examples for key functions such as connecting to a Modbus device, reading register values, writing register values, and closing the connection. By using these examples, readers can better understand and utilize PHP and Modbus TCP to implement distributed device control.
The above is the detailed content of PHP and Modbus TCP: Discussion on methods to achieve distributed device control. For more information, please follow other related articles on the PHP Chinese website!