Home > Backend Development > PHP Tutorial > PHP Programming Tips: Implementing High-Reliability Modbus TCP Communication

PHP Programming Tips: Implementing High-Reliability Modbus TCP Communication

WBOY
Release: 2023-07-18 10:16:02
Original
1510 people have browsed it

PHP Programming Tips: Implementing High Reliability Modbus TCP Communication

Abstract: Modbus TCP is a commonly used industrial communication protocol used to achieve data interaction between devices in industrial automation systems. This article introduces some techniques for Modbus TCP communication using the PHP programming language to achieve high-reliability data transmission. Specifically, it includes code examples for installing and using the PHP Modbus extension library, establishing TCP connections, writing Modbus requests, and parsing responses.

Keywords: PHP programming, Modbus TCP, reliability, data transmission, code examples

Introduction:

Modbus TCP (Modbus protocol based on TCP/IP) is a A communication protocol widely used in the field of industrial automation. It is commonly used for data exchange between PLC (programmable logic controller), sensors, motor drivers and other devices. In practice, it is very important to ensure the reliability of Modbus TCP communication, especially for industrial applications that require efficient and stable data transmission. This article introduces some PHP programming techniques to help readers achieve high-reliability Modbus TCP communication.

  1. Installation and use using the PHP Modbus extension library

First, we need to install the PHP Modbus extension library. This extension library provides a series of functions to facilitate communication with Modbus devices. It can be installed through the following steps:

Step 1: Download the PHP Modbus extension library source code.
Step 2: Unzip the source code and enter the unzipped directory.
Step 3: Execute the following command to compile the extension library:
$ phpize
$ ./configure
$ make
$ make install

After the installation is completed, you can install it in php. ini file to enable the extension library:
extension=modbus.so

The method of using the PHP Modbus extension library is as follows:

// 建立TCP连接
$tcp = modbus_tcp_connect('192.168.0.1', 502);

// 编写Modbus请求
$request = modbus_write_multiple_registers(1, 100, array(0x01, 0x02, 0x03));

// 发送请求并接收响应
$response = modbus_send_raw_request($tcp, $request);

// 解析响应
$data = modbus_get_response_data($tcp, $response);
Copy after login
  1. Establishing a TCP connection

Before carrying out Modbus TCP communication, you need to first establish a TCP connection with the Modbus device. You can use PHP's socket function to establish a connection. The specific examples are as follows:

// 建立TCP连接
$ip = '192.168.0.1';
$port = 502;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    die("Socket创建失败: " . socket_strerror(socket_last_error()));
}

$result = socket_connect($socket, $ip, $port);
if ($result === false) {
    die("Socket连接失败: " . socket_strerror(socket_last_error($socket)));
}

// 连接建立成功,进行后续操作
// ...
Copy after login
  1. Code examples for writing Modbus requests and parsing responses

Generally speaking, Modbus requests are made by Function It consists of Code, Start Address and Data. The following is a sample code for writing Modbus multiple register requests and parsing responses:

// 编写Modbus请求:写多个寄存器
$slave_address = 1;
$start_address = 100;
$data = array(0x01, 0x02, 0x03);

$request = pack('CCCC', $slave_address, 0x10, ($start_address >> 8) & 0xFF, $start_address & 0xFF);
$request .= pack('n', count($data) * 2);
foreach ($data as $value) {
    $request .= pack('n', $value);
}

// 发送请求并接收响应
socket_write($socket, $request, strlen($request));
$response = socket_read($socket, 1024);

// 解析响应
if (strlen($response) <= 0) {
    die("未接收到响应");
}

$raw_data = substr($response, 6); // 去除响应头部
$data_length = ord($response[4]);

$data = array();
for ($i = 0; $i < $data_length / 2; $i++) {
    $data[] = ord($raw_data[$i * 2]) << 8 | ord($raw_data[$i * 2 + 1]);
}

// 打印解析结果
print_r($data);
Copy after login

Conclusion:

This article introduces the techniques of using the PHP programming language to achieve high reliability Modbus TCP communication. Includes code examples for installing and using the PHP Modbus extension library, establishing TCP connections, writing Modbus requests, and parsing responses. By studying this article, readers can better master the knowledge and skills related to Modbus TCP communication in PHP programming, thereby improving the data transmission efficiency and stability of industrial automation systems.

Reference:

  1. PHP Modbus extension library official document
  2. PHP official document: Socket function
  3. Modbus TCP protocol specification

The above is the detailed content of PHP Programming Tips: Implementing High-Reliability Modbus TCP Communication. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template