首页 > 解决方案 > Arduino SIM900 GSM 如何在字符串上加入字符

问题描述

我目前正在使用 GSM900 GSM GPRS 制作一个 Arduino 项目。在这个项目中,我必须接收从手机发送的数据。我可以很容易地用单个字符接收数据,但是我不能加入 do 字符来获得一个完整的单词(字符串)。如果这个词等于另一个词(字符串),我必须在 If 语句中使用这个完整的词,做一些事情......

#include <SoftwareSerial.h>

// Configure software serial port
SoftwareSerial SIM900(7, 8);
//Variable to save incoming SMS characters
char incoming_char=0;
String newchar = "";

void setup() {
  // Arduino communicates with SIM900 GSM shield at a baud rate of 19200
  SIM900.begin(19200);
  Serial.begin(19200); 

  // Give time to your GSM shield log on to network
  delay(20000);

  // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}

void loop() {
  if(SIM900.available() >0) {
    incoming_char=SIM900.read(); 
    Serial.print(incoming_char); 
  }
}

我尝试将此命令放在循环内的 if 语句上,但是在尝试比较单词之后,它就不起作用了。

void loop() {
  if(SIM900.available() >0) {
    incoming_char=SIM900.read();
    newString = incoming_char + "";
    Serial.print(incoming_char); 
  }
  if (newString == "Test"){
       Serial.println("It worked");
    }
}

我从 Monitor Serial 得到的输出是这样的:+CMT: "+myNumber","","19/09/20,16:31:05-12" Test

标签: arduinoarduino-unogsmgprs

解决方案


void loop() {
    if (SIM900.available() >0) {
        incoming_char=SIM900.read();
        newString += incoming_char;
        Serial.print(incoming_char); 
    }

    if (newString.endsWith("Test")) {
        Serial.println("It worked");
    }
}

推荐阅读