You need to write either a thread that runs in the background and collects the data, or use a QTimer to periodically poll the serial port. The threaded version looks something like this:
setupTheSerialPortJustLikeInQESPTA();
while (true)
{
receiveData();
msleep(100);
}
void receiveData()
{
// Check if there is any data to read.
if (!port->bytesAvailable())
return -1;
// Read as much data as you can from the port.
bytesRead = port->read(receiveBuffer, qMin(port->bytesAvailable(), BUFFER_SIZE));
}
setupTheSerialPortJustLikeInQESPTA();
while (true)
{
receiveData();
msleep(100);
}
void receiveData()
{
// Check if there is any data to read.
if (!port->bytesAvailable())
return -1;
// Read as much data as you can from the port.
bytesRead = port->read(receiveBuffer, qMin(port->bytesAvailable(), BUFFER_SIZE));
}
To copy to clipboard, switch view to plain text mode
With the QTimer solution you don't have an infinite loop with a sleep, but you call receiveData() with a timer instead.
A blocking approach that tries to read from the port and waits until data arrives would be preferable, but I couldn't make that work because of a problem with waitForReadyRead().
Bookmarks