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

My modbus connection status stay on connecting forever.



  • 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
    
    

  • Moderators

    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.



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



  • 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();
    }
    
    


  • @J-Hilk it is on windows qt5.13


  • Moderators

    @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())
          
    }
    


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



  • @diuneiguohai

    The example is maybe also helpful



  • Hi!
    Has your problem been solved?


Log in to reply