Hi guys, I need someone help to resolve my problem, I've created a program which connect with QtModbus via TCP to the server.
For prototype I use slave program created From qt which is connected on 127.0.0.1 port 502

with this code:
Qt Code:
  1. modbusDevice = new QModbusTcpClient(this);
  2. modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "127.0.0.1");
  3. modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, 502);
  4. if(!modbusDevice->connectDevice()){
  5. ui->lbl_result->setText("Error");
  6. return false;
  7. }
To copy to clipboard, switch view to plain text mode 
The program continue so I suppose that connection work

next I need to Read a register, for example Holding Register at address 1 inside the device so I code:

Qt Code:
  1. QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 1, 1);
  2. if (auto *reply = modbusDevice->sendReadRequest(readUnit, 1)){
  3. if (!reply->isFinished()){
  4. connect(reply, &QModbusReply::finished, this, &MainWindow::show_result);
  5. }else{
  6. delete reply;
  7. }
  8. }else
  9. ui->lbl_result->setText("Error");
  10.  
  11. return true;
To copy to clipboard, switch view to plain text mode 

and the show_result function is:

Qt Code:
  1. void MainWindow::show_result(){
  2. auto reply = qobject_cast<QModbusReply *>(sender());
  3. if (!reply){
  4. qDebug() << modbusDevice->errorString();
  5. return;
  6. }
  7.  
  8. if (reply->error() == QModbusDevice::NoError){
  9. const QModbusDataUnit unit = reply->result();
  10. int startAddress = unit.startAddress();
  11. int value = unit.value(0);
  12. qDebug() << startAddress + " valore: " + value;
  13. }
  14. else
  15. ui->lbl_result->setText("Error");
  16.  
  17. reply->deleteLater();
  18. }
To copy to clipboard, switch view to plain text mode 

But always when this function was called, return an error in the first line: "Device is not connected"

How I can fix this, I try to check all example or someting but i not find error.

Thanks for your help and attention