Search code examples
c++asynchronousserial-portqt5qtserialport

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


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.


Solution

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