【问题标题】:Arduino accepting only alternate charactersArduino 只接受备用字符
【发布时间】:2019-09-25 18:03:54
【问题描述】:

我正在编写一个 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。我在这里错过了什么吗?非常感谢任何帮助。

【问题讨论】:

    标签: arduino arduino-ide sim800


    【解决方案1】:

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

    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
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 2016-01-02
      • 1970-01-01
      • 2016-03-27
      • 2020-12-02
      • 2016-07-26
      • 2012-01-09
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多