Results 1 to 13 of 13

Thread: implemet a timer

  1. #1
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default implemet a timer

    Hello.

    Could someone tell me how can I implement a timer?

    I am reading and writing from the serial port and I want to wait for datas during a limit time and if there are datas in the port (in the establish time) read it. But if the timer expires stop the process.

    I am using qextserialport but I would like to know how do it in c++ too.

    Cheers.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    Hi,

    Use the SIGNAL "timeOut" to take a look if there is data avaiable. If there is data you can do wat yo want with it, but if there is no data you can stop the timer. Then you maybe want to close the serial port, ...
    Òscar Llarch i Galán

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    Quote Originally Posted by adamatic View Post
    Hello.

    Could someone tell me how can I implement a timer?

    I am reading and writing from the serial port and I want to wait for datas during a limit time and if there are datas in the port (in the establish time) read it. But if the timer expires stop the process.

    I am using qextserialport but I would like to know how do it in c++ too.

    Cheers.
    set timer some interval .. as
    timer->start(1000) //one second
    so the SIGNAL(timeOut()) will emit in every one second .. with that u can check all your conditions in your SLOT function

  4. #4
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: implemet a timer

    But what I want is to interrupt the reading after 10 seconds without receive datas. If I use

    timer->start(10000)
    SIGNAL(timeOut())

    I think the slot that I especify will be running always although it has been datas and I want to go to this slot only if there are not datas during the establish time.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: implemet a timer

    maybe this will help you
    Qt Code:
    1. ...
    2. QTimer::singleShot(10000, this, SLOT(terminateDataReceiving()));
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: implemet a timer

    I thinke i am going to use QTimer::timerId () in a loop, like:

    Qt Code:
    1. while(timerId ()!=-1 && continue){ //timerId ()!=-1 this means the timer is running
    2.  
    3. if( port->DataAvalable){
    4. port->read();
    5. continue=false; //stop the loop
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    if there arent reading data and continue==true means that the timer expired without have read datas from the port.

    What do you think about this loop? I would like to know your opinion, maybe there is a better solution.

    cheers
    Last edited by jpn; 16th February 2009 at 17:54. Reason: missing [code] tags

  7. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    try this
    Qt Code:
    1. timer->start(1000);
    2. connect(timer, SIGNAL(timeOut()),this, SLOT(timerCall()));
    3.  
    4. ::timerCall() //slot
    5. {
    6. if( port->DataAvalable){
    7. port->read();
    8. // can call any private function
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    so every time the timer calls this slot every 1 sec ... if data is available it will do the operation or there emit your own private signal

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    Qt Code:
    1. QTime currentTime, time;
    2. QTimer *timer = new QTimer();
    3. while(true)
    4. {
    5. if(port->dataAvailable)
    6. {
    7. timer->start();
    8. }
    9. if(!port->dataAvailable)
    10. {
    11. if(!timer->isActive())
    12. {
    13. currentTime = QTime::currentTime();
    14. if((currentTime - time ) >= 10000)
    15. break;
    16. }
    17. if(timer->isActive())
    18. {
    19. timer->stop();
    20. time = QTime::currentTime();
    21. }
    22.  
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    havent tested it, but should work
    Last edited by jpn; 16th February 2009 at 17:51. Reason: missing [code] tags

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    Hi,

    Not a good aproach to use an infinite loop and breaking it on this way.

    Use 2 QTimers:
    -First one that will fire ever X ms(depending on Baud Rate you have to calculate this) and will take a look if there is data avaiable.
    When the first timer fires, take a look if there is data to read, if there isn't, you have to check if the secon timer is started, if not started, start it.

    The second one will fire if you have not readed any data in 10 seconds. This will be a singleShot timer. When it fires, stop the first timer.
    Òscar Llarch i Galán

  10. #10
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    wont an infinite loop be necessary in this case...where we have to do something until a condition satisfies..which means we dont know until this condition will come true..i dont know why everyone is so against using 'break' in a loop..its just an equivalent of JMP in assembly which is probably the most common instruction in assembly

  11. The following user says thank you to talk2amulya for this useful post:

    adamatic (17th February 2009)

  12. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    Yes,

    You do work until some case is done.
    But having data on serial port is slower than every iteration of the bucle, so you will waste too much time by doing nothing.
    Using a timer with a time calculated to satisfy the Baud Rate will be a more elegant way.
    Òscar Llarch i Galán

  13. The following user says thank you to ^NyAw^ for this useful post:

    adamatic (17th February 2009)

  14. #12
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: implemet a timer

    makes sense..thanks for clearin that up..i didnt know abt the baud rate and stuff

  15. #13
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: implemet a timer

    I have finally decided use the idea of use 2 timers, checking every second the input port and stop both timers when it receives datas, and if there arent datas after 10 seconds go to another slot and stop them.

    Thank you very much to all the answers.

Similar Threads

  1. how to enable a timer in a non-gui thread?
    By zeopha in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 09:29
  2. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  3. Timer for application
    By Zergi in forum Qt Programming
    Replies: 3
    Last Post: 26th December 2007, 22:21
  4. Thread(s) and socket, timer slots
    By stephdev1965 in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2006, 14:04
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.