The data I'm reading from a serialport (in Qt, using QtSerialPort/QSerialPort) is separated by the newline '
' and return '
' characters, which is the way I intend to look at it for parsing. The line length may very, but it is very easy to extract the data from the format of each line.
//signal/slot connection on readyRead() is as follows:
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
where readData() is defined as:
void MainWindow::readData()
{
//As mentioned below, which I will reiterate, I have already tried the addition of
// canReadLine():
if (serial->canReadLine()){
QByteArray data = serial->readLine();
//QByteArray allData = serial->readAll();
parseSerialBytes(data);
//console->putData(data);
//console->putData(alldata);
}
}
However, the QIODevice::readLine()
function is extremely slow, and clearly blocking data from being received at full frequency compared to QIODevice::readAll()
Can someone please explain how to properly use the readLine()
function so I don't have to loop through readAll()
into the QByteArray
to parse each line? I used the "terminal" Qt Widgets example to create this asynchronous serialport read functionality.
Thanks in advance - this seems to be a common problem I have not yet seen answered here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…