aldas / modbus-tcp-client

PHP client for Modbus TCP and Modbus RTU over TCP (can be used for serial)
Apache License 2.0
191 stars 55 forks source link

FC6 Timeout error #158

Closed jmcphail closed 7 months ago

jmcphail commented 7 months ago

I am trying to write a single register value using the FC6 example. Slave device is a RMS200 from EtherTek Circuits. I have modified the example code to match my environment:

$connection = BinaryStreamConnection::getBuilder()
    ->setPort(1502)
    ->setHost('192.168.1.97')
    ->setConnectTimeoutSec(1.5) // timeout when establishing connection to the server
    ->setWriteTimeoutSec(5) // timeout when writing/sending packet to the server
    ->setReadTimeoutSec(5) // timeout when waiting response from server
    ->build();

$startAddress = 16;
$value = 1;

This is the output from the FC6 example:

Packet to be sent (in hex): 6fcd00000006000600100001
An exception occurred
Read total timeout expired
#0 /var/www/revpi/custom/vendor/aldas/modbus-tcp-client/src/Network/BinaryStreamConnection.php(59): ModbusTcpClient\Network\BinaryStreamConnection->receiveFrom()
#1 /var/www/revpi/custom/vendor/aldas/modbus-tcp-client/src/Network/BinaryStreamConnection.php(80): ModbusTcpClient\Network\BinaryStreamConnection->receive()
#2 /var/www/revpi/custom/examples/fc6.php(26): ModbusTcpClient\Network\BinaryStreamConnection->sendAndReceive()
#3 {main}

FC3 example working correctly. Modpoll write command working correctly:

pi@RevPi32736:~ $ modpoll -m tcp -p 1502 -a 1 -r 17 -c 1 -t 4 -1 192.168.1.97 1
modpoll 3.11 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright (c) 2002-2024 proconX Pty Ltd
Visit https://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: MODBUS/TCP, FC6
Slave configuration...: address = 1, start reference = 17, count = 1
Communication.........: 192.168.1.97, port 1502, t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, output (holding) register table

Written 1 reference.

Here is tcpdump capture of FC6 example request:

pi@RevPi32736:/var/www/revpi/custom $ sudo tcpdump -X port 1502
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
16:12:24.626028 IP 192.168.1.87.41696 > 192.168.1.97.1502: Flags [S], seq 2102609061, win 64240, options [mss 1460,sackOK,TS val 934722796 ecr 0,nop,wscale 7], length 0
        0x0000:  4500 003c ba8e 4000 4006 fc24 c0a8 0157  E..<..@.@..$...W
        0x0010:  c0a8 0161 a2e0 05de 7d53 44a5 0000 0000  ...a....}SD.....
        0x0020:  a002 faf0 8437 0000 0204 05b4 0402 080a  .....7..........
        0x0030:  37b6 bcec 0000 0000 0103 0307            7...........
16:12:24.629095 IP 192.168.1.97.1502 > 192.168.1.87.41696: Flags [S.], seq 478071512, ack 2102609062, win 5792, options [mss 1460,sackOK,TS val 1529272 ecr 934722796,nop,wscale 1], length 0
        0x0000:  4500 003c 0000 4000 4006 b6b3 c0a8 0161  E..<..@.@......a
        0x0010:  c0a8 0157 05de a2e0 1c7e cad8 7d53 44a6  ...W.....~..}SD.
        0x0020:  a012 16a0 10cc 0000 0204 05b4 0402 080a  ................
        0x0030:  0017 55b8 37b6 bcec 0103 0301            ..U.7.......
16:12:24.629160 IP 192.168.1.87.41696 > 192.168.1.97.1502: Flags [.], ack 1, win 502, options [nop,nop,TS val 934722800 ecr 1529272], length 0
        0x0000:  4500 0034 ba8f 4000 4006 fc2b c0a8 0157  E..4..@.@..+...W
        0x0010:  c0a8 0161 a2e0 05de 7d53 44a6 1c7e cad9  ...a....}SD..~..
        0x0020:  8010 01f6 842f 0000 0101 080a 37b6 bcf0  ...../......7...
        0x0030:  0017 55b8                                ..U.
