/*** ***功能:转换$GPRMC数据 ***版本:v0.1 ***作者:jcxcnzj ***日期:2016.01.07 ***/ #include #include #include #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; }