How to know when QSerialPort is ready for more data in Qt5?

。_饼干妹妹 提交于 2020-01-06 23:51:00

问题


Is there some way to be notified when my serial device is ready to receive more data?

I am implementing a program that feeds commands to a servo controller over serial using the asynchronous API of QSerialPort. The program works, but it seems that I am experiencing congestion.

In my application I keep a registry of most updated servo position that I wish to synchronize with the controller with as low latency as possible. My attempt at pushing new data to the serial port as quickly as time lets me leads to what I think is a buildup of data that makes the communication break down altogether.

My question is, can I be notified when the serial interface is ready to receive new data somehow while using QSerialPort in asynchronous mode?

The only other option I see is to use synchronous mode and call waitForBytesWritten() But I really don't want to do that.

NOTE: I have studied the serial port examples in the Qt5 documentation carefully, but the example for asynchronous write does not provide the information I need, as it perform a one-shot send before quitting.


回答1:


You could listen to the signal bytesWritten and check bytesToWrite to determine if the write buffer is empty. Both are inherited from QIODevice.



来源:https://stackoverflow.com/questions/36839369/how-to-know-when-qserialport-is-ready-for-more-data-in-qt5

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