0

您好,我想要 Arduino 上的实时 GPS Sim800 模块 RTC 时间,而不使用库,我可以使用库来实现它,但没有库,正确的数据只需 1 分钟,其他的就会出现垃圾。

请告诉我可以帮助我实现它的代码

使用代码我做了以下事情

#include <SoftwareSerial.h>
SoftwareSerial gsm(10, 11);
String Buffer;
String inData;

int i;
int *year, *month, *day, *hour, *minute, *second;

void setup() {
  Serial.begin(9600);
  gsm.begin(9600);
  delay(5000);
}

void loop() {
Ansh:
  delay(500);
  gsm.println("AT+CCLK?\r\n");
  while (gsm.available()) {
    char recieved = gsm.read();
    inData += recieved;
    delay(100);

    // Process message when new line character is recieved
    if (recieved == '\n')
    {
      Serial.print("Arduino Received: ");
      Serial.println(inData);

      if (inData.substring(1, 5) == "+CCLK") {
        Serial.println("Hello");
      }

      // You can put some if and else here to process the message juste like that:

      if (inData == "+++\n") { // DON'T forget to add "\n" at the end of the string.
        Serial.println("OK. Press h for help.");
      }


      inData = ""; // Clear recieved buffer
      goto Ansh;
    }

    /* Buffer = Buffer.substring(Buffer.indexOf("\"") + 1, Buffer.lastIndexOf("\"") - 1);
      year = Buffer.substring(0, 2).toInt();
      month = Buffer.substring(3, 5).toInt();
      day = Buffer.substring(6, 8).toInt();
      hour = Buffer.substring(9, 11).toInt();
      minute = Buffer.substring(15, 17).toInt();
      second = Buffer.substring(18, 20).toInt();
    */
  }/*
  Serial.println(*hour);
  delay(100);
  Serial.println(*minute);
  delay(100);
  Serial.println(*second);
  delay(100);
*/
}

但我没有得到实时输出,但在 1 次正确输出后它返回垃圾如下

12:22:38.960 -> Arduino 接收:+CCLK:“20/10/04,12:22:31+22”12:22:38.960 -> 12:22:39.672 -> Arduino 接收:12:22:39.672 -> 12:22:40.596 -> Arduino 收到:OK 12:22:40.596 -> 12:22:42.339 -> Arduino 收到:AT+CCLK?

12:22:42.339 -> 12:22:44.362 -> Arduino 收到:+CCAT+CCLK?

12:22:44.362 -> 12:22:46.067 -> Arduino 收到:AT+CCLK?

12:22:46.067 -> 12:22:50.344 -> Arduino 收到:+CCLK:“20/10/04,12ATAT+CAT+CCLK?

12:22:50.344 -> 12:22:52.043 -> Arduino 收到:AT+CCLK?

12:22:52.043 -> 12:22:54.082 -> Arduino 收到:+CCAT+CCLK?

12:22:54.082 -> 12:22:55.822 -> Arduino 收到:AT+CCLK?

12:22:55.822 -> 12:23:00.064 -> Arduino 收到:+CCLK:"20/10/04,12:22:49AT+CCLK?

12:23:00.098 -> 12:23:01.791 -> Arduino 收到:AT+CCLK?

12:23:01.791 -> 12:23:03.810 -> Arduino 收到:+CCAT+CCLK?

12:23:03.845 -> 12:23:05.552 -> Arduino 收到:AT+CCLK?

12:23:05.552 -> 12:23:09.803 -> Arduino 收到:+CCLK:"20/10/04,12:22:58AT+CCLK?

12:23:09.838 -> 12:23:11.544 -> Arduino 收到:AT+CCLK?

12:23:11.544 -> 12:23:13.563 -> Arduino 收到:+CCAT+CCLK?

12:23:13.563 -> 12:23:15.290 -> Arduino 收到:AT+CCLK?

12:23:15.290 -> 12:23:19.557 -> Arduino 收到:+CCLK:"20/10/04,12:23:08AT+CCLK?

12:23:19.557 -> 12:23:21.278 -> Arduino 收到:AT+CCLK?

如果我从代码中删除延迟,那么我会收到以下输出

