0

我正在编写一个 Arduino 代码来接收来自 SIM800 模块的 SMS,然后将相同的消息发送回发送者。我的代码如下:

#include<SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);//Connect to pin 9&10 on GSM module

const byte numChars = 128; //character size
char receivedChars[numChars]; // Store data read from SMS
boolean newData = false; //flag to check if new data has arrived

void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);
    Serial.println("<Arduino is ready>");
    mySerial.println("AT");
    mySerial.println("AT+CMGF=1"); //text mode
    mySerial.println("AT+CNMI=1,2,0,0,0");
    mySerial.println("AT+CSQ"); //Check signal strength
    while (Serial.available()) 
    {
        mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    }
    while(mySerial.available()) 
    {
        Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
    }
}

void loop() {
    //code for checking sms
    scanSMS();
    //Print if a new data arrived
    showNewData();
    delay(25000);
}

void scanSMS() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '#'; //Start and end markers. Eg. SMS body will be sent as #status# 
    char endMarker = '#';
    char rc;
    while (Serial.available()) 
    {
        mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    }

    while (mySerial.available() > 0 && newData == false) {
        Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port

        rc = mySerial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

/*PRINT IF THERE IS A NEW DATA*/
void showNewData() {
    if (newData == true) {
        SendMessage(receivedChars);
        newData = false;
    }
}

/*SEND BACK WHATEVER SMS IS RECEIVED*/
void SendMessage(String receivedChars)
{
    Serial.print("here");
    mySerial.println("AT+CMGS=\"+xxxxxxxxxx\"\r"); // Replace x with mobile number
    mySerial.println("Humidity Alert!");// The SMS text you want to send
    mySerial.print(receivedChars);
    mySerial.println((char)26);// ASCII code of CTRL+Z
}  

问题是该模块似乎只接收替代字符而不是整个消息。例如,如果我发送文本#program#,Arduino 将只打印 porm 或 rga。我在这里错过了什么吗?非常感谢任何帮助。

4

1 回答 1

0

mySerial.read()在 while 循环中调用了两次。

while (mySerial.available() > 0 && newData == false) {
      Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
      rc = mySerial.read();

我认为这就是为什么您没有看到整个字符串的原因。read读取缓冲区中的第一个字节并前进。

您想存储返回值并将其传递给这样的write调用。

while (mySerial.available() > 0 && newData == false) {
    rc = mySerial.read();
    Serial.write(rc);//Forward what Software Serial received to Serial Port
    ....
}
于 2019-09-25T18:25:09.950 回答