Home > Backend Development > PHP Tutorial > How to implement batch reading and writing of Modbus TCP through PHP

How to implement batch reading and writing of Modbus TCP through PHP

王林
Release: 2023-07-17 15:56:01
Original
1862 people have browsed it

How to realize batch reading and writing of Modbus TCP through PHP

1. Introduction to Modbus TCP
Modbus TCP is an industrial communication protocol based on TCP/IP protocol, which is commonly used in the field of industrial control device communication. By using the Modbus TCP protocol, data reading and writing operations between devices can be achieved. This article will introduce the use of PHP language to implement batch reading and writing of Modbus TCP, and attach corresponding code examples.

2. Environment preparation
Before you start writing PHP code, you need to install the corresponding PHP extensions and environment. Make sure that PHP and Modbus TCP related extensions have been installed on the server. After the installation is complete, you can use the phpinfo() function to check whether PHP is running normally and whether the Modbus TCP extension is loaded.

3. Modbus TCP connection and communication
In PHP, you can use the modbus_new_tcp() function to create a Modbus TCP connection. This function accepts two parameters, the first parameter is the IP address of the Modbus TCP server, and the second parameter is the port number of the Modbus TCP server. The following is a sample code:

$modbus = modbus_new_tcp("192.168.1.10", 502);
if (!$modbus) {
    die('Failed to create Modbus TCP connection');
}
Copy after login

After successful creation, you can use the modbus_read_input_registers() function to read data. This function accepts four parameters, namely Modbus TCP connection, slave address, register address and The number of reads. The following is a sample code for reading the input register:

$data = modbus_read_input_registers($modbus, 1, 0, 10);
if ($data === false) {
    die('Failed to read input registers');
}
print_r($data);
Copy after login

Through the above code, you can read the data of 10 registers starting from register 0 of the device with slave address 1, and output the result.

If you need to write to the device, you can use the modbus_write_single_register() function. This function accepts four parameters, namely Modbus TCP connection, slave address, register address and the data to be written. The following is a sample code for writing to a single register:

$success = modbus_write_single_register($modbus, 1, 0, 100);
if (!$success) {
    die('Failed to write single register');
}
Copy after login

With the above code, data 100 can be written to register 0 of the device with slave address 1.

4. Batch reading and writing
In actual projects, it may be necessary to read and write data to multiple devices at the same time. You can use the modbus_read_input_registers_batch() function and modbus_write_single_register_batch() function to implement batch operations.

The modbus_read_input_registers_batch() function accepts three parameters, which are the Modbus TCP connection, the device address and an array of register addresses. The following is a sample code for batch reading input registers:

$addresses = [0, 1, 2, 3, 4];
$data = modbus_read_input_registers_batch($modbus, 1, $addresses);
if (!$data) {
    die('Failed to read input registers batch');
}
print_r($data);
Copy after login

The modbus_write_single_register_batch() function accepts three parameters, which are the Modbus TCP connection, the device address, and an array of registers and data. The following is a sample code for batch writing to a single register:

$registersAndData = [
    [0, 100],
    [1, 200],
    [2, 300],
    [3, 400],
];
$success = modbus_write_single_register_batch($modbus, 1, $registersAndData);
if (!$success) {
    die('Failed to write single register batch');
}
Copy after login

With the above code, the registers of a group of devices can be read and written at the same time.

5. Summary
Through the above introduction and sample code, we can see that it is relatively simple to implement batch reading and writing of Modbus TCP through PHP language. Just prepare the corresponding environment and use the PHP extension functions related to Modbus TCP to easily realize communication and data operations between devices.

The above is the detailed content of How to implement batch reading and writing of Modbus TCP through PHP. 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