簡體   English   中英

將串行輸入讀入字符數組

[英]Read Serial Input into Char Array

所以我設法將一個 ESP-01 模塊連接到我的 arduino,現在我試圖解析芯片通過串行連接給出的 +IPD 響應。 我對 C++ 並不是那么得心應手,但這是我在網上研究了一堆之后設法想出的:

#include <SoftwareSerial.h>
SoftwareSerial ESP8266(2, 3); // RX | TX
int baud = 9600;


void setup() {
  ESP8266.begin(baud);
  Serial.begin(baud);
  Serial.println("--- Start ---");
  
}

void loop() {

  if (ESP8266.available()) // check if the esp is sending a message
  {
    Serial.println("Something received");
    delay(500);
    if (ESP8266.find("%<"))
    {
      Serial.println("--------------- DEBUG ----------------A");
      char temp = {char(ESP8266.read())};
      while ((temp != '%') && (ESP8266.available())) {
        Serial.print(temp);
        temp = char(ESP8266.read());
      }
      Serial.println("\n--------------- END DEBUG ----------------");
    }
  }
}

芯片收到消息時給出的標准響應如下:

+IPD,<len>:<Message>
+IPD,0,14:%<255,128,0%

我試圖發送的數據 - 隨機 RGB 值(使用 '%<' 和 '%' 作為標志/標記):

%<255,128,0%

從這里開始,我設法編寫了上面的代碼,它將通過串行打印出以下內容:

在此處輸入圖片說明

所以我設法讓它通過串行打印我需要的值,但我似乎無法將它們存儲在某種數組中來處理數據。

我嘗試過的事情:

  • 使用 readString() 而不是 read(),加上 indexOf 來搜索/提取數據
  • 循環並附加到字符數組
  • 一堆數組的東西,雖然看起來很棘手,因為你必須在聲明它時知道長度

理想情況下,我想要一個函數來讀取 +IPD 值,提取 RGB 數據,然后將其拆分為 3 個索引數組,如下所示:

rgbArray = {124, 234, 96};

任何和所有的幫助都非常感謝!

要將輸入存儲到數組,只需分配一個數組並將數據存儲在那里。

      Serial.println("--------------- DEBUG ----------------A");
      int receivedLength = 0;
      char data[16];
      char temp = ESP8266.available();
      while ((temp != '%') && (ESP8266.available())) {
        Serial.print(temp);
        if (receivedLength < 16) data[receivedLength++] = temp;
      }
      for (int i = 0; i < receivedLength; i++) Serial.print(data[i]);
      Serial.println("\n--------------- END DEBUG ----------------");

或者,您可以在閱讀時轉換為整數:

      Serial.println("--------------- DEBUG ----------------A");
      int rgbSize = 0;
      int rgbArray[3];
      int currentValue = 0;
      char temp = ESP8266.available();
      while ((temp != '%') && (ESP8266.available())) {
        Serial.print(temp);
        if (temp == ',') {
          if (rgbSize < 3) rgbArray[rgbSize++] = currentValue;
          currentValue = 0;
        } else {
          currentValue = currentValue * 10 + (temp - '0');
        }
      }
      if (rgbSize < 3) rgbArray[rgbSize++] = currentValue;
      for (int i = 0; i < rgbSize; i++) {
        if (i > 0) Serial.print(',');
        Serial.print(rgbArray[i]);
      }
      Serial.println("\n--------------- END DEBUG ----------------");

最終以不同的方式做事:

設法閱讀了 readStringUntil(''); 在網絡的某個黑暗角落。 所以我想出了一個超級臟的實現 - 但它有效:

假設您的輸入字符串是:

+IPD,0,14:%<255,128,0%

然后做:

if (ESP8266.available()) // check if the esp is sending a message
  {

    delay(500);
    if (ESP8266.find("%<"))
    {
      r = ESP8266.readStringUntil(',');
      g = ESP8266.readStringUntil(',');
      b = ESP8266.readStringUntil('%');
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM