PDA

View Full Version : QExtSerialPort with readyRead()



tho97
27th August 2008, 02:01
Hi,

I try to listen to the serial port when there is data with

connect(port, SIGNAL(readyRead()), this, SLOT(receiveMsg()));

but readyRead() doesn't work with QExtSerialPort on linux, So how can I get the signal whenever there is new data on the port?

Thanks

wysota
27th August 2008, 12:53
You can't. You have to start a timer and periodically check bytesAvailable().

tho97
27th August 2008, 19:32
thank wysota

I set up the QTimer on main gui thread at 1000ms, does it affect the gui, slow it down or freeze it?

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(receiveMsg()));
timer->start(1000);

pherthyl
27th August 2008, 20:48
thank wysota

I set up the QTimer on main gui thread at 1000ms, does it affect the gui, slow it down or freeze it?

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(receiveMsg()));
timer->start(1000);

Depends on what you do in that timeout. Just read the bytes and return, don't do any long processing and you should be ok.

wysota
27th August 2008, 21:18
A suggest a smaller timeout (like 100ms) unless you rarely receive anything on the serial port. To be honest it is best to have an adaptive timeout, for instance start with 100ms. If you receive something reduce the timeout twice unless you are already on 100ms. If you don't receive anything, double the timeout.


void sth::tryReceive(){
if(bytesAvailable()>0){
timeout = qMax(100, timeout/2);
readData(...);
} else {
timeout = qMin(1000, timeout*2);
}
QTimer::singleShot(timeout, this, SLOT(tryReceive()));
}

This way you'll be receiving faster if there is anything to receive and will reduce the load on the application when there is not.