aldas / modbus-tcp-client

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

An exception occurred Response null or data length too short to be valid packet! #42

Closed gupta-shrinath closed 5 years ago

gupta-shrinath commented 5 years ago

Hi There Thanks for the library and I am new to Modbus protocol so this question might be naive. I used the library with Modbus Slave Software and it worked flawlessly but when I connect my laptop to actually devices an exception is thrown response packet to be null. I have 2 devices connected to the RS485 to Modbus TCP Converter and the setting for the devices are Baud Rate 9600 bits/second Data Size 8 bits/character Parity Odd Port Number502 Network Protocol TCP RS485 to Modbus TCP Convertor IP is 169.254.205.239 And here is my code `<?php include 'database.php'; use ModbusTcpClient\Network\BinaryStreamConnection; use ModbusTcpClient\Packet\ModbusFunction\ReadHoldingRegistersRequest; use ModbusTcpClient\Packet\ModbusFunction\ReadHoldingRegistersResponse; use ModbusTcpClient\Packet\ResponseFactory; require DIR . '/vendor/autoload.php'; //$server_ip = getHostByName(getHostName()); $server_ip = '169.254.205.239'; //echo "Server IP Address is: $server_ip"; $connection = BinaryStreamConnection::getBuilder() ->setPort(502) ->setHost($server_ip) ->build(); $startAddress = 40043; $quantity = 10; $packet = new ReadHoldingRegistersRequest($startAddress, $quantity); //echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; try { $binaryData = $connection->connect() ->sendAndReceive($packet); //echo 'Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL; /**

aldas commented 5 years ago
  1. by default unitID/SlaveId is 0. Maybe you need to use different slaveID ala 1

    $slaveID = 1;
    $packet = new ReadHoldingRegistersRequest($startAddress, $quantity, $slaveID);
  2. maybe you need to wait a little before reading change from using sendAndReceive to

    $connection->connect()->send($packet);
    usleep(100000); // wait 100ms
    $binaryData = $connection->receive();
  3. use wireshark to see what Modbus Slave Software sends and compare it to what is library sends.

gupta-shrinath commented 5 years ago
1. by default unitID/SlaveId is 0. Maybe you need to use different slaveID ala 1
$slaveID = 1;
$packet = new ReadHoldingRegistersRequest($startAddress, $quantity, $slaveID);
1. maybe you need to wait a little before reading
   change from using `sendAndReceive` to
    $connection->connect()->send($packet);
    usleep(100000); // wait 100ms
    $binaryData = $connection->receive();
1. use wireshark to see what `Modbus Slave Software` sends and compare it to what is library sends.

I tried the solutions you gave but it didn't work. Actually, the data packet sent is fine but the response packet is null for the reasons i don't know

gupta-shrinath commented 5 years ago

Packet to be sent (in hex): ad8500000006010300040002 Binary received (in hex): 00 An exception occurred Packet crc (\x00) does not match calculated crc (\xffff)! #0 C:\xampp\htdocs\modbus\readRegister.php(30): ModbusTcpClient\Packet\RtuConverter::fromRtu('\x00') #1 C:\xampp\htdocs\modbus\HRLoad.php(13): include('C:\xampp\htdocs...') #2 {main}

aldas commented 5 years ago

maybe you could try with some other client ala

just to see if it works at all.

gupta-shrinath commented 5 years ago

Earlier I did a mistake I made the packet for modbus tcp while I was sending it to the modbus rtu devices.I saw the issue 'This project supports Modbus RTU' and i am facing readtimeout even on increased readtimeout sec I have posted the reply in the closed issue please kindly do take a look