qt读取就绪信号 [英] qt read ready signal

查看:397
本文介绍了qt读取就绪信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与在1996处理器上运行的设备建立串行连接。这意味着可能需要几秒钟才能将数据传回给我。我知道 readyRead 信号是在每次有新数据时生成的,但我的问题是生成多长时间。也是他们的一种方式,我可以测试准备阅读低,因为如果它停止发射,当他们不再有任何数据留在读入,这将是非常有帮助的。

I'm trying to set up a serial connection with a device which runs on a 1996 processor. This means that it can take several second for the data to be transmitted back to me. I know that the readyRead signal is generated every time new data is available, but my question is how long is generated for. Also is their a way that I can test for the ready read being low because if it stops being emitted when their is no longer any data left to read in, that would be really helpful.

 do{
      ui->label_5->setText("readyRead");    
 } while (readyRead() == true);

 QString output = serial->readAll();
 ui->label->setText(output);
 }

我希望这段代码段说明我想做什么。我无法测试它,因为我还没有能够解决如何调用 readyRead 。我认为我的 readAll 命令可能在错误的地方,但我不知道。

I hope this code segment illustrates what i'm trying to do. I haven't been able to test it as I haven't been able to work out how to call readyRead. I think that my readAll command might be in the wrong place but i'm not sure?

帮助,将非常感谢。

我有一个更好的方法,添加 connect(serial,SIGNAL(readyRead()),this,SLOT(myReceivedData())); 到我的代码,但我还是有问题。我认为他们的发生,因为处理器是如此之慢,它需要几秒钟来传输数据,这是因为我的程序退出void循环只返回,因为readyRead标志仍然是真的,因为他们仍然是数据留给读。我尝试添加一个睡眠命令,但这暂停了gui和接缝影响数据的读取时间。

I've added connect(serial, SIGNAL(readyRead()),this,SLOT(myReceivedData())); to my code but i've still got issues. I think their occurring because the processor is so slow that it takes several seconds to transmit the data, and that because of this my program is exiting the void loop only to return again because the readyRead flag is still true because their is still data left to read. I have tried adding a "sleep" command but this pauses the gui and seams to affect the reading time of the data.

我的主要问题是我正确的 readyRead 信号在数据转换的中途仍然是正确的或者我缺少一个关键事实?

My main question is am I correct about the readyRead signal still being true mid way through the data transition or am I missing a key fact?

感谢回复。

推荐答案

这很简单:当你对 readyRead ,您必须读取所有可用的资料。您将不会再收到通知。还要记住,当有任何非零量的数据可以读取时,信号被发射 - 所以你应该期望一个或多个字节,但不要指望任何特定的数字。例如,如果您的通信伙伴发送一定大小的数据包,您不应该期望接收完整的数据包,除非协议保证(UDP是唯一的一个)。

It's really simple: When you act on the readyRead signal, you must read all data available. You won't be notified again. Also keep in mind that the signal is emitted when there is any non-zero amount of data available to read - so you should expect one or more bytes, but don't expect any particular number. For example, if your communications partner sends packets of a certain size, you should not expect to receive full packets unless the protocol guarantees that (UDP is about the only one).

您需要设置代码,以便读取整个数据,您就可以了。

You need to set up your code so that entire data is read, and you'll be OK.

这篇关于qt读取就绪信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