簡體   English   中英

比較從Arduino中的串行數據接收到的簡明字符串

[英]Comparing concated string recieved from serial data in Arduino

我正在嘗試將串行數據讀入一個字符串,該字符串能夠與另一個字符串進行比較。 我正在使用if(inputString.equals(“ test”))測試布爾值,但是它總是返回false,因為在串行監視器中鍵入test時從不顯示THEY ARE EQUAL(它回顯我發送的內容到arduino)。 有任何想法嗎? 以這種方式壓縮字符串是否會在字符串中增加額外的未顯示字節?

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
// initialize serial:
Serial.begin(9600);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {

       if (inputString.equals("test")) {
       Serial.print("THEY ARE EQUAL"); 
    }

    Serial.print(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
    }

   }

void serialEvent() {
  while (Serial.available()) {
  // get the new byte:
  char inChar = (char)Serial.read(); 
  // add it to the inputString:
  inputString += inChar;
  stringComplete = true;

  }
}

謝謝!

您需要留出足夠的時間來讀取串行數據。 添加延遲

char inChar = (char)Serial.read(); 
// add it to the inputString:
delay(100);
inputString += inChar;

暫無
暫無

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

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