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. QtModbus doesn't reconnect programmatically
Forum Updated to NodeBB v4.3 + New Features

QtModbus doesn't reconnect programmatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.7k Views 3 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    In my application I have two QAction for connecting and disconnecting:

    
    QModbusClient *_modbus;
    
    void modbusConnect(QString host, int port)
    {
        if (_modbus)
        {
            _modbus->disconnectDevice();
            delete _modbus;
        }
    
        _modbus = new QModbusTcpClient(this);
        _modbus->setConnectionParameter(QModbusDevice::NetworkPortParameter, port);
        _modbus->setConnectionParameter(QModbusDevice::NetworkAddressParameter, host);    
    
        _modbus->setTimeout(250);
        _modbus->setNumberOfRetries(1);
    
        qDebug() << _modbus->connectDevice();
        _timerConnectionTimeout.start();
    }
    
    void modbusDisconnect()
    {
        if (_modbus) _modbus->disconnectDevice();
    }
    
    void timerConnection_timeout()
    {
        _modbus->disconnectDevice();
        emit timeoutConnection();
    }
    

    First question: even if the remote device is not available the connectDevice() function returns always true. But the documentation says:

    Connects the device to the Modbus network. Returns true on success; otherwise false.

    Hence if it cannot connect the device to the Modbus network it should return false. Am I wrong?

    Now I have to handle an automatic re-connection. That means if the device is connected and then it's not reachable anymore (i.e. I turn it off) when it will be available again I want to reconnect to it. Hence I did something very simple: when a reading timeouts I do the following:

    ui->actionDisconnect->trigger();
    QTimer::singleShot(1000, ui->actionConnect, &QAction::trigger);
    

    Those QAction will call the two functions above. Well, something weird happens: with this code the _modbus->state() will remain ConnectingState forever. Even when the device is available - I mean it does not connect at all.

    Instead if I click those actions with the mouse, it will connect immediately!
    The code executed is the very same... why it doesn't work programmatically? Is there a better way to reconnect?

    1 Reply Last reply
    0
    • AndySA Offline
      AndySA Offline
      AndyS
      Moderators
      wrote on last edited by AndyS
      #2

      Hi @Mark81,

      If the device is not there and it is returning true when calling connectToDevice(), then this sounds like a bug. Can you report this on JIRA with an example there to reproduce it? Thanks.

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

      M 1 Reply Last reply
      0
      • AndySA AndyS

        Hi @Mark81,

        If the device is not there and it is returning true when calling connectToDevice(), then this sounds like a bug. Can you report this on JIRA with an example there to reproduce it? Thanks.

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @AndyS Here the issue: https://bugreports.qt.io/browse/QTBUG-71282. It's the first time I created one - I hope I did it well!

        1 Reply Last reply
        3
        • AndySA Offline
          AndySA Offline
          AndyS
          Moderators
          wrote on last edited by
          #4

          Hi @Mark81,

          You did it right thanks!

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

          Pablo J. RoginaP 1 Reply Last reply
          1
          • AndySA AndyS

            Hi @Mark81,

            You did it right thanks!

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @AndyS if your issue is solved, please don't forget to mark your post as such! Thanks.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            AndySA 1 Reply Last reply
            0
            • Pablo J. RoginaP Pablo J. Rogina

              @AndyS if your issue is solved, please don't forget to mark your post as such! Thanks.

              AndySA Offline
              AndySA Offline
              AndyS
              Moderators
              wrote on last edited by
              #6

              @Pablo-J.-Rogina It's not my issue :)

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

              1 Reply Last reply
              0
              • aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Neither is mine :)

                Anyway, closing this as there is nothing we can do here anymore.

                Qt has to stay free or it will die.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mark81
                  wrote on last edited by
                  #8

                  Sorry guys, but the issue is not solved at all! We've just discovered a documentation bug - but that wasn't the main issue.

                  The big problem, still there, is the second part of the question. Why does reconnection work only if triggered by a user click and it doesn't executing the same code using a QTimer?

                  aha_1980A 1 Reply Last reply
                  0
                  • M Mark81

                    Sorry guys, but the issue is not solved at all! We've just discovered a documentation bug - but that wasn't the main issue.

                    The big problem, still there, is the second part of the question. Why does reconnection work only if triggered by a user click and it doesn't executing the same code using a QTimer?

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Mark81 don't connect the timer to the action, connect it to the corresponding slot instead.

                    Qt has to stay free or it will die.

                    M 1 Reply Last reply
                    2
                    • beeckscheB Offline
                      beeckscheB Offline
                      beecksche
                      wrote on last edited by beecksche
                      #10

                      The QtModubus module has an asynchronous API. So by calling connectDevice() or disconnectDevice() your client is not connected/disconnected to the device immediately, but rather in ConnectingState/ClosingState. You have to receive the signal stateChanged() to ensure the connection state of your client. Only when the state of the client is ConnectedState your client is connected.

                      Also when you want to reconnect, I would ensure that you the state of the client is UnconnectedState.

                      Or just do a QHostInfo::lookupHost . Once the lookup was successfull you can connect again.

                      M 1 Reply Last reply
                      2
                      • aha_1980A aha_1980

                        @Mark81 don't connect the timer to the action, connect it to the corresponding slot instead.

                        M Offline
                        M Offline
                        Mark81
                        wrote on last edited by
                        #11

                        @aha_1980 said in QtModbus doesn't reconnect programmatically:

                        @Mark81 don't connect the timer to the action, connect it to the corresponding slot instead.

                        Tried: nothing change.

                        aha_1980A 1 Reply Last reply
                        0
                        • beeckscheB beecksche

                          The QtModubus module has an asynchronous API. So by calling connectDevice() or disconnectDevice() your client is not connected/disconnected to the device immediately, but rather in ConnectingState/ClosingState. You have to receive the signal stateChanged() to ensure the connection state of your client. Only when the state of the client is ConnectedState your client is connected.

                          Also when you want to reconnect, I would ensure that you the state of the client is UnconnectedState.

                          Or just do a QHostInfo::lookupHost . Once the lookup was successfull you can connect again.

                          M Offline
                          M Offline
                          Mark81
                          wrote on last edited by
                          #12

                          @beecksche said in QtModbus doesn't reconnect programmatically:

                          The QtModubus module has an asynchronous API. So by calling connectDevice() or disconnectDevice() your client is not connected/disconnected to the device immediately, but rather in ConnectingState/ClosingState. You have to receive the signal stateChanged() to ensure the connection state of your client. Only when the state of the client is ConnectedState your client is connected.

                          Also when you want to reconnect, I would ensure that you the state of the client is UnconnectedState.

                          I'm aware of this, but I have some thoughts about:

                          • I cannot wait such a long time: the built-in timeout is too long for my needs. Hence when I want to disconnect and reconnect I must use my own timeout timer

                          • I understand it might not be the best approach, but after calling disconnectDevice() I actually delete the object. It's not enough to force a disconnection?

                          • Anyway, I don't understand why it works clicking the action on the UI, even one right the other, and it doesn't by code, even waiting few seconds

                          1 Reply Last reply
                          0
                          • M Mark81

                            @aha_1980 said in QtModbus doesn't reconnect programmatically:

                            @Mark81 don't connect the timer to the action, connect it to the corresponding slot instead.

                            Tried: nothing change.

                            aha_1980A Offline
                            aha_1980A Offline
                            aha_1980
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Mark81

                            Then either debug it yourself, or provide more code.

                            Are you sure ui->actionDisconnect->trigger(); does what you expect?

                            It should work the same way with a timer as it would work with a button.

                            Qt has to stay free or it will die.

                            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