Results 1 to 3 of 3

Thread: QtModbus Help

  1. #1
    Join Date
    Jul 2019
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question QtModbus Help

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtModbus Help

    Are you making sure that you call the read function only after the device object has signalled it is in connected state?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    facste (20th July 2019)

  4. #3
    Join Date
    Jul 2019
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QtModbus Help

    I hope yes but... I try to check modbusDevice->state(); and in effect it is afer connection in QModbusDevice::ConnectingState. Maybe i need other instruction for connection, or maybe there is a problem in localhost slave?

    Thanks for reply!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.