QextSerialPort::read() does not return until timeout expired but data is available

1k Views Asked by At

I am using QextSerialPort with Qt 4.8.1 on Win32. When "polling" query mode is set, and a timeout set using QextSerialPort::setTimeout(). When I call QExtSerialPort::read(), even when data is available, the read function does not return until the entire timeout period has expired, even though it returns with data.

For example:

m_port->setTimeout( 3000 ) ;
char data = 0 ;
int count = m_port->read( &data, 1 ) ;
// Returns after three seconds, but count is 1, and data set as expected

I would expect it to return as soon a the specified number or bytes are read or the timeout expires - which ever occurs first.

Should this work or am I misunderstanding this interface? Is there a way of achieving the expected behaviour in polling mode.

3

There are 3 best solutions below

0
On

It appears the solution is to open in unbuffered mode thus:

m_port->open( QIODevice::ReadWrite | QIODevice::Unbuffered ) ;

I am not really sure why that should be necessary, so if anyone can shed some light on the philosophy of this interface design I'd still be interested in any answers.

2
On

I think, problem is in QIODevice::read(char *data, qint64 maxSize) function from qiodevice.cpp file:

qint64 QIODevice::read(char *data, qint64 maxSize)
{
    ...
            if ((d->openMode & Unbuffered) == 0 && maxSize < QIODEVICE_BUFFERSIZE) {
                ...
                int bytesToBuffer = QIODEVICE_BUFFERSIZE;
                ...
                qint64 readFromDevice = readData(writePointer, bytesToBuffer);
                ...
            }
    ...
}

where QIODEVICE_BUFFERSIZE is 16384.

readData() function is implemented in QextSerialPort and calls ReadFile() WinApi function.

0
On

I'm pretty sure QExtSerial uses ReadFile() on Windows.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx

Is there an error string returned? qextserialport->errorString();

Maybe ReadFile() is getting hung up but not until it has already pushed data into the pointer?