PDA

View Full Version : serial communication



semden
13th February 2009, 07:33
Hi everybody. Iam working with serial communication , using qextserialport library on QT 4.4.3 intergrated with visual studio 2005.
I have been able to successfully configure the library and try out an example QESPTA , which is able to recieve data from an embedded system to the computer running the QESPTA example.
Iam able to recieve data when i click on the open and recieve buttons, my problem is that i want to continously recieve data as long as it is available on the port , not just by clicking the recieve buttom.

I need some guidance.

Cruz
13th February 2009, 09:31
You need to write either a thread that runs in the background and collects the data, or use a QTimer to periodically poll the serial port. The threaded version looks something like this:



setupTheSerialPortJustLikeInQESPTA();

while (true)
{
receiveData();
msleep(100);
}

void receiveData()
{
// Check if there is any data to read.
if (!port->bytesAvailable())
return -1;

// Read as much data as you can from the port.
bytesRead = port->read(receiveBuffer, qMin(port->bytesAvailable(), BUFFER_SIZE));
}


With the QTimer solution you don't have an infinite loop with a sleep, but you call receiveData() with a timer instead.

A blocking approach that tries to read from the port and waits until data arrives would be preferable, but I couldn't make that work because of a problem with waitForReadyRead() (http://www.qtcentre.org/forum/f-qt-programming-2/t-serial-communication-waitforreadyread-doesnt-wait-18560.html).

semden
13th February 2009, 09:57
Thanks , i will try it now and see how it works