串行数据从连续字符串中读取并解析为变量(Arduino)

Serial Data Read and parse to variables from continuous string (Arduino)

我有 2 个 Arduino 和 2 个 xbee。我将 2 个传感器数据从 Arduino 1(路由器)发送到 Arduino 到(协调器): 在协调器上,我完美地从这 2 个传感器(来自路由器)接收无线数据。

数据流是这样的:

20.1324325452924 divided in:
-first sensor(temperature): 20.1324325452
-second sensor(gas):924


我的目标是将这 2 个值作为 2 个不断更新的变量,然后将这些值传递给程序的其余部分,以便在 LCD 上打印或其他内容:

temperature=20.1324325452
gas=924


我设法将我在串行 (20.1324325452924) 上收到的初始字符串分成 2 个变量,但是这 2 个变量的值不像初始字符串那样更新(当传感器值更改时) :

我的代码:

LiquidCrystal lcd(12,11,10,9,8,7);
String temperature;
String gas;
String readString;

char IncomingData[13];


 void setup() {
 Serial.begin(9600);                                     
}

 void loop() {
 while (Serial.available() > 0)                           
 {
       char IncomingData = Serial.read();                   
        readString += IncomingData ;     
        temperature = readString.substring(0, 13); //get the first 13 characters
        gas = readString.substring(13, 16); //get the last 3 characters 

        Serial.print(IncomingData);  //here I have my string: 20.1324325452924  wich is updating properly when I have sensor values changes 

        // Process message when new line character is DatePrimite
        if (IncomingData == '\n')
        {
          Serial.println(temperature);
           lcd.begin(16, 2);
          lcd.setCursor(0,0);                              
          lcd.write("T:");
          lcd.print(temperature);               
         delay(500);
         temperature = "";                               // Clear DatePrimite buffer

         Serial.println(gaz);
         lcd.begin(16, 2);
         lcd.setCursor(0,1);                              
         lcd.write("G:");
         lcd.print(gas);
         delay(500);
         gaz = "";                                       // Clear DatePrimite buffer
      }
    }
}

Output from serial:
20.1324325452924
20.1324325452
924

当我收到新的传感器数据时,它正在更新第一个字符串,但接下来的 2 个字符串每次都保持不变。我被困了好几天我不知道要做这项工作。我需要做的就是将包含来自 2 个传感器的数据的初始字符串分成 2 个不断更新的变量,然后将这些值传递给程序的其余部分,以便在 LCD 上打印。

有谁知道如何进行这项工作吗?

你必须像这样修改程序:(在循环中对 readString 执行操作)

    // Process message when new line character is DatePrimite
    if (IncomingData == '\n')
    {
      Serial.println(temperature);
       lcd.begin(16, 2);
      lcd.setCursor(0,0);                              
      lcd.write("T:");
      lcd.print(temperature);               
     delay(500);
     temperature = "";                               // Clear DatePrimite buffer

     Serial.println(gaz);
     lcd.begin(16, 2);
     lcd.setCursor(0,1);                              
     lcd.write("G:");
     lcd.print(gas);
     delay(500);
     gaz = "";                                       // Clear DatePrimite buffer

     readString = "";   //clear either you concatenate  at each loop!!*******
  } 

收到完整的字符串后拆分数据。

void loop() {

  while(!Serial.available()); // wait till data to be filled in serial buffer

  String incommingStr = Serial.readStringUntil('\n'); // read the complete string

  String temperature = incommingStr.substring(0, 13);
  String gas = incommingStr.substring(13, 16);

  Serial.print(incommingStr);
  Serial.println(temperature);
  Serial.println(gas);

  lcd.setCursor(0,0);                              
  lcd.print(temperature);

  lcd.setCursor(0,1);                              
  lcd.print(gas);

  delay(500);
}

您只需调用 lcd.begin() 一次。从 setup() 函数调用它。