2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#include
#include
#include
#include
#include
#include
#include
#include
/*
// 由于$GPRMC比较重要,所以重点讲解:
// $GPRMC(Recommended Minimum Specific GPS/TRANSIT Data)
//
// 帧头 UTC时间 状态 纬度 北纬/南纬 经度 东经/西经 速度
// $GPRMC hhmmss.sss A/V ddmm.mmmm N/S dddmm.mmmm E/W 节
//
// 方位角 UTC日期 磁偏角 磁偏角方向 模式 校验 回车换行
// 度 ddmmyy 000 - 180 E/W A/D/E/N *hh CR+LF
//
// 格 式: $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>,<12>*hh
// $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
//
// $GPRMC,175622,A,2921.219000,N,11946.287000,E,13.00,348.27,20161006,,,D*4E
//
// 说 明:
// 字段 0:$GPRMC,语句ID,表明该语句为Recommended Minimum Specific GPS/TRANSIT Data(RMC)推荐最小定位信息
// 字段 1:UTC时间,hhmmss.sss格式
// 字段 2:状态,A=定位,V=未定位
// 字段 3:纬度ddmm.mmmm,度分格式(前导位数不足则补0)
// 字段 4:纬度N(北纬)或S(南纬)
// 字段 5:经度dddmm.mmmm,度分格式(前导位数不足则补0)
// 字段 6:经度E(东经)或W(西经)
// 字段 7:速度,节,Knots(一节也是1.852千米/小时)
// 字段 8:方位角,度(二维方向指向,相当于二维罗盘)
// 字段 9:UTC日期,DDMMYY格式
// 字段10:磁偏角,(000 - 180)度(前导位数不足则补0)
// 字段11:磁偏角方向,E=东,W=西
// 字段12:模式,A=自动,D=差分,E=估测,N=数据无效(3.0协议内容)
// 字段13:校验值
// NMEA Decoding here
// Wait for gps data, want RMC sentence:
//$GPRMC,230046,A,3759.8006,N,12205.4429,W,0.0,190.3,260702,15.1,E,A*3C
// | | / | / | |
//$GPRMC,233834,A,3759.842,N,12205.440,W,000.0,173.7,200602,015.8,E*63
// | | | | | | | | |--checksum
// | | | | | | | |- Magnetic Deviation?
// | | | | | | |- course over ground
// | | | | | |- speed over ground
// | | | | |--Lon E/W
// | | | |-- Lon 122' 5.44"
// | | |-- Lat N/S
// | |-- Lat 37' 59.842"
// |-- A=Good, V=Bad
*/
typedef
struct
{
double
rcv_time;
char
status;
double
lat;
char
lat_direct;
double
lon;
char
lon_direct;
double
sog;
double
cog;
int
date;
double
mag_variation;
char
mag_var_direct;
char
mode;
int
chksum;
} gprmc_format;
// 计算校验:异或
int
checksum(
const
char
* s);
// 打印错误信息
int
print_error(
int
err);
// 打印 GPS坐标速度时间戳
void
print_gps_point(
FILE
* outfile, gprmc_format* gps_point);
using
namespace
std;
int
main(
int
argc,
char
* argv[])
{
// 从数据读取一行
char
line[512] =
"$GPRMC,183859,A,2938.861000,N,11951.912000,E,39.00,34.06,20161006,,,D*7A"
;
if
(2 == argc)
strcpy
(line, argv[1]);
// 接受一个 参数输入数据
gprmc_format rmc;
int
chksum = 0;
char
tempstr[512];
char
finalstr[512];
if
(
'$'
!= line[0]) {
print_error(1);
return
-1;
}
else
{
char
* pch =
strrchr
(line,
'*'
);
if
(pch != NULL) {
*pch =
'\0'
;
rmc.mode = *(pch - 1);
pch++;
rmc.chksum =
strtol
(pch, &pch, 16);
// printf("%X\n", chksum);
if
(rmc.chksum != checksum(line + 1)) {
print_error(2);
return
-1;
}
}
else
{
print_error(3);
return
-1;
}
pch =
strtok
(line,
","
);
if
((pch != NULL) && !
strcmp
(pch,
"$GPRMC"
)) {
// printf("%s\n", pch); //GPRMC
pch =
strtok
(NULL,
","
);
rmc.rcv_time =
atof
(pch);
pch =
strtok
(NULL,
","
);
rmc.status = *pch;
pch =
strtok
(NULL,
","
);
rmc.lat =
atof
(pch);
pch =
strtok
(NULL,
","
);
rmc.lat_direct = *pch;
pch =
strtok
(NULL,
","
);
rmc.lon =
atof
(pch);
pch =
strtok
(NULL,
","
);
rmc.lon_direct = *pch;
pch =
strtok
(NULL,
","
);
rmc.sog =
atof
(pch);
pch =
strtok
(NULL,
","
);
rmc.cog =
atof
(pch);
pch =
strtok
(NULL,
","
);
rmc.date =
atoi
(pch);
// 一般空
rmc.mag_variation = 0;
rmc.mag_var_direct =
'W'
;
rmc.mode = rmc.mode;
// 之前已经读到
}
else
{
print_error(4);
return
-1;
}
}
// 测试重新生成 GPRMC 数据,校验值会变化
sprintf
(tempstr,
"%s,%06.0f,%c,%.5f,%c,%.5f,%c,%.2f,%.1f,%06u,,,%c"
,
"GPRMC"
,
rmc.rcv_time,
rmc.status,
rmc.lat,
rmc.lat_direct,
rmc.lon,
rmc.lon_direct,
rmc.sog,
rmc.cog,
rmc.date,
rmc.mode
);
//printf("%s\n", tempstr);
chksum = checksum(tempstr);
sprintf
(finalstr,
"$%s*%X"
, tempstr, chksum);
printf
(
"%s\n"
, finalstr);
// 打印 GPS坐标速度时间戳
fprintf
(stdout,
"纬度\t经度\t时速(Km/H)\t时间戳\n"
);
print_gps_point(stdout, &rmc);
return
0;
}
// 计算校验:异或
int
checksum(
const
char
* s)
{
int
c = 0;
while
(*s)
c ^= *s++;
return
c;
}
// 打印错误信息
int
print_error(
int
err)
{
switch
(err) {
case
1:
printf
(
"行首不是 $ --> "
);
break
;
case
2:
printf
(
"校验值错误 --> "
);
break
;
case
3:
printf
(
"找不到校验值 --> "
);
break
;
case
4:
printf
(
"不是$GPRMC帧头--> "
);
break
;
default
:
printf
(
"数据错误 --> "
);
break
;
}
printf
(
"请检查是否 NMEA 183 数据!\n"
);
}
void
print_gps_point(
FILE
* outfile, gprmc_format* gps_point)
{
fprintf
(outfile,
"%.5f\t"
, gps_point->lat / 100);
// 纬度
fprintf
(outfile,
"%.5f\t"
, gps_point->lon / 100);
// 经度度
fprintf
(outfile,
"%.1f\t"
, gps_point->sog * 1.852);
// 时速:节换算公里
// strftime(time_str, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
char
time_str [80] =
""
;
int
date = gps_point->date;
int
time
= (
int
)gps_point->rcv_time;
if
(date < 2000 * 10000)
date += 2000 * 10000;
sprintf
(time_str,
"%d-%02d-%02d %02d:%02d:%02d"
,
date / 10000, (date % 10000) / 100, date % 100,
time
/ 10000, (
time
% 10000) / 100,
time
% 100);
fprintf
(outfile,
"%s\n"
, time_str);
}
|