【问题标题】:Qt reading serial input then splitting it into separate variablesQt 读取串行输入,然后将其拆分为单独的变量
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 您好,我之前确实尝试过,但是即使它们发生变化,这些值也会保持为零。 num1 = parsed_data.mid(0,1).toInt(); 似乎有效,但我认为这不是解决它的正确方法。
  • 嘿,我有点困惑。如果您的问题是从串行端口读取而不是拆分值,您能否读取 4、0、2、5、7、1 之类的原始数据。尝试发送以 \n 换行符结尾的值并在收到整行时读取它,您可以使用 canReadLine() 函数进行检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2021-12-22
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
相关资源
最近更新 更多