获取整个 GPRMC 数据

问题描述 投票:0回答:2

大家好,这是我当前的代码

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

SoftwareSerial GPS(4,5);                             /* Software serial - RX, TX pins                           */
File Cordinates;

/*........................................ Variables declaration .............................................  */

char Rec_data;
char RMC_flag=0,comma=0,i=0,j=0;
char latitude[12],longitude[12];
int smsflag=0,stable=0,finish=0,snd=0;

/*............................................... Setup ....................................................... */

void setup() 
{
  Serial.begin(9600);                                /* Initialize serial communication at 9600 bits per second */
  GPS.begin(9600);                                   /* Initialize software serial at 9600 bps for GPS          */

  while (!SD.begin(9)) 
  {
    Serial.println("SD Card Initialization failed!");
    return;
  }
  Serial.println("SD Card Initialized");

  Serial.print("AT\r\n");                            /* Initialization command                                  */
  delay(1000); 
  Serial.print("ATE0\r\n");                          /* Turn echo off                                           */
  delay(1000);
  Serial.print("AT+CMGF=1\r\n");                     /* Text mode                                               */
  delay(1000); 
  Serial.print("AT+CNMI=2,1,0,0,0\r\n");             /* Set message format                                      */
  delay(1000);
}

/*...................................................Loop...................................................... */

void loop()
{
  if(smsflag==0 && finish==1 && snd==0)              /* Send message after storing GPS cordinates               */
  {
    Serial.print("AT+CMGS=\"+91xxxxxxxxxx\"\r\n");   /* Replace xxxxxxxxxx with a valid 10-digit mobile no:     */
    delay(1000); 
  }    

  if(smsflag==1 && finish==1 && snd==0)              /* Send the message                                        */
  {
    Serial.print("Latitude:");  
      Serial.print(latitude);                        /* Send latitude                                           */
    Serial.print("\r\n");
    Serial.print("Longitude:");
      Serial.print(longitude);                       /* Send longitude                                          */
    Serial.print('\x1A');                            /* Send Ctrl+Z after the message                           */

    Cordinates = SD.open("GPS.txt", FILE_WRITE);     /* Open the file GPS.txt to write cordinates               */

    if(Cordinates)                                   /* If the file opened okay, write to it                    */
    {
      Serial.print("Writing to GPS.txt...");
      Cordinates.print("Latitude:");
      Cordinates.println(latitude);                  /* Write latitude to the file                              */
      Cordinates.print("Longitude:");
      Cordinates.println(longitude);                 /* Write longitude to the file                             */
      Cordinates.close();                            /* Close the file                                          */
      Serial.println("done.");
    } 
    else
    {
      Serial.println("Error opening GPS.txt");       /* If the file didn't open, print an error                 */
    }

    Cordinates = SD.open("GPS.txt");                 /* Re-open the file for reading                            */
    if(Cordinates)
    {
      Serial.println("GPS.txt");
      while (Cordinates.available())                 /* Read from the file until there's nothing else in it     */
      {
        Serial.write(Cordinates.read());
      }
      Cordinates.close();                            /* Close the file                                          */

/*----------- Uncomment these lines if you want to delete the file ---------------------------------------------

      Serial.println("Removing GPS.txt...");
      SD.remove("GPS.txt");
      Serial.println("Removed GPS.txt...");

----------------------------------------------------------------------------------------------------------------*/
    } 
    else
    {
      Serial.println("Error opening GPS.txt");       /* If the file didn't open, print an error                 */
    }  

    i = 0;
    j = 0;
    RMC_flag = 0;
    comma = 0;                                       /* Clear the variables                                     */
    snd = 1;
    smsflag = 0;
    finish = 0;
    stable = 0;
  }

  while(GPS.available())                             /* Check if any data has arrived in software UART          */
  {
    Rec_data = GPS.read();                           /* Copy the received charactr to a variable                */

   if(Rec_data == 'G')                /* Check for GPRMC header */
 { 
 RMC_flag = 1;
 }
 if(Rec_data == 'P' && RMC_flag==1)  /* Check for GPRMC header */
 { 
 RMC_flag = 2;
 }
    if(Rec_data == 'R' && RMC_flag==2)               /* Check for GPRMC header 

                             */
    {                                                  
      RMC_flag = 3;
    }

    else if(Rec_data=='M' && RMC_flag==3) 
    {
      RMC_flag = 4;
    }

    else if(Rec_data=='C' && RMC_flag==4)
    { 
      RMC_flag = 5;
    }

    if(RMC_flag == 5)
    {
      if(Rec_data==',') 
         comma++;                                    /* If GPRMC header is received, count the no: of commas    */  

      if(comma==2 && Rec_data=='A')                  /* Check if GPS is stable                                  */
      {
         stable=1;
      }  

      else if(comma==2 && Rec_data=='V')
      {
         stable=0;
         comma=0;
         RMC_flag=0;
      }  

      else if(comma>=3 && comma<5 && Rec_data!=',' && stable==1)
      {   
        latitude[i++]=Rec_data;                      /* Store latitude in an array                              */
      }

      else if(comma>=5 && comma<7 && Rec_data!=',' && stable==1)
      {   
        longitude[j++]=Rec_data;                     /* Store longitude in an array                             */
      }

      if(Rec_data=='*' && stable==1)
      {
        comma=0; finish=1;
      }
    }
  } 

  while (Serial.available())                         /* Check if any data has arrived in hardware UART          */
  {
    Rec_data = Serial.read();                        /* Copy the received character to a variable               */
    if(Rec_data=='>')                                /* Set flag for response to "AT+CMGS=\"+91xxxxxxxxxx\""    */
    {
      smsflag = 1;
    } 
  }
}

