获取整个GPRMC数据

时间:2016-06-02 06:21:11

标签: c++ arduino

嘿,伙计们这是我现在的代码

#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 / Transit数据 http://aprs.gids.nl/nmea/

例如 $ GPRMC,hhmmss.ss,A,llll.ll,一个,yyyyy.yy,一个,x.x中,x.x中,DDMMYY,x.x中,一个* HH

1 =定位的UTC

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

3 =修正的纬度

4 = N或S

5 =修复经度

6 = E或W

7 =结节速度

8 =轨道在度数上表现良好

9 = UT日期

10 =磁变化度(Easterly var。从真实过程中减去)

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

1 个答案:

答案 0 :(得分: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 Data Model页面显示了如何获取其他字段。

其他说明:

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