Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Modbusclient as a console application, "Stuck in Connectingstate loop"



  • Hey guys,
    I'm trying hardly to convert the Modbusmaster example (qt example) which is a widget application to a console application. I wanted to build a connection between a local slave and my master. The problem I'm facing is that my code is changing its state to “Connecting state” ,gets stuck and doesn't want to build a connection. That's why, I recon that the Modbus library is limited and it's only compatible with the widget form.
    Could someone tell me if my guesses are right.
    down below you will find my code:

    #include <QCoreApplication>
    #include <QDebug>
    #include <QModbusDataUnit>
    #include<iostream>
    #include <QTimer>// this bib was add to the 50ms Loop check
    #include <string>
    #include <QString>
    #include <QThread>
    #include <QModbusTcpClient>
    #include <QModbusDataUnit>
    #include <QUrl>
    
    
    
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Mainmodbus T ;
        T.modbusDevice = new QModbusTcpClient();
    
    
    
    
        if (!T.modbusDevice) qDebug()<<"error 1";
    
    
        if (T.modbusDevice->state() != QModbusDevice::ConnectedState) {
    
    
                T.modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "127.0.0.1");
                T.modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter, 502);
    
                T.modbusDevice->connectDevice();
    
    
                
                while(true){
                qDebug()<<T.modbusDevice->state();}
    
    
    
                }
    
        return a.exec();
    }
    
    

    picconsole.png



  • @Zac-Boussaid said in Modbusclient as a console application, "Stuck in Connectingstate loop":

            while(true){
            qDebug()<<T.modbusDevice->state();}
    

    You must not block Qt like this. The connection will not happen while you are in this loop. You must use signals & slots in Qt.



  • @JonB hey, i'm really thankfull for your help and i cannot describe how happy I am. i changed my code as you told me and here is the new code

     QCoreApplication a(argc, argv);
    
        Mainmodbus T ;
        QModbusTcpClient*modbusDevice;
        modbusDevice =new QModbusTcpClient();
    
    
          modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter,502);
          modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter,"127.0.0.1");
          modbusDevice->setTimeout(500);
          modbusDevice->setNumberOfRetries(1);
    
          qDebug() << modbusDevice->state();
          qDebug() << modbusDevice->connectionParameter(1);
    
    
          qDebug() << modbusDevice->state() ;
          modbusDevice->connectDevice();
          qDebug() << modbusDevice->error()<< modbusDevice->errorString();
          QObject::connect(modbusDevice,&QModbusTcpClient::stateChanged,&a,[](QModbusDevice::State state)->void{qDebug() << "State changed to:" << state;});
          QObject::connect(modbusDevice, &QModbusTcpClient::errorOccurred, &a, [](QModbusDevice::Error error)->void{qDebug() << "error happened :" << error;});
          qDebug() << modbusDevice->state() ;
    
    


  • @Zac-Boussaid
    So this now works for you?



  • @JonB yes it worked
    now i need to change it to a function.


Log in to reply