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. QTimer in Threads Emits Time out at Wrong Timing

QTimer in Threads Emits Time out at Wrong Timing

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 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.
  • Dayama PradeepD Offline
    Dayama PradeepD Offline
    Dayama Pradeep
    wrote on last edited by Dayama Pradeep
    #1

    The idea is to achieve serial communication using multi-threading at every 10ms.
    Platform used: Linux, Python and Pyqt for UI, PySerial for Serial Communication.

    Thread creation code snippet is given below:

      self.objThread[i] = QThread()  
    

    Starting Timer in Thread class, as shown below:

    self.ctimer = QTimer() 
    self.ctimer.timeout.connect(self.readWriteData) 
    self.ctimer.start(self.TIME_INTERVAL)
    

    In TimeOut slot, just calling Transmit and Receive Routine

    def readWriteData(self): 
         print("Time = ",datetime.now().strftime('%d:%m:%Y_%H:%M:%S.%f')[:-3]) 
         if(temp_packet_rate != 'aperiodic'):
             if(self.serial_operation_flag[self.index] == 1):      
                if(self.Transfer_direction == 'T'):
                   self.Transmit_Routine()
                elif(self.Transfer_direction == 'R'):   
                   self.Receive_Routine()
    

    In above Routine, Printing Time. Could make out that the above routine is called beyond 10ms, something like 11, 12.. some times more than 30ms.

    Obersvation:
    In transmit Routine, when the Data is put on serial Out buffer, it takes more time, here is the routine

    ser_handler[index].write(transmit_data_edited.encode())
    

    Serial Handler is initiated with all details like, Port name, Baud Rate, Parity and Stop bit. Time out is set to 0.

    Please suggest me where the mistake could be. How could 10ms timing can be acheived

    Thanks in advance

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You have to ensure that everything you do in readWriteData takes less than 10ms including your print statement.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Dayama PradeepD 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        You have to ensure that everything you do in readWriteData takes less than 10ms including your print statement.

        Dayama PradeepD Offline
        Dayama PradeepD Offline
        Dayama Pradeep
        wrote on last edited by
        #3

        @SGaist yes.. print statement takes more time.. so printing timing into a file, after each step. also when i disable writing data into serial buffer, timing is fine. But once enebled that, found that Qtimer goes for toss

        J.HilkJ 1 Reply Last reply
        0
        • K Offline
          K Offline
          karlheinzreichel
          wrote on last edited by
          #4

          Using QTimer for precise timing is conceptually not a good idea.
          see QTimer docs :

          "All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy"

          regards
          Karl-Heinz

          Dayama PradeepD 1 Reply Last reply
          0
          • Dayama PradeepD Dayama Pradeep

            @SGaist yes.. print statement takes more time.. so printing timing into a file, after each step. also when i disable writing data into serial buffer, timing is fine. But once enebled that, found that Qtimer goes for toss

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

            @Dayama-Pradeep
            You can also start a QElapsedTimer at the beginning of readWriteData substract that from your 10 ms and start the Time new with the modified time.

            Should reduce the shift.


            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.

            Dayama PradeepD 1 Reply Last reply
            0
            • K karlheinzreichel

              Using QTimer for precise timing is conceptually not a good idea.
              see QTimer docs :

              "All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy"

              regards
              Karl-Heinz

              Dayama PradeepD Offline
              Dayama PradeepD Offline
              Dayama Pradeep
              wrote on last edited by
              #6

              @karlheinzreichel yes..

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @Dayama-Pradeep
                You can also start a QElapsedTimer at the beginning of readWriteData substract that from your 10 ms and start the Time new with the modified time.

                Should reduce the shift.

                Dayama PradeepD Offline
                Dayama PradeepD Offline
                Dayama Pradeep
                wrote on last edited by
                #7

                @J.Hilk will look into it and revert back.. thanks for suggestion

                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