MyGPS.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***
  2. ***功能:转换$GPRMC数据
  3. ***版本:v0.1
  4. ***作者:jcxcnzj
  5. ***日期:2016.01.07
  6. ***/
  7. #include <cstring>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include "MyGPS.h"
  11. #define Maxn 128
  12. char input[Maxn];
  13. int convertMyGPS(char *buffer,int buflen,SutMyGps *output);
  14. int findSecondComma(char *buffer,int buflen);
  15. int countNumOfComma(char *buffer,int buflen);
  16. SutMyGps MyGPS;
  17. //int main(void)
  18. //{
  19. // gets(input);
  20. // myGPS result;
  21. // int buflen = strlen(input);
  22. // int numOfComma = countNumOfComma(input,buflen);
  23. // if(strstr(input,"$GPRMC") == NULL || numOfComma != 12)
  24. // {
  25. // printf("Invalid parameter!\n");
  26. // }
  27. // else
  28. // {
  29. // if(!convertMyGPS(input,buflen,&result))
  30. // {
  31. // printf("Please go out!\n");
  32. // }
  33. // else
  34. // {
  35. // printf("%.5f %c\n",result.latitude,result.la_direction);
  36. // printf("%.5f %c\n",result.longtitude,result.lo_direction);
  37. // }
  38. // }
  39. //
  40. // system("pause");
  41. // return 0;
  42. //}
  43. /***
  44. *** 输入参数:
  45. char *buffer: GPS输出的信息,无效时是 $GPRMC,043423.00,V,,,,,,,070116,,,N*7E 这种形式
  46. 有效时是 $GPRMC,030049.00,A,3015.83980,N,12007.02584,E,0.039,,231115,,,A*73 这种形式
  47. 各部分代表的含义可以搜索 $GPRMC
  48. int buflen: GPS输出的信息长度
  49. myGPS *output: 用来存取转换结果
  50. 输出解果:
  51. 0:GPS定位不成功,可能在室内,或信号不好
  52. 1:GPS定位成功
  53. ***/
  54. int convertMyGPS(char *buffer,int buflen,SutMyGps *output)
  55. {
  56. int la_degree; //度,单位:°
  57. int la_minute; //分,单位:′
  58. double la_second; //秒,单位:″
  59. int lo_degree; //度,单位:°
  60. int lo_minute; //分,单位:′
  61. double lo_second; //秒,单位:″
  62. int secondCommaIndex = findSecondComma(buffer,buflen);
  63. if(buffer[secondCommaIndex+1] != 'A')
  64. {
  65. return 0;
  66. }
  67. else
  68. {
  69. char tempBuffer[26] = {0};
  70. memcpy(tempBuffer,buffer+secondCommaIndex+3,26);
  71. int tempLoSecond = 0;
  72. int tempLaSecond = 0;
  73. sscanf(tempBuffer,"%2d%2d.%5d,%c,%3d%2d.%5d,%c",&(la_degree),&(la_minute),&tempLaSecond,&(output->la_direction),
  74. &(lo_degree),&(lo_minute),&tempLoSecond,&(output->lo_direction));
  75. la_second = tempLaSecond * 60.0 / 100000.0;
  76. lo_second = tempLoSecond * 60.0 / 100000.0;
  77. output->latitude = la_degree + la_minute / 60.0 + la_second / 3600.0;
  78. output->longitude = lo_degree + lo_minute / 60.0 + lo_second / 3600.0;
  79. return 1;
  80. }
  81. }
  82. /*找到并返回GPS输出信息中第二个逗号,的位置*/
  83. int findSecondComma(char *buffer,int buflen)
  84. {
  85. int i;
  86. int total = 2;
  87. for(i = 0;i < buflen;++i)
  88. {
  89. if(buffer[i] == ',')
  90. {
  91. --total;
  92. if(total == 0)
  93. {
  94. break;
  95. }
  96. }
  97. }
  98. return i;
  99. }
  100. /*统计并返回GPS输出信息中所有逗号,的个数*/
  101. int countNumOfComma(char *buffer,int buflen)
  102. {
  103. int result = 0;
  104. int i;
  105. for(i = 0;i < buflen;++i)
  106. {
  107. if(buffer[i] == ',')
  108. {
  109. ++result;
  110. }
  111. }
  112. return result;
  113. }