16:12:24.629348 IP 192.168.1.87.41696 > 192.168.1.97.1502: Flags [P.], seq 1:13, ack 1, win 502, options [nop,nop,TS val 934722800 ecr 1529272], length 12
        0x0000:  4500 0040 ba90 4000 4006 fc1e c0a8 0157  E..@..@.@......W
        0x0010:  c0a8 0161 a2e0 05de 7d53 44a6 1c7e cad9  ...a....}SD..~..
        0x0020:  8018 01f6 843b 0000 0101 080a 37b6 bcf0  .....;......7...
        0x0030:  0017 55b8 95cc 0000 0006 0006 0010 0001  ..U.............
16:12:24.629933 IP 192.168.1.97.1502 > 192.168.1.87.41696: Flags [.], ack 13, win 2896, options [nop,nop,TS val 1529273 ecr 934722800], length 0
        0x0000:  4500 0034 9f60 4000 4006 175b c0a8 0161  E..4.`@.@..[...a
        0x0010:  c0a8 0157 05de a2e0 1c7e cad9 7d53 44b2  ...W.....~..}SD.
        0x0020:  8010 0b50 4ad1 0000 0101 080a 0017 55b9  ...PJ.........U.
        0x0030:  37b6 bcf0                                7...
16:12:29.635501 IP 192.168.1.87.41696 > 192.168.1.97.1502: Flags [F.], seq 13, ack 1, win 502, options [nop,nop,TS val 934727806 ecr 1529273], length 0
        0x0000:  4500 0034 ba91 4000 4006 fc29 c0a8 0157  E..4..@.@..)...W
        0x0010:  c0a8 0161 a2e0 05de 7d53 44b2 1c7e cad9  ...a....}SD..~..
        0x0020:  8011 01f6 842f 0000 0101 080a 37b6 d07e  ...../......7..~
        0x0030:  0017 55b9                                ..U.
16:12:29.636773 IP 192.168.1.97.1502 > 192.168.1.87.41696: Flags [F.], seq 1, ack 14, win 2896, options [nop,nop,TS val 1529773 ecr 934727806], length 0
        0x0000:  4500 0034 9f61 4000 4006 175a c0a8 0161  E..4.a@.@..Z...a
        0x0010:  c0a8 0157 05de a2e0 1c7e cad9 7d53 44b3  ...W.....~..}SD.
        0x0020:  8011 0b50 354d 0000 0101 080a 0017 57ad  ...P5M........W.
        0x0030:  37b6 d07e                                7..~
16:12:29.636897 IP 192.168.1.87.41696 > 192.168.1.97.1502: Flags [.], ack 2, win 502, options [nop,nop,TS val 934727807 ecr 1529773], length 0
        0x0000:  4500 0034 ba92 4000 4006 fc28 c0a8 0157  E..4..@.@..(...W
        0x0010:  c0a8 0161 a2e0 05de 7d53 44b3 1c7e cada  ...a....}SD..~..
        0x0020:  8010 01f6 842f 0000 0101 080a 37b6 d07f  ...../......7...
        0x0030:  0017 57ad                                ..W.
^C
8 packets captured

Can you help see where problem is? Is it an unexpected slave device response? Library error? The odd thing is the FC6 command is properly received by device (modbus register value is confirmed as changed). It appears as though the slave response is not recognized by library.

aldas commented 7 months ago

I think the problem here is that you are using $unitID = 0; in FC6.php example. I can see that from packet that is sent 6fcd00000006000600100001

Here is FC6 request explained: https://github.com/aldas/modbus-tcp-client/blob/53657b3de8f90c9e6727c9aca841b3e355fbe554/src/Packet/ModbusFunction/WriteSingleRegisterRequest.php#L19-L26

So 6fcd00000006000600100001 is sending packet to Unit/Slave address = 0

but in modpoll you are sending to UnitID = 1. This is that -a 1 argument.


If I recall correctly Unit/Slave 0 is "broadcast" address which may and may not work for some PLCs

jmcphail commented 7 months ago

Thank you for your efforts and for the second pair of eyes. This has resolved the 'time out'

$connection = BinaryStreamConnection::getBuilder()
    ->setPort(1502)
    ->setHost('192.168.1.97')
    ->setConnectTimeoutSec(1.5) // timeout when establishing connection to the server
    ->setWriteTimeoutSec(5) // timeout when writing/sending packet to the server
    ->setReadTimeoutSec(5) // timeout when waiting response from server
    ->build();

$startAddress = 16;
$value = 1;
$unitID = 1;
$packet = new WriteSingleRegisterRequest($startAddress, $value, $unitID);