Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packet length more bytes than expected error #128

Closed
ranjithphptech opened this issue Jan 25, 2023 · 3 comments
Closed

packet length more bytes than expected error #128

ranjithphptech opened this issue Jan 25, 2023 · 3 comments
Labels

Comments

@ranjithphptech
Copy link

ranjithphptech commented Jan 25, 2023

root@ubuntu:/home/rkphptech# php /var/www/html/modbus-tcp-client-master/examples/write_usb_serial.php
RTU Binary to sent (in hex):   0106000d0001d9c9
2023-01-25 10:12:55.876190: [debug] msg: "Connected"
2023-01-25 10:12:55.880604: [debug] msg: "Data sent", data: array (
  1 => '0106000d0001d9c9',
)
2023-01-25 10:12:55.917155: [debug] msg: "Polling data"
2023-01-25 10:12:55.917287: [debug] msg: "Stream 37 @ index: 0 received data: ", data: array (
  1 => '0106000d0001d9c9',
)
An exception occurred
packet length more bytes than expected
#0 /var/www/html/modbus-tcp-client-master/examples/write_usb_serial.php(16): ModbusTcpClient\Utils\Packet::isCompleteLengthRTU()
#1 /var/www/html/modbus-tcp-client-master/src/Network/StreamHandler.php(78): {closure}()
#2 /var/www/html/modbus-tcp-client-master/src/Network/BinaryStreamConnection.php(60): ModbusTcpClient\Network\BinaryStreamConnection->receiveFrom()
#3 /var/www/html/modbus-tcp-client-master/src/Network/BinaryStreamConnection.php(83): ModbusTcpClient\Network\BinaryStreamConnection->receive()
#4 /var/www/html/modbus-tcp-client-master/examples/write_usb_serial.php(32): ModbusTcpClient\Network\BinaryStreamConnection->sendAndReceive()
#5 {main}

my code
use ModbusTcpClient\Network\BinaryStreamConnection;
use ModbusTcpClient\Packet\ModbusFunction\WriteSingleRegisterRequest;
use ModbusTcpClient\Packet\ModbusFunction\WriteSingleRegisterResponse;
use ModbusTcpClient\Packet\RtuConverter;
use ModbusTcpClient\Utils\Packet;

require DIR . '/../vendor/autoload.php';
require DIR . '/logger.php';

$connection = BinaryStreamConnection::getBuilder()
->setUri('/dev/ttyUSB0')
->setProtocol('serial')
->setIsCompleteCallback(static function ($binaryData, $streamIndex): bool {
return Packet::isCompleteLengthRTU($binaryData);
})
->setLogger(new EchoLogger())
->build();

@aldas
Copy link
Owner

aldas commented Jan 25, 2023

Seems that Packet::isCompleteLengthRTU is little buggy with for write* responses.

This should work for FC6 response:

->setIsCompleteCallback(static function ($binaryData, $streamIndex): bool {
return strlen($binaryData) == 8;
})

FC6 RTU response has 6 bytes of data + 2 bytes for CRC

* \x03 - unit id
* \x06 - function code
* \x00\x02 - start address
* \xFF\x00 - register data

@aldas
Copy link
Owner

aldas commented Jan 25, 2023

Hey, I have released new version 3.1.1 that fixes Packet::isCompleteLengthRTU to understand write response length correctly

@aldas aldas added the bug label Jan 26, 2023
@aldas
Copy link
Owner

aldas commented Jan 26, 2023

@ranjithphptech could you recheck with latest version?

@aldas aldas closed this as completed Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants