PDA

View Full Version : qextserialport waitForReadyRead and timeOut



adamatic
9th March 2009, 13:11
Hello.

I am implementing a kind of waitForReadyRead datas from the serial port, where if after 10 seconds without bytesAvailable the program continues running.

This is the code:


QTimer *timer2= new QTimer();

timer2->start(10000);
while(port->bytesAvailable()<=0 && timer2->isActive());

if(port->bytesAvailable()<=0){
//There arent bytesAvailable and the timer has expired ( timeOut)

}else{
port->read();
}



The problem is that if it isnt bytesAvailable the program is all the time in an infinite loop, in the while loop, but it should stop the loop after 10 seconds.

Maybe I am using the timer in a wrong way, do you think it is this?

Cheers.

^NyAw^
9th March 2009, 13:35
Hi,



while(port->bytesAvailable()<=0 || timer2->isActive());


You have to exit the bucle when there are bytes available OR the timer is active. When there are no bytes but the timer is stoped you have to exit the bucle.

Note also that when exiting the bucle you have to stop the timer because maybe data has arrived and the timer is active and will be fired at time.

adamatic
9th March 2009, 14:17
I dont understand why do you say the while loop with AND
while(port->bytesAvailable()<=0 && timer2->isActive()); is wrong? because I am waiting until receive datas in the port or the timer has expired? I mean:

If there are bytes availabled, port->bytesAvailabe<=0 is false, so it goes out the loop.
and if the there arent bytes but the timer has expired timer->isActive return false, so it is going out from the loop too.

adamatic
9th March 2009, 14:21
I have another question in relation with this:

I am testing the code, and I am running the code without sending any data to the port. And I dont know why but I am obtaining port->bytesAvailable > 0 and I dont have sent datas. But this is only when I have the conection done.

^NyAw^
9th March 2009, 15:58
Hi,

Sorry, so when the timer is fired you have to stop it. :rolleyes:
Also you can set the timer to be "singleShot"

On the second question, you can use a Serial Port monitor to obtain the data that is recived. Maybe you can then know what are you receiving. Mybe the reciver is sending some data when the connection is stablished.

SteveH
10th March 2009, 00:24
Hi,

Just a sideways query to all following this thread (I am coding a Windows comport program and checking on things to include).

Is the waitForReadyRead signal widely used ? (possibly because the readyRead signal in qextserialport is unreliable ?)

adamatic
10th March 2009, 10:01
Hello.

What I know about waitForReadyRead in serial port is that it is not working with windows, so for this reason i used the while loop.

You can check this thread
http://www.qtcentre.org/forum/f-qt-programming-2/t-serial-communication-waitforreadyread-doesnt-wait-18560.html

Cheers.

adamatic
10th March 2009, 10:05
I have another question about qextserialport. When I want to write only a character in the port can I use the write function like this?:

char *a;
a[0]='b';
port->write(a);

Or do i have to write a const char* and the size as parameters?

And another thing, do i have to reset the port before use it? Because maybe there are bytes still written in the port.

adamatic
10th March 2009, 10:41
And about to use a singleshot if the program is running in a while loop, it is never to go to the slot in the singleshot or i should to have another thread..

or is there another way to go to the slot althoug it is running the while loop?

^NyAw^
10th March 2009, 10:52
Hi,

I recommend you to write your code into a QThread so the while loop will not hang the main thread. In the constructor of the QThread you can create the Timer(or define it as a member variable) and start it on the "run" method.

Maybe another way without using a timer is to use the thread to calculate the time at start and at every loop iteration and if 10 seconds elapsed you can exit.

These are just someways that I think you can use to get it works as you expected.

On the other question about how to write on the serial port. It's easier to use a QByteArray and write it directly. The serial port will take care of the size of the data to be sended.

The SIGNAL "waitForReadyRead" works only on Windows using Alpha version of QextSerialPort and I don't know if it really works as expected.