2

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

只有当有字节要读取时,我才想从串口读取字节。

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

然后我尝试使用QextSerialPort::read(char *, uint64),它返回读取的字节数,然后我再次尝试组合QextSerialPort::bytesAvailable()QextSerialPort::read(char *, uint64)查看这是否有助于我的应用程序不阻塞。

但是,应用程序总是阻塞并且必须被终止,因为它不响应任何鼠标或键盘事件。我认为应用程序冻结是因为读取例程阻塞。

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

如果没有,我应该使用什么库来从 Windows 上的串行端口获得非阻塞读取功能?

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

4

1 回答 1

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.
于 2011-03-22T18:29:55.867 回答