PDA

View Full Version : implemet a timer



adamatic
16th February 2009, 12:45
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.

^NyAw^
16th February 2009, 12:51
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, ...

wagmare
16th February 2009, 13:05
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

adamatic
16th February 2009, 13:29
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.

spirit
16th February 2009, 13:36
maybe this will help you


...
QTimer::singleShot(10000, this, SLOT(terminateDataReceiving()));
...

adamatic
16th February 2009, 14:05
I thinke i am going to use QTimer::timerId () in a loop, like:


while(timerId ()!=-1 && continue){ //timerId ()!=-1 this means the timer is running

if( port->DataAvalable){
port->read();
continue=false; //stop the loop
}
}

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

wagmare
16th February 2009, 14:37
try this


timer->start(1000);
connect(timer, SIGNAL(timeOut()),this, SLOT(timerCall()));

::timerCall() //slot
{
if( port->DataAvalable){
port->read();
// can call any private function
}
}


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

talk2amulya
16th February 2009, 14:38
QTime currentTime, time;
QTimer *timer = new QTimer();
while(true)
{
if(port->dataAvailable)
{
timer->start();
}
if(!port->dataAvailable)
{
if(!timer->isActive())
{
currentTime = QTime::currentTime();
if((currentTime - time ) >= 10000)
break;
}
if(timer->isActive())
{
timer->stop();
time = QTime::currentTime();
}

}
}

havent tested it, but should work

^NyAw^
16th February 2009, 14:58
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.

talk2amulya
16th February 2009, 15:15
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

^NyAw^
16th February 2009, 15:39
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.

talk2amulya
16th February 2009, 16:58
makes sense..thanks for clearin that up..i didnt know abt the baud rate and stuff

adamatic
17th February 2009, 07:31
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. :)