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?
如果有人可以提供帮助,将不胜感激.另外,如果有人知道这样做的更好方法,那就更好了.
If anybody could help that would be greatly appreciated. Also if anybody knows a better way of doing this that would be greater still.
我已将 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.
相关文章