arduino GPS 经纬度解析(C语言)

第一篇,还不太会用这个,哈哈哈。

目录:
原始数据
GGA行提取
纬度提取
经度提取

原始数据

$GNGGA,085731.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,16,0.9,27.8,M,0.0,M,4F
$GNGLL,3018.4819,N,12004.6687,E,085731.000,A,A
49
$GPGSA,A,3,25,33,31,29,14,12,32,10,26,34,20,35,1.6,0.9,1.331
$BDGSA,A,3,07,09,10,06,1.6,0.9,1.3
26
$GPGSV,4,1,14,10,35,186,32,12,11,038,37,14,62,345,34,20,12,171,327F
$GPGSV,4,2,14,22,16,313,26,25,43,045,38,26,31,212,32,29,21,110,29
77
$GPGSV,4,3,14,31,51,299,34,32,73,043,40,33,48,171,29,34,71,044,367A
$GPGSV,4,4,14,35,29,145,29,36,26
4B
$BDGSV,2,1,06,02,31,05,32,06,78,093,35,07,75,228,3361
$BDGSV,2,2,06,09,67,334,33,10,44,227,22
64
$GNRMC,085731.000,A,xxxx.xxxx,N,xxxxx.xxxx,E,0.34,348.93,091218,A7F
$GNVTG,348.93,T,M,0.34,N,0.63,K,A
24
$GNZDA,085731.000,09,12,2018,00,0041
$GPTXT,01,01,01,ANTENNA OK
35

GGA行提取

#define GPSMAX 670                                                             //GPS数据读取一次获得长度
#define GGA 70                                                                      //GGA长度
#define READBEGIN  600                                                      //开始提取GPS长度

char gpsdata[GPSMAX];                                                          //GPS数据提取
char read;
char gpsread[GGA];                                                             //提取的GPS字符串

void GPSread()     //GPS数据读取
{
  int kk = 0;
  if (gpsSerial.available()) {
    gpsdata[i++] = (char)gpsSerial.read();                                                                       //读取每一个元素
    if(i >= READBEGIN) {
      int ii = 0;
      while(ii < i){
        if(ii+GPSMAX-READBEGIN < GPSMAX && gpsdata[ii] == 'G' && gpsdata[ii+1] == 'G' && gpsdata[ii+2] == 'A'){ 
         //对GGA那一行进行提取
          int tt = ii;
          while(gpsdata[tt] != '\n'){
            gpsread[kk++] = gpsdata[tt];
            //Serial.print(gpsdata[tt]);                                                                            //输出提取的GPS数据 
            tt++;
          }
          gpsread[kk+1] = '\n';
          Serial.print('\n');
        }
        ii++;
      }
      memset(gpsdata, 0, GPSMAX);
      i = 0;
    } //clear info
  }
}

结果:
16:52:28.855 -> $GNGGA,085229.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,18.3,M,0.0,M,*43
16:52:29.856 -> $GNGGA,085230.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,17.6,M,0.0,M,*4D
16:52:30.857 -> $GNGGA,085231.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,16.9,M,0.0,M,*4F
16:52:31.852 -> $GNGGA,085232.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,16.5,M,0.0,M,*4C
16:52:32.817 -> $GNGGA,085233.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,15.8,M,0.0,M,*44
16:52:33.851 -> $GNGGA,085234.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,15.1,M,0.0,M,*48
16:52:34.853 -> $GNGGA,085235.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,15.0,M,0.0,M,*44
16:52:35.826 -> $GNGGA,085236.000,xxxx.xxxx,N,xxxxx.xxxx,E,1,12,1.0,13.2,M,0.0,M,*41

纬度提取

double wei
void weiduread()    //纬度获取
{
  int tt = 0;
  while(gpsread[tt] != '\n'){
    if (gpsread[tt] == 'N'){                                  //纬度提取 xx.xxxx
      int j;
      for (j = tt-2; gpsread[j] != ','; j--){
        ;
      }
      j++;
        wei = (gpsread[j]-48)*10 + (gpsread[j+1]-48) + ((gpsread[j+2]-48)*10 + (gpsread[j+3]-48) + (gpsread[j+5]-48)/10.0 + (gpsread[j+6]-48)/100.0 + (gpsread[j+7]-48)/1000.0 + (gpsread[j+8]-48)/10000.0)/60.0;
        Serial.println(wei);
      }
      else ;
    }
    tt++;
  }
}

经度提取

double jing
void jingduread()   //经度获取
{
  int tt = 0;
  while(gpsread[tt] != '\n'){
    if (gpsread[tt] == 'E'){                                     //经度提取 xxx.xxxx
      int j, k;
      for (j = tt-2; gpsread[j] != ','; j--){
        ;
      }
      j++;
      jing = (gpsread[j]-48)*100 + (gpsread[j+1]-48)*10 + (gpsread[j+2]-48);
      jing = jing + ((gpsread[j+3]-48)*10 + (gpsread[j+4]-48) + (gpsread[j+6]-48)/10.0 + (gpsread[j+7]-48)/100.0 + (gpsread[j+8]-48)/1000.0 + (gpsread[j+9])/10000.0)/60.0;
      Serial.println(jing);
      }
      }
      else ;
    }
    tt++;
  }
}

说明:很早写的,用过,稍微改了改,可能会有点问题。这种解析方式会不定时出现乱码,这是由于每次接收到的数据长度不一导致的,选择不同的宏定义长度会有不同的结果。当然,更好地方式便是去下载TinyGPS++.h库,网上找得到,引用也很简单,而且有速度、航向角等其他有用的数据,程序如下:

#include 
double weiread()
{
  return (gps.location.lat());
}

double jingread()
{
  return (gps.location.lng());
}
double highread()
{
  return(gps.altitude.value());
}
double degreeread()
{
  return (gps.course.value());
}
double vread()
{
  return (gps.speed.mps());
}

然后再返回到一个double类型就可以用啦!

参考资料
[1]: https://blog.csdn.net/Fourier_Legend/article/details/84107998
[2]: https://blog.csdn.net/Fourier_Legend/article/details/84107494
里面很详细!

你可能感兴趣的:(arduino)