123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- /***
- ***功能:转换$GPRMC数据
- ***版本:v0.1
- ***作者:jcxcnzj
- ***日期:2016.01.07
- ***/
- #include <cstring>
- #include <cstdio>
- #include <cstdlib>
- #include "MyGPS.h"
- #define Maxn 128
- char input[Maxn];
- int convertMyGPS(char *buffer,int buflen,SutMyGps *output);
- int findSecondComma(char *buffer,int buflen);
- int countNumOfComma(char *buffer,int buflen);
- SutMyGps MyGPS;
- //int main(void)
- //{
- // gets(input);
- // myGPS result;
- // int buflen = strlen(input);
- // int numOfComma = countNumOfComma(input,buflen);
- // if(strstr(input,"$GPRMC") == NULL || numOfComma != 12)
- // {
- // printf("Invalid parameter!\n");
- // }
- // else
- // {
- // if(!convertMyGPS(input,buflen,&result))
- // {
- // printf("Please go out!\n");
- // }
- // else
- // {
- // printf("%.5f %c\n",result.latitude,result.la_direction);
- // printf("%.5f %c\n",result.longtitude,result.lo_direction);
- // }
- // }
- //
- // system("pause");
- // return 0;
- //}
- /***
- *** 输入参数:
- char *buffer: GPS输出的信息,无效时是 $GPRMC,043423.00,V,,,,,,,070116,,,N*7E 这种形式
- 有效时是 $GPRMC,030049.00,A,3015.83980,N,12007.02584,E,0.039,,231115,,,A*73 这种形式
- 各部分代表的含义可以搜索 $GPRMC
- int buflen: GPS输出的信息长度
- myGPS *output: 用来存取转换结果
- 输出解果:
- 0:GPS定位不成功,可能在室内,或信号不好
- 1:GPS定位成功
- ***/
- int convertMyGPS(char *buffer,int buflen,SutMyGps *output)
- {
- int la_degree; //度,单位:°
- int la_minute; //分,单位:′
- double la_second; //秒,单位:″
- int lo_degree; //度,单位:°
- int lo_minute; //分,单位:′
- double lo_second; //秒,单位:″
-
- int secondCommaIndex = findSecondComma(buffer,buflen);
- if(buffer[secondCommaIndex+1] != 'A')
- {
- return 0;
- }
- else
- {
- char tempBuffer[26] = {0};
- memcpy(tempBuffer,buffer+secondCommaIndex+3,26);
- int tempLoSecond = 0;
- int tempLaSecond = 0;
- sscanf(tempBuffer,"%2d%2d.%5d,%c,%3d%2d.%5d,%c",&(la_degree),&(la_minute),&tempLaSecond,&(output->la_direction),
- &(lo_degree),&(lo_minute),&tempLoSecond,&(output->lo_direction));
-
-
- la_second = tempLaSecond * 60.0 / 100000.0;
- lo_second = tempLoSecond * 60.0 / 100000.0;
-
- output->latitude = la_degree + la_minute / 60.0 + la_second / 3600.0;
- output->longitude = lo_degree + lo_minute / 60.0 + lo_second / 3600.0;
-
- return 1;
- }
- }
- /*找到并返回GPS输出信息中第二个逗号,的位置*/
- int findSecondComma(char *buffer,int buflen)
- {
- int i;
- int total = 2;
- for(i = 0;i < buflen;++i)
- {
- if(buffer[i] == ',')
- {
- --total;
- if(total == 0)
- {
- break;
- }
- }
- }
- return i;
- }
- /*统计并返回GPS输出信息中所有逗号,的个数*/
- int countNumOfComma(char *buffer,int buflen)
- {
- int result = 0;
- int i;
- for(i = 0;i < buflen;++i)
- {
- if(buffer[i] == ',')
- {
- ++result;
- }
- }
- return result;
- }
|