0

我在 Win32 上使用 QextSerialPort 和 Qt 4.8.1。当设置“轮询”查询模式,并使用 QextSerialPort::setTimeout() 设置超时。当我调用 QExtSerialPort::read() 时,即使数据可用,读取函数也不会返回,直到整个超时期限到期,即使它返回数据也是如此。

例如:

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

我希望它会在读取指定的数量或字节超时到期后立即返回 - 这首先发生。

这应该工作还是我误解了这个界面?有没有办法在轮询模式下实现预期的行为。

4

3 回答 3

0

我认为,问题出在qiodevice.cpp文件QIODevice::read(char *data, qint64 maxSize)

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

哪里QIODEVICE_BUFFERSIZE16384

readData()函数在QextSerialPort中实现并调用ReadFile() WinApi函数。

于 2014-05-28T16:49:18.253 回答
0

看来解决方案是以无缓冲模式打开,因此:

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

我不确定为什么这应该是必要的,所以如果有人能对这种界面设计的理念有所了解,我仍然会对任何答案感兴趣。

于 2012-10-18T17:08:07.680 回答
0

我很确定 QExtSerial 在 Windows 上使用 ReadFile()。

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

是否返回错误字符串?qextserialport->errorString();

也许 ReadFile() 被挂断了,但直到它已经将数据推送到指针中?

于 2012-10-29T23:05:40.430 回答