我正在尝试将串行数据读入一个能够与另一个字符串进行比较的字符串。我正在使用 if (inputString.equals("test")) 来测试布尔值,但它总是返回为假,因为当在串行监视器中键入测试时,它们永远不会显示(它会回显我发送的内容)到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;
}
}
谢谢!