Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Modbusclient as a console application, "Stuck in Connectingstate loop"
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 607 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zac.Boussaid
    wrote on last edited by Zac.Boussaid
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • Z Zac.Boussaid

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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.

      Z 1 Reply Last reply
      3
      • JonBJ JonB

        @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.

        Z Offline
        Z Offline
        Zac.Boussaid
        wrote on last edited by
        #3

        @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() ;
        
        
        JonBJ 1 Reply Last reply
        0
        • Z Zac.Boussaid

          @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() ;
          
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Zac-Boussaid
          So this now works for you?

          Z 1 Reply Last reply
          0
          • JonBJ JonB

            @Zac-Boussaid
            So this now works for you?

            Z Offline
            Z Offline
            Zac.Boussaid
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved