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. Problem with running timer in another thread
Qt 6.11 is out! See what's new in the release blog

Problem with running timer in another thread

Scheduled Pinned Locked Moved Solved General and Desktop
44 Posts 9 Posters 19.3k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #32

    Pass the correct parent to your timer.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    0
    • D Damian7546

      @J-Hilk So, how can I fix this code ? Can you give me tips ?

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

      @Damian7546

      1. Fix your constructor of your QObject based worker: More of a principal matter in this case
      class SerialWorker : public QObject {
      	Q_OBJECT
      	public:
      
      		/// Constructor
      		SerialWorker();
      ...->...
      class SerialWorker : public QObject {
      	Q_OBJECT
      	public:
      
      		/// Constructor
      		explicit SerialWorker(QObbject *parent = nullptr);
      .....
      SerialWorker::SerialWorker()
      {
      ...->...
      SerialWorker::SerialWorker(QObject*parent) : QObject(parent)
      {
      
      1. Drop the scoped pointer, you're trying to be fancy when there's no need for it and it's causing trouble
      QScopedPointer<QSerialPort> serial_port_; 
      ...->
      QSerialPort * const serial_port_; //Raw dogging the serial port, hell ya 
      
      1. Init the QSerialport correctly:
      SerialWorker::SerialWorker(QObject*parent) : QObject(parent), serial_port_{new QSerialPort(this)}
      {
      

      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.

      Pl45m4P 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @Damian7546

        1. Fix your constructor of your QObject based worker: More of a principal matter in this case
        class SerialWorker : public QObject {
        	Q_OBJECT
        	public:
        
        		/// Constructor
        		SerialWorker();
        ...->...
        class SerialWorker : public QObject {
        	Q_OBJECT
        	public:
        
        		/// Constructor
        		explicit SerialWorker(QObbject *parent = nullptr);
        .....
        SerialWorker::SerialWorker()
        {
        ...->...
        SerialWorker::SerialWorker(QObject*parent) : QObject(parent)
        {
        
        1. Drop the scoped pointer, you're trying to be fancy when there's no need for it and it's causing trouble
        QScopedPointer<QSerialPort> serial_port_; 
        ...->
        QSerialPort * const serial_port_; //Raw dogging the serial port, hell ya 
        
        1. Init the QSerialport correctly:
        SerialWorker::SerialWorker(QObject*parent) : QObject(parent), serial_port_{new QSerialPort(this)}
        {
        
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #34

        @J-Hilk said in Problem with running timer in another thread:

        Fix your constructor of your QObject based worker: More of a principal matter in this case

        Drop the scoped pointer, you're trying to be fancy when there's no need for it and it's causing trouble

        Init the QSerialport correctly:

        TBH these things are also "weird" / designed badly in the original repository, @Damian7546 just copied/forked this structure.
        I'm wondering if any of the original stuff actually works... from looking at it I would say it also has many issues.
        Usually just including the class inQObject's parent-child structure and therefore moving the thread affinity with the parent would fix this. That's what I wrote/thought earlier, but it seems that this is not so easy to do due to the way the whole thing is designed.

        QSerialPort * const serial_port_; //Raw dogging the serial port, hell ya 
        

        LMAO :D


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • D Offline
          D Offline
          Damian7546
          wrote on last edited by
          #35

          @J-Hilk After changes proposed by you , the logs are this:

          "* Coin acceptor configured device: /dev/ttyUSB0"
          "* Opening port \"/dev/ttyUSB0\"."
          "Requested device state change from ShutDown to: Initialized"
          "* Port \"/dev/ttyUSB0\" opened."
          "> ccTalk request: SimplePoll, address: 2, data: (empty)"
          "Starting poll timer."
          "> Request: 020001feff"
          "Polling......"
          "< Full response: 020001feff01000200fd"
          "< ccTalk response from address 2, data: (empty)"
          "* Device is alive (answered to simple poll)"
          QObject::startTimer: Timers cannot be started from another thread
          "Polling......"
          "Polling......"
          "Polling......"
          "- The last message was repeated 40 times total"
          "- The last message was repeated 80 times total"
          "- The last message was repeated 120 times total"
          "- The last message was repeated 160 times total"
          
          JonBJ Pl45m4P 2 Replies Last reply
          0
          • D Damian7546

            @J-Hilk After changes proposed by you , the logs are this:

            "* Coin acceptor configured device: /dev/ttyUSB0"
            "* Opening port \"/dev/ttyUSB0\"."
            "Requested device state change from ShutDown to: Initialized"
            "* Port \"/dev/ttyUSB0\" opened."
            "> ccTalk request: SimplePoll, address: 2, data: (empty)"
            "Starting poll timer."
            "> Request: 020001feff"
            "Polling......"
            "< Full response: 020001feff01000200fd"
            "< ccTalk response from address 2, data: (empty)"
            "* Device is alive (answered to simple poll)"
            QObject::startTimer: Timers cannot be started from another thread
            "Polling......"
            "Polling......"
            "Polling......"
            "- The last message was repeated 40 times total"
            "- The last message was repeated 80 times total"
            "- The last message was repeated 120 times total"
            "- The last message was repeated 160 times total"
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #36

            @Damian7546
            At this point I doubt that anyone can reliably know what exactly your code reads. You clearly still have a thread issue.

            I am unsure: have you identified where the startTimer() call is issued from in code?

            1 Reply Last reply
            0
            • D Damian7546

              @J-Hilk After changes proposed by you , the logs are this:

              "* Coin acceptor configured device: /dev/ttyUSB0"
              "* Opening port \"/dev/ttyUSB0\"."
              "Requested device state change from ShutDown to: Initialized"
              "* Port \"/dev/ttyUSB0\" opened."
              "> ccTalk request: SimplePoll, address: 2, data: (empty)"
              "Starting poll timer."
              "> Request: 020001feff"
              "Polling......"
              "< Full response: 020001feff01000200fd"
              "< ccTalk response from address 2, data: (empty)"
              "* Device is alive (answered to simple poll)"
              QObject::startTimer: Timers cannot be started from another thread
              "Polling......"
              "Polling......"
              "Polling......"
              "- The last message was repeated 40 times total"
              "- The last message was repeated 80 times total"
              "- The last message was repeated 120 times total"
              "- The last message was repeated 160 times total"
              
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #37

              @Damian7546

              Since you probably used the code from the original GitHub repo without modifying it, you should reach out to the owner of the repo via GitHub issues.
              Maybe he can identify the problem as a whole faster than a bunch of people in the forum here, who have to dive into the project first and dig through the logic to understand how everything is put together.
              As @J-Hilk mentioned above, there are a couple of things that could have been done cleaner...
              (creating QObject derived worker without actually calling the QObject base implementation... etc...)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Damian7546
                wrote on last edited by
                #38

                @JonB
                My opinion it call only once, in the CctalkDevice::requestSwitchDeviceState (cctalk_device.cpp) , when the device goes to Initialized.
                241 linie: https://github.com/ienecode/cctalk/blob/main/cctalk/cctalk_device.cpp

                @Pl45m4
                I contacted the author, and he tells me that this code was developed for a project that was stopped before there was a chance to properly test the code,
                so this is the state it was left in.

                I interesed in this code, because I think that the structure of this code for SERIAL communication seems very reasonable and developmental to me.

                So I would be really grateful if anyone of forumers could be help me run this code , especially to run cyclic polling , in way is realized in this code.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #39

                  I don't see a reason to use threads for simple serial communication with QSerialPort at all. It just complicates things, esp. for people without proper knowledge about threads. Don't use it - simply use QSerialPort and proper signals & slots.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    Damian7546
                    wrote on last edited by Damian7546
                    #40

                    @Christian-Ehrlicher I expected such answers: "simply use QSerialPort and proper signals & slots." . I have project realized in this way.
                    But as part of learning (main goal of the project) programming and uing threads I would like to use threads this time to serial communication, and run example considered in this topic.

                    JonBJ Pl45m4P Christian EhrlicherC 3 Replies Last reply
                    0
                    • D Damian7546

                      @Christian-Ehrlicher I expected such answers: "simply use QSerialPort and proper signals & slots." . I have project realized in this way.
                      But as part of learning (main goal of the project) programming and uing threads I would like to use threads this time to serial communication, and run example considered in this topic.

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

                      @Damian7546
                      Then by choosing to use threads where they are probably not needed you (or the original author) have ended up with a problem using threads, possibly because the original code was not well written for threads. Which tells you a lot... ! Just saying.

                      1 Reply Last reply
                      0
                      • D Damian7546

                        @Christian-Ehrlicher I expected such answers: "simply use QSerialPort and proper signals & slots." . I have project realized in this way.
                        But as part of learning (main goal of the project) programming and uing threads I would like to use threads this time to serial communication, and run example considered in this topic.

                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by Pl45m4
                        #42

                        @Damian7546 said in Problem with running timer in another thread:

                        But as part of learning (main goal of the project) programming and uing threads I would like to use threads this time to serial communication, and run example considered in this topic.

                        Then you can look, for example, at Qt's Blocking Serial Port Examples:

                        (Sender)

                        • https://doc.qt.io/qt-6/qtserialport-blockingsender-example.html

                        (Receiver)

                        • https://doc.qt.io/qt-6/qtserialport-blockingreceiver-example.html

                        Or you use some non-Qt implementation of serial port communication, such as serial_port from Boost.ASIO library.
                        (makes little sense, since you are already using Qt which provides all that stuff... but for learning purposes it's fine to look at different techniques)

                        • https://www.boost.org/doc/libs/1_84_0/doc/html/boost_asio/overview/serial_ports.html

                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        0
                        • D Damian7546

                          @Christian-Ehrlicher I expected such answers: "simply use QSerialPort and proper signals & slots." . I have project realized in this way.
                          But as part of learning (main goal of the project) programming and uing threads I would like to use threads this time to serial communication, and run example considered in this topic.

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #43

                          @Damian7546 said in Problem with running timer in another thread:

                          But as part of learning (main goal of the project) programming and uing threads I would like to use threads this time to serial

                          Then look at the Qt documentation for Qt threads and it's examples instead some obscure git projects.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          1
                          • D Offline
                            D Offline
                            Damian7546
                            wrote on last edited by Damian7546
                            #44

                            Problem with QObject::startTimer: Timers cannot be started from another thread Solved!

                            I changed:

                            -All entries of the connect function, from:

                            connect(this, &CctalkLinkController::openPortInWorker, serial_worker_.data(), &SerialWorker::openPort, Qt::QueuedConnection);
                            

                            to:

                            connect(this, &CctalkLinkController::openPortInWorker, serial_worker_, &SerialWorker::openPort, Qt::QueuedConnection);
                            

                            Because I don't have any idea what is data() ?

                            And like suggested @J.Hilk in CctalkLinkController I changed:

                            CctalkLinkController::CctalkLinkController()
                            {
                                serial_worker_ = new SerialWorker();
                            

                            to

                            CctalkLinkController::CctalkLinkController(QObject *parent) : QObject(parent), serial_worker_{new SerialWorker(this)}
                            {
                            
                            1 Reply Last reply
                            0
                            • D Damian7546 has marked this topic as solved on
                            • D Damian7546 has marked this topic as solved on
                            • D Damian7546 has marked this topic as solved on

                            • Login

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