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

王林
王林Original
2023-07-18 10:25:151353browse

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.

  1. Introduction to Modbus TCP:
    Modbus TCP is a communication method based on the Modbus protocol, which implements communication between devices on a TCP/IP network. Modbus TCP uses standard Modbus registers for data exchange, supporting reading and writing data to the device.
  2. Integration of PHP with Modbus TCP:
    PHP is a very popular server-side scripting language with strong network and database support. Using PHP to communicate with Modbus TCP devices provides flexibility and ease of use.

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
  1. Connect to the Modbus device:
    First, we need to establish a connection to the Modbus device. The following is an example of a PHP function for connecting to a Modbus device:
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.

  1. Read the register value:
    Once connected to the Modbus device, we can use the Modbus TCP protocol to read the register value on the device. Here is an example function for reading the value of a register:
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.

  1. Write the value of the register:
    In addition to reading the value of the register, we can also use the Modbus TCP protocol to write data to the register of the device. The following is an example function for writing the value of a register:
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 $registerValueThe parameter is set to the value to be written.

  1. Close the connection with the Modbus device:
    After completing the communication with the Modbus device, we should close the established connection. The following is a sample function to close the connection:
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn