您的位置:首页 > 产品设计 > UI/UE

Qt5.5中使用serialport类使得Ui界面很卡

2015-11-24 19:24 459 查看
实际的读取串口工作中出现了这种情况,经过分析问题,可能有以下几个原因。

第一 ,在timer(out)信号触发的readData事件内,出现了非常复杂的循环,程序逻辑问题带来的时间复杂度的增加。

第二,readdata的数据量过大,也就是缓冲去buffer的长度太大了,每个循环中读入的实际数据量远小于maxsize,因此程序就会在forever循环中不停的循环读入数据装入buffersize直到其装满为止。

第三,最容易引起Ui界面卡的原因,就是设置的串口Comm的waitforreadyread(timeout)过大,以至于read动作一直在timeout时间内等待数据的到来,程序处理在这里暂停了timeout时间,这个timeout时间如果超过0.5s,Ui界面自然就卡的不行。

当然,使用waitforreadyread使得Ui变得非常卡的原因就是这个函数阻塞了当前线程。

以下是Qt QIODevice类中有关于这个函数的文档:

bool QIODevice::waitForReadyRead(int msecs)

Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out.

Returns true if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).

This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.

If called from within a slot connected to the readyRead() signal, readyRead() will not be reemitted.

Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.

Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: