C++实现GNSS/GPS的NMEA数据格式解析类示例

nmea数据具体各字段的含义请参考标准定义,这里给出一个C++实现的例子,环境是Android,本文只解析了几个常用的字段;

本示例的关键方法有split、startswith方法和stringToNumber模版函数


   
   
  1. bool GnssNmeaParser::startsWith(const std::string &src, const std::string &str)
  2. {
  3. int srcpos = 0;
  4. int srclen = src. length();
  5. int sublen = str. length();
  6. if (srclen < sublen) {
  7. return false;
  8. }
  9. return ( 0 == src. compare(srcpos, sublen, str));
  10. }
  11. bool GnssNmeaParser::endsWith(const std::string &src, const std::string &str)
  12. {
  13. int srcpos = 0;
  14. int srclen = src. length();
  15. int sublen = str. length();
  16. if (srclen < sublen) {
  17. return false;
  18. }
  19. srcpos = srclen - sublen;
  20. return ( 0 == src. compare(srcpos, sublen, str));
  21. }

   
   
  1. template < class T>
  2. T stringToNumber(const std::string &sstr)
  3. {
  4. T number {};
  5. std::istringstream iss {};
  6. iss. str(sstr);
  7. iss >> number; /* can auto remove leading 0 */
  8. return number;
  9. }

   
   
  1. size_t GnssNmeaParser::split(const std::string &line, const std::string &delim, std::vector<std::string> &vstr)
  2. {
  3. size_t pstart = 0;
  4. size_t phit = 0;
  5. std::string sstr;
  6. size_t length = line. length();
  7. vstr. clear();
  8. for (;pstart <= length;)
  9. {
  10. phit = line. find(delim, pstart);
  11. if (std::string::npos != phit)
  12. {
  13. /* find delim, get substr */
  14. sstr = line. substr(pstart, phit-pstart);
  15. vstr. push_back(sstr);
  16. pstart = phit + delim. size();
  17. } else {
  18. /* not find delim, append remaining str and break */
  19. vstr. push_back(line. substr(pstart));
  20. break;
  21. }
  22. }
  23. return vstr. size();
  24. }

for Android Gnss V1.0

解析.h头文件


   
   
  1. #ifndef HAL_GNSS_V1_0_GNSSNMEAPARSER_H
  2. #define HAL_GNSS_V1_0_GNSSNMEAPARSER_H
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <bitset>
  7. #include <vector>
  8. #include <map>
  9. #include <queue>
  10. #include <hardware/gps.h>
  11. #define GNSS_NMEA_LINE_SEP "\n"
  12. #define GNSS_NMEA_ELEMENT_SEP ","
  13. #define GNSS_NMEA_PARSER_VERSION "v1.0-20.0716"
  14. #if __GNSS_HAL_DEBUG_ON__
  15. /* Flags that indicate information about the satellite */
  16. typedef uint8_t LocGnssSvFlags;
  17. #define GNSS_SV_FLAGS_NONE 0
  18. #define GNSS_SV_FLAGS_HAS_EPHEMERIS_DATA (1)
  19. #define GNSS_SV_FLAGS_HAS_ALMANAC_DATA (2)
  20. #define GNSS_SV_FLAGS_USED_IN_FIX (4)
  21. //hardware/libhardware/include/hardware/gps.h
  22. typedef int64_t GpsUtcTime;
  23. /**
  24. * Flags that indicate information about the satellite
  25. */
  26. typedef uint8_t GnssSvFlags;
  27. /**
  28. * Constellation type of GnssSvInfo
  29. */
  30. typedef uint8_t GnssConstellationType;
  31. /** Represents a location. */
  32. typedef struct {
  33. /** set to sizeof(GpsLocation) */
  34. size_t size;
  35. /** Contains GpsLocationFlags bits. */
  36. uint16_t flags;
  37. /** Represents latitude in degrees. */
  38. double latitude;
  39. /** Represents longitude in degrees. */
  40. double longitude;
  41. /**
  42. * Represents altitude in meters above the WGS 84 reference ellipsoid.
  43. */
  44. double altitude;
  45. /** Represents speed in meters per second. */
  46. float speed;
  47. /** Represents heading in degrees. */
  48. float bearing;
  49. /** Represents expected accuracy in meters. */
  50. float accuracy;
  51. /** Timestamp for the location fix. */
  52. GpsUtcTime timestamp;
  53. } GpsLocation;
  54. typedef struct {
  55. /** set to sizeof(GnssSvInfo) */
  56. size_t size;
  57. /**
  58. * Pseudo-random number for the SV, or FCN/OSN number for Glonass. The
  59. * distinction is made by looking at constellation field. Values should be
  60. * in the range of:
  61. *
  62. * - GPS: 1-32
  63. * - SBAS: 120-151, 183-192
  64. * - GLONASS: 1-24, the orbital slot number (OSN), if known. Or, if not:
  65. * 93-106, the frequency channel number (FCN) (-7 to +6) offset by + 100
  66. * i.e. report an FCN of -7 as 93, FCN of 0 as 100, and FCN of +6 as 106.
  67. * - QZSS: 193-200
  68. * - Galileo: 1-36
  69. * - Beidou: 1-37
  70. */
  71. int16_t svid;
  72. /**
  73. * Defines the constellation of the given SV. Value should be one of those
  74. * GNSS_CONSTELLATION_* constants
  75. */
  76. GnssConstellationType constellation;
  77. /**
  78. * Carrier-to-noise density in dB-Hz, typically in the range [0, 63].
  79. * It contains the measured C/N0 value for the signal at the antenna port.
  80. *
  81. * This is a mandatory value.
  82. */
  83. float c_n0_dbhz;
  84. /** Elevation of SV in degrees. */
  85. float elevation;
  86. /** Azimuth of SV in degrees. */
  87. float azimuth;
  88. /**
  89. * Contains additional data about the given SV. Value should be one of those
  90. * GNSS_SV_FLAGS_* constants
  91. */
  92. GnssSvFlags flags;
  93. } GnssSvInfo;
  94. /**
  95. * Legacy struct to represents SV information.
  96. * Deprecated, to be removed in the next Android release.
  97. * Use GnssSvInfo instead.
  98. */
  99. typedef struct {
  100. /** set to sizeof(GpsSvInfo) */
  101. size_t size;
  102. /** Pseudo-random number for the SV. */
  103. int prn;
  104. /** Signal to noise ratio. */
  105. float snr;
  106. /** Elevation of SV in degrees. */
  107. float elevation;
  108. /** Azimuth of SV in degrees. */
  109. float azimuth;
  110. } GpsSvInfo;
  111. /**
  112. * Legacy struct to represents SV status.
  113. * Deprecated, to be removed in the next Android release.
  114. * Use GnssSvStatus instead.
  115. */
  116. typedef struct {
  117. /** set to sizeof(GpsSvStatus) */
  118. size_t size;
  119. int num_svs;
  120. GpsSvInfo sv_list[GPS_MAX_SVS];
  121. uint32_t ephemeris_mask;
  122. uint32_t almanac_mask;
  123. uint32_t used_in_fix_mask;
  124. } GpsSvStatus;
  125. typedef struct {
  126. /** set to sizeof(GnssSvStatus) */
  127. size_t size;
  128. /** Number of GPS SVs currently visible, refers to the SVs stored in sv_list */
  129. int num_svs;
  130. /**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值