QextSerialPort在尝试读取时阻塞

时间:2011-03-22 18:03:55

标签: c++ qt serial-port qextserialport

我在Windows上使用QextSerialPort使我的Qt / C ++应用程序能够从串口读取和写入。

我想只在有字节要读取时从串口读取字节。

首先,我尝试将QextSerialPort::readyRead()信号连接到主类中的插槽,但我注意到应用程序挂起。

然后我尝试使用QextSerialPort::read(char *, uint64),它返回读取的字节数,然后我又进行了另一次不成功的尝试,结合QextSerialPort::bytesAvailable()QextSerialPort::read(char *, uint64)来查看是否有助于我的应用程序不要块。

然而,应用程序总是阻塞并且必须被杀死,因为它不响应任何鼠标或键盘事件。我压缩应用程序冻结,因为读取例程阻塞。

有没有办法用QextSerialPort执行非阻塞读取?

如果没有,我应该使用哪个库从Windows上的串口获得非阻塞读取功能?

更新 我尝试使用QextSerialPort::atEnd()检查是否有要读取的字节而不是使用QextSerialPort::bytesAvailable()QextSerialPort::size(),并且它始终返回false。

1 个答案:

答案 0 :(得分:0)

读取应该是非阻塞的,除非您已将查询模式设置为轮询

    inline QueryMode queryMode() const { return _queryMode; }

    /*!
     * Set desired serial communication handling style. You may choose from polling
     * or event driven approach. This function does nothing when port is open; to
     * apply changes port must be reopened.
     *
     * In event driven approach read() and write() functions are acting
     * asynchronously. They return immediately and the operation is performed in
     * the background, so they doesn't freeze the calling thread.
     * To determine when operation is finished, QextSerialPort runs separate thread
     * and monitors serial port events. Whenever the event occurs, adequate signal
     * is emitted.
     *
     * When polling is set, read() and write() are acting synchronously. Signals are
     * not working in this mode and some functions may not be available. The advantage
     * of polling is that it generates less overhead due to lack of signals emissions
     * and it doesn't start separate thread to monitor events.
     *
     * Generally event driven approach is more capable and friendly, although some
     * applications may need as low overhead as possible and then polling comes.