Qt读取串行输入,然后将其拆分为单独的变量

Qt reading serial input then splitting it into separate variables

本文关键字:拆分 单独 变量 然后 读取 输入 Qt      更新时间:2023-10-16

我正在研究Qt,但是我遇到了一个我一生都无法解决的问题。我已经尝试了许多不同的代码组合,但它仍然没有给我我正在寻找的输出。我希望有人能帮助我。

QStringList buffer_split = serialBuffer.split(","); //  split the serialBuffer string, parsing with ',' as the separator
//  Check to see if there less than 3 tokens in buffer_split.
//  If there are at least 3 then this means there were 2 commas,
//  means there is a parsed temperature value as the second token (between 2 commas)
if(buffer_split.length() < 3){
// no parsed value yet so continue accumulating bytes from serial in the buffer.
serialData = arduino->readAll();
serialBuffer = serialBuffer + QString::fromStdString(serialData.toStdString());
serialData.clear();
}else{
// the second element of buffer_split is parsed correctly, update the temperature value on temp_lcdNumber
serialBuffer = "";
qDebug() << buffer_split << "n";
parsed_data = buffer_split[1];
}

上述解决方案对我有用,反过来我正在读取通过串行端口发送的值,例如:

0,0,0,0,0,0

以上是parsed_data如何从串行端口读取信息,这是正确的。

我遇到的问题是拆分它,然后将它们存储在单独的变量中以启动一些 if 语句。 到目前为止,我似乎无法让它工作。

如果有人可以帮助我,我将不胜感激

谢谢

你不需要额外的parsed_data变量buffer_split存储它们,你只需要int num1 = buffer_split[0].toInt(); int num2 = buffer_split[1].toInt();

...