simple timer in main or separate thread



  • Hello!
    Im a beginner in qt, wants to receipt data from uart and to do it i need perodically monitor the receive buffer. I cant do it in while(1) or from button event. I ve seen many examples but all of these doesnt compile and I m not figureout where is a problem yet. Can somebody give simple example?, please
    Thanks in advance


  • Moderators

    @another_one Do you really think with the information you provided anybody will be able to help you?
    What exactly did you try? What was the error message? What example do you want to have? For timer examples you find already some in the documentation: http://doc.qt.io/qt-5/qtimer.html
    If something still doesn't work then please be more specific and show what you did and what errors you get.
    Regarding the title of your post: there is usually really no need to use a timer in a thread.



  • @another_one Hi,

    Don't use a QTimer, but either QSerialPort::readyRead() signal, which will notify you when data are available on the port.

    Qt already provide examples for QSerialPort (and for almost everything)
    I would suggest Terminal Example, Command Line Writer Async Example and Command Line Reader Async Example. Which seems to be close of what you want to achieve.
    Note that you can open these examples directly from QtCreator.



  • Hi @another_one , Welcome!

    Although @jsulm is correct about desiring more information, he could have been a little more tactful (I firmly believe what goes around comes around!) in conveying it. QSerialPort is your best bet as @Gojir4 explained. Use the dataReady to read the port.

    *** Be warned! SIGNALS and SLOTS work on the event queue in multiple threads. You can freeze out (or delay processing) if you do a lot of busy work in large routines.

    One technique I do use is to start a timer when I am waiting on a response from the serial port as a timeout. This is helpful for detecting loss of signal. Stop/reset the timer when data arrives depending on your use case.

    If you need to use the serial port in the background and not worry about interference with the user interface (either in responsiveness or port data processing) then run the serial port in a worker thread. The best thread option is the worker thread. See Maya's blog (https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/). I use this technique for both QSerialPort and QCanBUs.


  • Moderators

    @Buckwheat Thanks for the hint - I agree with you :-)
    People often ask questions which are hard to understand or do not provide enough information (like in this thread), if you see such questions again and again it is easy to loose patience, especially if you have a bad day :-)
    @another_one Sorry if I was rude! Try to be more precise if you ask a question, then it is easier to answer it :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.