gpt4 book ai didi

c++ - 尝试读取时 QextSerialPort 阻塞

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:01:36 25 4
gpt4 key购买 nike

我在 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。

最佳答案

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

    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.

关于c++ - 尝试读取时 QextSerialPort 阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com