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. My modbus connection status stay on connecting forever.
Forum Updated to NodeBB v4.3 + New Features

My modbus connection status stay on connecting forever.

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.1k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    diuneiguohai
    wrote on last edited by
    #1

    I am trying to write a library of modbus receiving data from a server. the state never goes to the connected state. I have no idea whats the problem as I have seen examples of Qt modbus applications and documentary on modbus classes.

    my .pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2020-01-07T16:09:17
    #
    #-------------------------------------------------
    
    QT       += core gui
    QT += serialbus
    
    TARGET = Modbus
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    CONFIG += c++11
    CONFIG += console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    SOURCES += \
            Modbus.cpp \
            main.cpp
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    HEADERS += \
        Modbus.h
    
    # Default rules for deployment.
    
    
    

    my .h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QModbusClient>
    #include <QModbusDevice>
    #include <QModbusTcpClient>
    #include <QString>
    
    
    class Modbus_connection
    {
    
    public:
    
        QString ip;
        int port;
        int startaddress;
        int valuecount;
        QModbusTcpClient*modbusDevice;
        QModbusDataUnit*modbusData;
    
    
        void connect( QString ip,int port);
        void recieve(int startaddress,int valuecount );
    
    
    private:
    
    };
    
    #endif // Modbus_H
    
    
    
    

    my cpp

    #include "Modbus.h"
    #include <QDebug>
    #include <QModbusClient>
    #include <QModbusDevice>
    #include <QModbusTcpClient>
    #include <QModbusReply>
    #include <QObject>
    
    
    #include <QModbusDataUnit>
    
    
    
    void Modbus_connection::connect(QString ip,int port) {
    
        modbusDevice =new QModbusTcpClient();
    
    
        modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter,8000);
        modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter,"192.168.0.121");//ip);
        modbusDevice->setTimeout(500);
        modbusDevice->setNumberOfRetries(3);
    
        qDebug() << modbusDevice->state();
        qDebug() << modbusDevice->connectionParameter(5) << modbusDevice->connectionParameter(6);
    
        if(modbusDevice->connectDevice()){
            qDebug() << modbusDevice->state();
        }
        else{
            qDebug() <<"fk2"<< "/n"<< modbusDevice->connectDevice();
            qDebug() << modbusDevice->state() ;
        }
        while(modbusDevice->state() != QModbusDevice::ConnectedState);
        qDebug() << modbusDevice->state() ;
        qDebug() << modbusDevice->error()<< modbusDevice->errorString();
    
        if (auto *reply = modbusDevice->sendReadRequest(QModbusDataUnit(static_cast<QModbusDataUnit::RegisterType> (2),0,10), 1)) {
        }
    }
    

    out put

    QModbusDevice::UnconnectedState
    QVariant(int, 8000) QVariant(QString, "192.168.0.121")
    QModbusDevice::ConnectingState
    
    
    J.HilkJ 1 Reply Last reply
    1
    • D diuneiguohai

      I am trying to write a library of modbus receiving data from a server. the state never goes to the connected state. I have no idea whats the problem as I have seen examples of Qt modbus applications and documentary on modbus classes.

      my .pro

      #-------------------------------------------------
      #
      # Project created by QtCreator 2020-01-07T16:09:17
      #
      #-------------------------------------------------
      
      QT       += core gui
      QT += serialbus
      
      TARGET = Modbus
      
      # The following define makes your compiler emit warnings if you use
      # any feature of Qt which has been marked as deprecated (the exact warnings
      # depend on your compiler). Please consult the documentation of the
      # deprecated API in order to know how to port your code away from it.
      
      
      # You can also make your code fail to compile if you use deprecated APIs.
      # In order to do so, uncomment the following line.
      # You can also select to disable deprecated APIs only up to a certain version of Qt.
      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
      
      CONFIG += c++11
      CONFIG += console
      CONFIG -= app_bundle
      
      TEMPLATE = app
      
      SOURCES += \
              Modbus.cpp \
              main.cpp
      
      DEFINES += QT_DEPRECATED_WARNINGS
      
      HEADERS += \
          Modbus.h
      
      # Default rules for deployment.
      
      
      

      my .h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QModbusClient>
      #include <QModbusDevice>
      #include <QModbusTcpClient>
      #include <QString>
      
      
      class Modbus_connection
      {
      
      public:
      
          QString ip;
          int port;
          int startaddress;
          int valuecount;
          QModbusTcpClient*modbusDevice;
          QModbusDataUnit*modbusData;
      
      
          void connect( QString ip,int port);
          void recieve(int startaddress,int valuecount );
      
      
      private:
      
      };
      
      #endif // Modbus_H
      
      
      
      

      my cpp

      #include "Modbus.h"
      #include <QDebug>
      #include <QModbusClient>
      #include <QModbusDevice>
      #include <QModbusTcpClient>
      #include <QModbusReply>
      #include <QObject>
      
      
      #include <QModbusDataUnit>
      
      
      
      void Modbus_connection::connect(QString ip,int port) {
      
          modbusDevice =new QModbusTcpClient();
      
      
          modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter,8000);
          modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter,"192.168.0.121");//ip);
          modbusDevice->setTimeout(500);
          modbusDevice->setNumberOfRetries(3);
      
          qDebug() << modbusDevice->state();
          qDebug() << modbusDevice->connectionParameter(5) << modbusDevice->connectionParameter(6);
      
          if(modbusDevice->connectDevice()){
              qDebug() << modbusDevice->state();
          }
          else{
              qDebug() <<"fk2"<< "/n"<< modbusDevice->connectDevice();
              qDebug() << modbusDevice->state() ;
          }
          while(modbusDevice->state() != QModbusDevice::ConnectedState);
          qDebug() << modbusDevice->state() ;
          qDebug() << modbusDevice->error()<< modbusDevice->errorString();
      
          if (auto *reply = modbusDevice->sendReadRequest(QModbusDataUnit(static_cast<QModbusDataUnit::RegisterType> (2),0,10), 1)) {
          }
      }
      

      out put

      QModbusDevice::UnconnectedState
      QVariant(int, 8000) QVariant(QString, "192.168.0.121")
      QModbusDevice::ConnectingState
      
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      hi @diuneiguohai and welcome

      first of, it's very important to know your Qt version (the one from your Kit not the one from Creator) and the target operating System.

      QModbus when through a couple of faulty releases 😔


      Woa, what's this ?

      while(modbusDevice->state() != QModbusDevice::ConnectedState);
          qDebug() << modbusDevice->state() ;
      

      !?!?

      This is blocking your event loop! No signals will work -> State will not change.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      D 1 Reply Last reply
      5
      • D Offline
        D Offline
        diuneiguohai
        wrote on last edited by
        #3

        but it writes and read read normally on the modbus master exmaple

        my version is 5.13

        I heard the API of the QModbusDevice classes are asynchronous. therefore this was for testing purposes to see if it would be connected in the end.

        even without that line, it's still in the state of connecting after 5 mins of running.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          diuneiguohai
          wrote on last edited by
          #4

          here is my main that is forgot

          main

          #include "Modbus.h"
          #include <QCoreApplication>
          #include <QString>
          #include <QDebug>
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              Modbus_connection device;
              device.ip = "192.168.0.121";
              device.port = 8000;
              device.connect(device.ip,device.port);
              return a.exec();
          }
          
          
          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            hi @diuneiguohai and welcome

            first of, it's very important to know your Qt version (the one from your Kit not the one from Creator) and the target operating System.

            QModbus when through a couple of faulty releases 😔


            Woa, what's this ?

            while(modbusDevice->state() != QModbusDevice::ConnectedState);
                qDebug() << modbusDevice->state() ;
            

            !?!?

            This is blocking your event loop! No signals will work -> State will not change.

            D Offline
            D Offline
            diuneiguohai
            wrote on last edited by
            #5

            @J-Hilk it is on windows qt5.13

            J.HilkJ 1 Reply Last reply
            0
            • D diuneiguohai

              @J-Hilk it is on windows qt5.13

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @diuneiguohai
              ok,

              1. update at least to the latest 5.13 release ( 5.13.2 I believe) the 0 releases are always seeded with errors, not enough beta testers, sadly.

              2. remove all whiles and if from your Modbus_connection::connect(QString ip,int port) slot and use the signals it emits
                Listen to the errorOccurred(QModbusDevice::Error error) and stateChanged(QModbusDevice::State state) signals

              void Modbus_connection::connect(QString ip,int port) {
              
                  modbusDevice =new QModbusTcpClient();
              
              
                  modbusDevice->setConnectionParameter(QModbusDevice::NetworkPortParameter,8000);
                  modbusDevice->setConnectionParameter(QModbusDevice::NetworkAddressParameter,"192.168.0.121");//ip);
                  modbusDevice->setTimeout(500);
                  modbusDevice->setNumberOfRetries(3);
              
                  qDebug() << modbusDevice->state();
                  qDebug() << modbusDevice->connectionParameter(5) << modbusDevice->connectionParameter(6);
              
                  connect(modbusDevice, &QModbusTcpClient::stateChanged, this, [](QModbusDevice::State state)->void{qDebug() << "State changed to:" << state});
                 connect(modbusDevice, &QModbusTcpClient::errorOccurred, this, [](QModbusDevice::Error error)->void{qDebug() << "error happened :" << error});
               
                  modbusDevice->connectDevice())
                    
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              5
              • D Offline
                D Offline
                diuneiguohai
                wrote on last edited by
                #7

                thanks for your reply
                I tried to install the version of 5.13.2 (it took a very long time) then 64bit version disappeared.
                may update you later.

                beeckscheB 1 Reply Last reply
                0
                • D diuneiguohai

                  thanks for your reply
                  I tried to install the version of 5.13.2 (it took a very long time) then 64bit version disappeared.
                  may update you later.

                  beeckscheB Offline
                  beeckscheB Offline
                  beecksche
                  wrote on last edited by
                  #8

                  @diuneiguohai

                  The example is maybe also helpful

                  1 Reply Last reply
                  1
                  • C Offline
                    C Offline
                    Canaan
                    wrote on last edited by
                    #9

                    Hi!
                    Has your problem been solved?

                    1 Reply Last reply
                    0
                    • Z Offline
                      Z Offline
                      Zac.Boussaid
                      wrote on last edited by
                      #10

                      @J-Hilk said in My modbus connection status stay on connecting forever.:

                      connect(modbusDevice, &QModbusTcpClient::errorOccurred, this, [](QModbusDevice::Error error)->void{qDebug() << "error happened :" << error});
                      check this link you will find a solution:
                      https://forum.qt.io/topic/125391/modbusclient-as-a-console-application-stuck-in-connectingstate-loop/3

                      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