我正在使用QtSerialPort / QSerialPort从串口读取数据,该数据以换行符'\n'和回车符'\r'分隔,这是我用于解析的方式。每行的长度可能会有所不同,但从每行的格式中提取数据非常容易。
//signal/slot connection on readyRead() is as follows:
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
其中readData()的定义如下:
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);
}
}
然而,函数
QIODevice::readLine()
的速度非常慢,相对于QIODevice::readAll()
来说明显阻止了数据以满频率接收。有人可以解释一下如何正确使用
readLine()
函数,这样我就不必通过循环readAll()
到QByteArray
来解析每一行。我使用了“终端”Qt小部件示例来创建这个异步串口读取功能。谢谢事先提供帮助 - 这似乎是一个普遍的问题,我还没有在这里找到答案。
if (serial->canReadLine())
包含了我的serial->readLine()
语句,但数据读取频率似乎没有明显改变。我还注意到这几乎与一个 VB 在 Visual Studio 用户提出的问题完全相同 像这个,以及其他使用其他 IDE 和语言的人也提出了这个问题,但都没有解决。非常奇怪。 - Rachael