PHP code example of erikwang2013 / industrial-protocols-modbus
1. Go to this page and download the library: Download erikwang2013/industrial-protocols-modbus library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
erikwang2013 / industrial-protocols-modbus example snippets
$kernel = new Kernel(['config_path' => __DIR__ . '/config.php']);
$kernel->getProtocolRegistry()->register(new ModbusProtocol());
$kernel->boot();
$conn = $kernel->getConnectionManager()->connect('plc-001');
$result = $conn->read('40001'); // ['40001' => 42]
$conn->write(['40001' => 25]); // 单寄存器写入
$conn->write(['40001', '40002'], [100, 200]); // 批量写入
'devices' => [
'plc-001' => [
'protocol' => 'modbus', 'variant' => 'tcp',
'host' => '192.168.1.10', 'port' => 502,
'unit_id' => 1, 'timeout' => 3000,
],
'plc-rtu' => [
'protocol' => 'modbus', 'variant' => 'rtu',
'device' => '/dev/ttyUSB0', 'baud_rate' => 19200,
'parity' => 'N', 'data_bits' => 8, 'stop_bits' => 1,
'unit_id' => 1,
],
],