-
Notifications
You must be signed in to change notification settings - Fork 0
/
ozex_gps.cpp
executable file
·393 lines (324 loc) · 9.97 KB
/
ozex_gps.cpp
1
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "ozex_gps.h"
#include <time.h>
FILE* track = NULL;
/** do something with the GGA data */
static void print_gga(nmeap_gga_t *gga)
{
printf("found GPGGA message %.6f %.6f %.0f %lu %d %d %f %f\n",
gga->latitude ,
gga->longitude,
gga->altitude ,
gga->time ,
gga->satellites,
gga->quality ,
gga->hdop ,
gga->geoid
);
}
/** called when a gpgga message is received and parsed */
static void gpgga_callout(nmeap_context_t *context,void *data,void *user_data)
{
nmeap_gga_t *gga = (nmeap_gga_t *)data;
// printf("-------------callout\n");
// print_gga(gga);
}
/** do something with the RMC data */
static void print_rmc(nmeap_rmc_t *rmc)
{
printf("found GPRMC Message %lu %c %.6f %.6f %f %f %lu %f\n",
rmc->time,
rmc->warn,
rmc->latitude,
rmc->longitude,
rmc->speed,
rmc->course,
rmc->date,
rmc->magvar
);
}
/** called when a gprmc message is received and parsed */
static void gprmc_callout(nmeap_context_t *context,void *data,void *user_data)
{
nmeap_rmc_t *rmc = (nmeap_rmc_t *)data;
// printf("-------------callout\n");
// print_rmc(rmc);
if (track)
{
fprintf(track, " %2.6f, %2.6f,0,-777,,,\r\n",
rmc->latitude, rmc->longitude);
}
}
/*************************************************************************
* LINUX IO
*/
/**
* open the specified serial port for read/write
* @return port file descriptor or -1
*/
int openPort(const char *tty,int baud)
{
int status;
int fd;
struct termios newtio;
// open the tty
fd = open(tty,O_RDWR | O_NOCTTY);
if (fd < 0) {
perror("open");
return fd;
}
// flush serial port
status = tcflush(fd, TCIFLUSH);
if (status < 0) {
perror("tcflush");
close(fd);
return -1;
}
/* get current terminal state */
tcgetattr(fd,&newtio);
// set to raw terminal type
newtio.c_cflag = baud | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNBRK | IGNPAR;
newtio.c_oflag = 0;
// control parameters
newtio.c_cc[VMIN] = 1; // block for at least one charater
// set its new attrigutes
status = tcsetattr(fd,TCSANOW,&newtio);
if (status < 0) {
perror("tcsetattr");
close(fd);
fd = -1;
return fd;
}
return fd;
}
/* ---------------------------------------------------------------------------------------*/
/* STEP 1 : allocate the data structures. be careful if you put them on the stack because */
/* they need to be live for the duration of the parser */
/* ---------------------------------------------------------------------------------------*/
static nmeap_context_t nmea; /* parser context */
static nmeap_gga_t gga; /* this is where the data from GGA messages will show up */
static nmeap_rmc_t rmc; /* this is where the data from RMC messages will show up */
static int user_data; /* user can pass in anything. typically it will be a pointer to some user data */
static int fd = 0;
double ozex_gps_lat()
{
return rmc.latitude;
}
double ozex_gps_lon()
{
return rmc.longitude;
}
static void ozex_gps_init_internals()
{
int status;
status = nmeap_init(&nmea,(void *)&user_data);
if (status != 0) {
printf("nmeap_init %d\n",status);
exit(1);
}
/* ---------------------------------------*/
/*STEP 3 : add standard GPGGA parser */
/* -------------------------------------- */
status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga);
if (status != 0) {
printf("nmeap_add %d\n",status);
exit(1);
}
/* ---------------------------------------*/
/*STEP 4 : add standard GPRMC parser */
/* -------------------------------------- */
status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc);
if (status != 0) {
printf("nmeap_add %d\n",status);
exit(1);
}
struct tm *time_date_struct_ptr;
time_t timer;
time( &timer );
time_date_struct_ptr = localtime( &timer );
char filename[128];
strcpy(filename, asctime( time_date_struct_ptr ));
char *s = strchr(filename, '\n');
*s = 0;
strcat(filename, ".plt");
track = fopen(filename, "wt");
fprintf(track, "OziExplorer Track Point File Version 2.1\r\n");
fprintf(track, "WGS 84\r\n");
fprintf(track, "Altitude is in Feet\r\n");
fprintf(track, "Reserved 3\r\n");
fprintf(track, "0,2,255,Track Log,0,0,2,8421376\r\n");
fprintf(track, "100\r\n");
}
void ozex_gps_start(const char *port, int bauds)
{
int baudrate = B9600;
switch (bauds)
{
case 4800: baudrate = B4800; break;
case 9600: baudrate = B9600; break;
case 19200: baudrate = B19200; break;
case 38400: baudrate = B38400; break;
case 57600: baudrate = B57600; break;
case 115200: baudrate = B115200; break;
default:
break;
}
fd = openPort(port, baudrate);
if (fd < 0)
{
printf("openPort %d\n",fd);
}
ozex_gps_init_internals();
}
void ozex_gps_start_fromfile(const char *port)
{
fd = open(port, O_RDONLY);
if (fd < 0)
{
printf("openPort %d\n",fd);
}
ozex_gps_init_internals();
}
void ozex_gps_update()
{
int status;
int rem;
int offset;
int len;
static char buffer[4096];
len = rem = read(fd,buffer,sizeof(buffer));
if (len <= 0)
{
perror("read");
return;
}
offset = 0;
while(rem > 0)
{
status = nmeap_parseBuffer(&nmea,&buffer[offset],&rem);
offset += (len - rem);
/*
switch(status)
{
case NMEAP_GPGGA:
printf("-------------switch\n");
print_gga(&gga);
printf("-------------\n");
break;
case NMEAP_GPRMC:
printf("-------------switch\n");
print_rmc(&rmc);
printf("-------------\n");
break;
default:
break;
}
*/
}
}
void ozex_gps_close()
{
if (track)
{
fclose(track);
}
close(fd);
}
#if 0
int main(int argc,char *argv[])
{
int status;
int rem;
int offset;
int len;
char buffer[32];
int fd;
const char *port = "/dev/ttyS0";
// default to ttyS0 or invoke with 'linux_nmeap <other serial device>'
if (argc == 2) {
port = argv[1];
}
/* --------------------------------------- */
/* open the serial port device */
/* using default 4800 baud for most GPS */
/* --------------------------------------- */
fd = openPort(port,B4800);
if (fd < 0) {
/* open failed */
printf("openPort %d\n",fd);
return fd;
}
status = nmeap_init(&nmea,(void *)&user_data);
if (status != 0) {
printf("nmeap_init %d\n",status);
exit(1);
}
/* ---------------------------------------*/
/*STEP 3 : add standard GPGGA parser */
/* -------------------------------------- */
status = nmeap_addParser(&nmea,"GPGGA",nmeap_gpgga,gpgga_callout,&gga);
if (status != 0) {
printf("nmeap_add %d\n",status);
exit(1);
}
/* ---------------------------------------*/
/*STEP 4 : add standard GPRMC parser */
/* -------------------------------------- */
status = nmeap_addParser(&nmea,"GPRMC",nmeap_gprmc,gprmc_callout,&rmc);
if (status != 0) {
printf("nmeap_add %d\n",status);
exit(1);
}
/* ---------------------------------------*/
/*STEP 5 : process input until done */
/* -------------------------------------- */
for(;;) {
/* ---------------------------------------*/
/*STEP 6 : get a buffer of input */
/* -------------------------------------- */
len = rem = read(fd,buffer,sizeof(buffer));
if (len <= 0) {
perror("read");
break;
}
/* ----------------------------------------------*/
/*STEP 7 : process input until buffer is used up */
/* --------------------------------------------- */
offset = 0;
while(rem > 0) {
/* --------------------------------------- */
/*STEP 8 : pass it to the parser */
/* status indicates whether a complete msg */
/* arrived for this byte */
/* NOTE : in addition to the return status */
/* the message callout will be fired when */
/* a complete message is processed */
/* --------------------------------------- */
status = nmeap_parseBuffer(&nmea,&buffer[offset],&rem);
offset += (len - rem);
/* ---------------------------------------*/
/*STEP 9 : process the return code */
/* DON"T NEED THIS IF USING CALLOUTS */
/* PICK ONE OR THE OTHER */
/* -------------------------------------- */
switch(status) {
case NMEAP_GPGGA:
printf("-------------switch\n");
print_gga(&gga);
printf("-------------\n");
break;
case NMEAP_GPRMC:
printf("-------------switch\n");
print_rmc(&rmc);
printf("-------------\n");
break;
default:
break;
}
}
}
/* close the serial port */
close(fd);
return 0;
}
#endif