我想要做的是获取所有 GPS/交通数据 http://aprs.gids.nl/nmea/

例如 $GPRMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,x.x,a*hh

1 = 定位的 UTC

2 = 数据状态(V = 导航接收器警告)

3 = 修复纬度

4 = N 或 S

5 = 定位经度

6 = E 或 W

7 = 地面速度(节)

8 = 轨道良好度数 True

9 = 世界标准日期

10 = 磁力变化度数(从真实航向中减去东向变化)

11 = E 或 W

12 = 校验和

我想获取整个 gprmc 数据 当前输出为

纬度:1000.7898N 经度:0761 纬度:1000.7898N 经度:0761 纬度:1000.7898N 经度:0761 纬度:1000.7898N 经度:0761 纬度:1000.7652N 经度:07619.8605E 纬度:1000.7617N 经度:07619.8636E 纬度:1000.7662N 经度:07619.8566E

c++ arduino
2个回答
0
投票

如果您想要所有数据字段,请尝试使用我的 GPS 库,NeoGPS。它确实简化了所有数据字段的解析。这是您的草图,已修改为使用 NeoGPS:

#include <SPI.h>
#include <SD.h>
#include <NeoSWSerial.h>
#include "NMEAGPS.h"

NeoSWSerial gps_port( 4, 5 );
NMEAGPS gps;
File    Cordinates;

bool smsflag = false;
bool snd     = false;

//--------------------------

void setup()
{
  Serial.begin(9600);
  while (!Serial)
    ;
  gps_port.begin(9600);

  while (!SD.begin(9)) {
    Serial.println( F("SD Card Initialization failed!") );
    return;
  }
  Serial.println( F("SD Card Initialized") );

  Serial.print( F("AT\r\n") );                // Initialization command
  delay(1000); 
  Serial.print( F("ATE0\r\n") );              // Turn echo off 
  delay(1000);
  Serial.print( F("AT+CMGF=1\r\n") );         // Text mode
  delay(1000); 
  Serial.print( F("AT+CNMI=2,1,0,0,0\r\n") ); // Set message format
  delay(1000);
}

//--------------------------

void loop()
{
  while (gps.available( gps_port )) {
    gps_fix fix = gps.read();

    if (fix.valid.location && !snd) {

      if (!smsflag) {
        Serial.print( F("AT+CMGS=\"+91xxxxxxxxxx\"\r\n") );
      } else {
        Serial.print( F("Latitude:") );  
        Serial.print( fix.latitude(), 6 ); // floating-point format
        Serial.print( F("\r\n") );
        Serial.print( F("Longitude:") );
        Serial.print( fix.longitude(), 6 ); // floating-point format
        Serial.print( '\x1A' );   /* Send Ctrl+Z after the message */
        snd     = true;

        Cordinates = SD.open("GPS.txt", FILE_WRITE); // (O_APPEND | O_WRITE | O_CREAT) ?

        if (Cordinates) {
          Serial.print( F("Writing to GPS.txt...") );
          Cordinates.print( F("Latitude:") );
          Cordinates.print( fix.latitude(), 6 ); // floating-point format
          Cordinates.print( F"Longitude:") );
          Cordinates.print( fix.longitude(), 6 ); // floating-point format
          Cordinates.close();
          Serial.println( F("done.") );
        } else {
          Serial.println( F("Error opening GPS.txt") );
        }

        smsflag = false;
      }
    }
  }

  while (Serial.available()) {
    char c = Serial.read();
    if (c == '>') { /* Set flag for response to "AT+CMGS=\"+91xxxxxxxxxx\""    */
      smsflag = true;
    } 
  }
}

NeoGPS 数据模型页面显示了如何获取其他字段。

其他注意事项:

  • 纬度/经度以浮点度数显示,而不是原始 NMEA 句子中时髦的 DDMM.MMMM(两位数的度数和 6 位或更多位的浮点分:P)。
  • 如果
    snd
    smsflag
    只能具有值 0 和 1,请改用
    bool
    类型。它们只能具有值
    true
    false
  • 您确定只想发送 1 份报告吗?
    snd
    永远不会被清除。
  • 您确定要每秒发送 AT+CMGS 命令吗?也许应该在发送坐标之前发送一次?
  • delay
    中使用
    loop()
    通常是不好的,所以在坐标之后发送AT+CMGS。下一个 GPS 数据将在大约 1 秒内不可用,从而提供类似的延迟。
  • NeoSWSerial 库比内置 SoftwareSerial 库更可靠。如果您可以使用引脚 8 和 9,AltSoftSerial 会更好。
  • 您使用什么 Arduino 和 GPS 设备?可能还需要考虑其他 NeoGPS 或串行设置。例如,F宏用于 8 位 AVR,以节省大量 RAM。

0
投票

也许只需将 $GPRMC 消息作为字符串获取并全部或部分保存。

无效循环(){ 如果 (gpsSerial.available() > 0) {

// Read data from GPS module.
String data = gpsSerial.readStringUntil('\n');

// Check if data contains time information (assuming it starts with $GPRMC).
if (data.startsWith("$GPRMC")) {
  // Parse GPS data for time and do the other juicy stuff.
  parseGPSData(data);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.