readyRead() signal of QextSerialPort (QIODevice) is not being called fast enough

删除回忆录丶 提交于 2020-01-24 16:17:06

问题


I am using qextserialport on a Raspberry Pi to communicate with an PanStamp (Arduino compatible device).

This PanStamp connected to the Pi executes two functions:

  • Send some sensor's readings each second (about 12 bytes);
  • Send all data it receives through a wireless link (about 60 bytes about 6 times per second).

My architecture is:

  • Hub: PanStamp + Raspberry Pi;
  • Satellite: PanStamp + a few sensors.

There are two situations:

  • Satellite on sending data wirelessy to the Hub. I this situation the Pi receives lots of data through it's serial port every second;
  • Satellite off, the Pi receives about 12 bytes each second through serial port.

When the satellite is off the readyRead() signal is not generated every time a byte arrives and it drives my program to a "out of sync" condition where to each data packet read one or more stays in the buffer (that keeps growing).

However when I turn the satellite on and the Pi starts to receive lots of data this "out of sync" condition disappear, there are a burst of data (the buffer grows faster and after is empted) and the my program starts to work "in real time".

Here is a example of my program's output: www.tiago.eti.br/storage/iSEDE.log

As you can see in the log the bytes available keeps growing and the data send every second (line starting with HUB: is not being processed every second. there is a time stamp at the beginning). After a while there is a burst (satellite has been turned on) and there is lots of data being processed every second, the satellite's data start to be processed (lines starting with an 8), the buffer is emptied and my program starts processing data in "real time".

So what can I do to avoid the buffer from growing too much and do not lose data? I tried to call the function that is connected to readyRead() when the buffer gets bigger than 100 bytes but it created a mess and I started to lose some packets.


回答1:


Your problem is in most common mistake people do with QIODevice.. you wrongly assume that readyRead is called on every byte, saying more it would be completely wrong if it works like that. Idea is that every time you receive a readyRead there is SOMETHING to read from the device.. it can be 1 byte, 10 bytes, 1k.. etc.. In simple words its done like that to minimise CPU loading in case of block transfers as well as on a hardware to read data in blocks rather then in bytes.

So what you should do is to call readAll() to get all available data which arrived and process them in a way you like.

you might want to have a look in here..



来源:https://stackoverflow.com/questions/18082775/readyread-signal-of-qextserialport-qiodevice-is-not-being-called-fast-enough

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!