12:34:56.406 -> Arduino 收到:(⸮⸮SCC⸮⸮CL?⸮</p>

1-⸮</p>

AT+CCLK?

AT+CCL~Q⸮ -j 12:34:56.473 -> 12:34:56.541 -> Arduino 收到:⸮5⸮⸮AT+CCLK?

⸮⸮⸮~⸮⸮ 1⸮j⸮b-⸮</p>

AT+CCLK?

AT+C⸮~⸮⸮ 1-j⸮5⸮⸮(⸮i 1-⸮</p>

AV⸮1-j 12:34:56.607 -> 12:34:56.913 -> Arduino 收到:⸮5⸮⸮J⸮jj⸮j AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CK5Q⸮1⸮j⸮5⸮⸮AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK⸮Q⸮ Zj⸮5⸮⸮(⸮⸮1-⸮</p>

⸮</p>

1⸮j⸮5j⸮ը(⸮CCP⸮i 1-⸮5Q⸮⸮j⸮jj⸮ը(j⸮jQb⸮⸮E⸮jj⸮(⸮⸮1-⸮5Q⸮ Zj⸮5⸮⸮(⸮⸮( j⸮jQ⸮Zj⸮5j⸮ը(⸮j⸮⸮ 1⸮j⸮⸮5j⸮(⸮⸮(⸮j⸮V 1-j 12:34:57.184 -> 12:34:57.252 -> Arduino 收到:⸮ 5⸮⸮D⸮j1-⸮</p>

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

1-⸮</p>

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCL~Q⸮ -j 12:34:57.354 -> 12:34:57.561 -> Arduino 收到:⸮5⸮⸮AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?j AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+C⸮⸮5Q⸮⸮⸮Ԋi⸮j⸮(Q⸮⸮j⸮jj⸮ը(⸮CCP⸮i⸮j⸮ը(⸮CC⸮⸮⸮K?⸮+CCLK?

AT+C⸮Q⸮Zj⸮5⸮⸮(Q⸮ -j 12:34:57.763 -> 12:34:57.763 -> Arduino 收到:⸮5⸮⸮E⸮jj⸮j AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT+CCLK?

AT⸮⸮z⸮⸮z⸮⸮b⸮⸮Қ⸮Ҫ⸮Z⸮⸮j 12:34:57.866 -> 12:34:57.866 -> Arduino 收到:12:34:57.905 -> 12:34:57.905 - > Arduino 接收:OK 12:34:57.968 -> 12:34:57.968 -> Arduino 接收:AT+CCLK?

12:34:57.968 -> 12:34:57.968 -> Arduino 接收:+CCLK: "20/10/04,12:34:54+22" 12:34:58.037 -> 12:34:58.037 -> Arduino收到:12:34:58.070 -> 12:34:58.070 -> Arduino 收到:OK 12:34:58.070 -> 12:34:58.070 -> Arduino 收到:AT+CCK ⸮⸮'I⸮)j⸮H( ⸮5⸮ꨨH⸮)⸮Қ⸮Ҫ⸮Z⸮⸮j 12:34:58.138 -> 12:34:58.138 -> Arduino 收到:12:34:58.172 -> 12:34:58.172 -> Arduino 收到:OK A+⸮1-⸮jRb҂⸮jjZ⸮⸮Ҫ⸮Z⸮⸮j 12:34:58.207 -> 12:34:58.207 -> Arduino 收到:

4

2 回答 2

1

试试这个:

#include <SoftwareSerial.h>

SoftwareSerial gsm(10, 11);
String inData;

void setup() {
    Serial.begin(9600);
    gsm.begin(9600);
    delay(5000);
}

void loop() {

    inData = "";

    gsm.println("AT+CCLK?");
    delay(500);

    while (gsm.available()) {
        inData += char(gsm.read());
    }

    Serial.print("Arduino Received: ");
    Serial.println(inData);

    delay(500);
}
于 2020-10-04T10:59:39.327 回答
1

@Danny_ds

正在做while (gsm.available()) { inData += char(gsm.read()); }

如果 GSM 在您之前提供的 500 毫秒暂停期间没有机会填满缓冲区,则将比填充缓冲区更快地清空传入缓冲区。

缓冲区也只有 64 字节深,在 9600 波特率下,您可以在 500 毫秒内接收 480 个字符。这意味着如果答案长于 64 字节,您将丢失其他所有内容。

所以简而言之,你不能保证看到完整的答案。

执行此操作的正确方法是等待文档中描述的完整答案(通常您会在答案末尾得到“ OK\r\n ”)或先出现的超时。

于 2020-10-04T12:37:34.077 回答