【发布时间】:2017-08-04 15:21:47
【问题描述】:
我正在研究 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[1] 编写的那样,您可以访问每个单独的值并存储它。你被困在哪里了?
-
当我尝试存储例如 buffer_split[2] 时,它似乎不起作用。寻找这样的东西。我希望这使它更容易理解。例如,我发送的数组是 45,0,67,0,0,13,我需要将每个数字存储在一个变量中。 int num1 = parsed_data[0] 为 45,int num2 = parsed_data[1] 为 0,int num3 = parsed_data[2] 为 67
标签: c++ qt split serial-port