Skip to content

Commit 20b5a65

Browse files
committed
Add unitID and logger to examples
1 parent 73eba8b commit 20b5a65

11 files changed

+40
-11
lines changed

examples/fc1.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
use ModbusTcpClient\Packet\ResponseFactory;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setPort(5020)
1213
->setHost('127.0.0.1')
14+
->setLogger(new EchoLogger())
1315
->build();
1416

1517
$startAddress = 12288;
1618
$quantity = 16;
17-
$packet = new ReadCoilsRequest($startAddress, $quantity);
19+
$unitID = 0;
20+
$packet = new ReadCoilsRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
1821
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
1922

2023
try {

examples/fc15.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use ModbusTcpClient\Packet\ResponseFactory;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setPort(5020)
1213
->setHost('127.0.0.1')
14+
->setLogger(new EchoLogger())
1315
->build();
1416

1517
$startAddress = 12288;
@@ -18,7 +20,8 @@
1820
0, 0, 0, 0, 0, 0, 0, 0, // dec: 0, hex x0
1921
1, 0, 0, 1 // dec: 9, hex: x9
2022
];
21-
$packet = new WriteMultipleCoilsRequest($startAddress, $coils);
23+
$unitID = 0;
24+
$packet = new WriteMultipleCoilsRequest($startAddress, $coils, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
2225
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
2326

2427
try {

examples/fc16.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use ModbusTcpClient\Utils\Types;
88

99
require __DIR__ . '/../vendor/autoload.php';
10+
require __DIR__ . '/logger.php';
1011

1112
$connection = BinaryStreamConnection::getBuilder()
1213
->setPort(5020)
1314
->setHost('127.0.0.1')
15+
->setLogger(new EchoLogger())
1416
->build();
1517

1618
$startAddress = 12288;
@@ -20,7 +22,8 @@
2022
Types::toInt16(-1000), //hex: FC18 as word
2123
Types::toInt32(2000), //dec: 2000 -> hex: 7d00 is as 2 word 7D00 0000
2224
];
23-
$packet = new WriteMultipleRegistersRequest($startAddress, $registers);
25+
$unitID = 0;
26+
$packet = new WriteMultipleRegistersRequest($startAddress, $registers, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
2427
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
2528

2629
try {

examples/fc2.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
use ModbusTcpClient\Packet\ResponseFactory;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setPort(5020)
1213
->setHost('127.0.0.1')
14+
->setLogger(new EchoLogger())
1315
->build();
1416

1517
$startAddress = 12288;
1618
$quantity = 16;
17-
$packet = new ReadInputDiscretesRequest($startAddress, $quantity);
19+
$unitID = 0;
20+
$packet = new ReadInputDiscretesRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
1821
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
1922

2023
try {

examples/fc23.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use ModbusTcpClient\Utils\Types;
88

99
require __DIR__ . '/../vendor/autoload.php';
10+
require __DIR__ . '/logger.php';
1011

1112
$connection = BinaryStreamConnection::getBuilder()
1213
->setPort(5020)
1314
->setHost('127.0.0.1')
15+
->setLogger(new EchoLogger())
1416
->build();
1517

1618
$readStartAddress = 12288;
@@ -21,12 +23,14 @@
2123
Types::toInt16(10), //000a as word
2224
Types::toInt16(-1000), //hex: FC18 as word
2325
];
26+
$unitID = 0;
2427
$packet = new ReadWriteMultipleRegistersRequest(
2528
$readStartAddress,
2629
$readQuantity,
2730
$writeStartAddress,
28-
$writeRegisters
29-
);
31+
$writeRegisters,
32+
$unitID
33+
); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
3034
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
3135

3236
try {

examples/fc3.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
$startAddress = 256;
2424
$quantity = 6;
2525
$unitID = 0;
26-
$packet = new ReadHoldingRegistersRequest($startAddress, $quantity, $unitID);
26+
$packet = new ReadHoldingRegistersRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
2727

2828
try {
2929
$binaryData = $connection->connect()->sendAndReceive($packet);

examples/fc4.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
use ModbusTcpClient\Packet\ResponseFactory;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setPort(5020)
1213
->setHost('127.0.0.1')
14+
->setLogger(new EchoLogger())
1315
->build();
1416

1517
$startAddress = 256;
1618
$quantity = 6;
17-
$packet = new ReadInputRegistersRequest($startAddress, $quantity);
19+
$unitID = 0;
20+
$packet = new ReadInputRegistersRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
1821
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
1922

2023
try {

examples/fc5.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
use ModbusTcpClient\Packet\ResponseFactory;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setPort(5020)
1213
->setHost('127.0.0.1')
14+
->setLogger(new EchoLogger())
1315
->build();
1416

1517
$startAddress = 12288;
1618
$coil = true;
17-
$packet = new WriteSingleCoilRequest($startAddress, $coil);
19+
$unitID = 0;
20+
$packet = new WriteSingleCoilRequest($startAddress, $coil, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
1821
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
1922

2023
try {

examples/fc6.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
use ModbusTcpClient\Packet\ResponseFactory;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setPort(5020)
1213
->setHost('127.0.0.1')
14+
->setLogger(new EchoLogger())
1315
->build();
1416

1517
$startAddress = 12288;
16-
$value = 258; // 0x0102
17-
$packet = new WriteSingleRegisterRequest($startAddress, $value);
18+
$value = 55;
19+
$unitID = 0;
20+
$packet = new WriteSingleRegisterRequest($startAddress, $value, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
1821
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
1922

2023
try {

examples/rtu.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ModbusTcpClient\Packet\RtuConverter;
66

77
require __DIR__ . '/../vendor/autoload.php';
8+
require __DIR__ . '/logger.php';
89

910
$connection = BinaryStreamConnection::getBuilder()
1011
->setPort(502)
@@ -17,6 +18,7 @@
1718
// Read about differences here: https://www.simplymodbus.ca/TCP.htm
1819
return true;
1920
})
21+
->setLogger(new EchoLogger())
2022
->build();
2123

2224
$startAddress = 256;

examples/rtu_usb_to_serial_stream.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ModbusTcpClient\Packet\RtuConverter;
77

88
require __DIR__ . '/../vendor/autoload.php';
9+
require __DIR__ . '/logger.php';
910

1011
$connection = BinaryStreamConnection::getBuilder()
1112
->setUri('/dev/ttyUSB0')
@@ -17,6 +18,7 @@
1718
// Example for FC4 with quantity 2: 8 bytes = 1 unit id + 1 byte for function code + 2 bytes start address + 2 * quantity
1819
return true;
1920
})
21+
->setLogger(new EchoLogger())
2022
->build();
2123

2224
$startAddress = 1;

0 commit comments

Comments
 (0)