arduino - 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
解决方案
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");
}
}
推荐阅读
- r - 比较组间的年费率
- python - numpy.core._exceptions.MemoryError:无法为形状为 (138494, 10000) 且数据类型为 float64 的数组分配 10.3 GiB
- r - 使用 htmltab 从 html 获取表格
- python - 在 python 中输入提示因输入 import Any 而感到困惑
- node.js - 有什么方法可以更快地插入?(Mongo DB 集合更新)
- ios - 集合视图单元格在某些视图中显示错误的背景颜色
- typescript - VueJs 3 TDD 一个依赖于外部库组件的组件
- postman - Keycloak 使用授权类型密码获取令牌总是返回 401 错误
- html - 页面重新加载时将 html 输入默认值放回去
- gulp - babel 输出文件名选项