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

时间:2017-08-04 15:21:47

标签: c++ qt split serial-port

我正在研究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语句。到目前为止,我似乎无法让它发挥作用。

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

谢谢

1 个答案:

答案 0 :(得分:0)

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

相关问题