-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtestgps.c
107 lines (99 loc) · 2.42 KB
/
testgps.c
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
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <sys/types.h>
#include "tinygps.h"
#define BAUDRATE B4800
#define FALSE 0
#define TRUE 1
//
volatile int STOP=FALSE;
void signal_handler_IO (int status);
int wait_flag=TRUE;
char devicename[80] = "/dev/ttyUSB0", ch;
int status;
int newdata = 0;
//
int main(int argc, char *argv[])
{
int fd, res, i;
struct termios newtio;
struct sigaction saio;
char buf[255];
//
//open the device in non-blocking way (read will return immediately)
fd = open(devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
{
perror(devicename);
exit(1);
}
//
//install the serial handler before making the device asynchronous
saio.sa_handler = signal_handler_IO;
sigemptyset(&saio.sa_mask); //saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
//
// allow the process to receive SIGIO
fcntl(fd, F_SETOWN, getpid());
//
// make the file descriptor asynchronous
fcntl(fd, F_SETFL, FASYNC);
//
// set new port settings for canonical input processing
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
//
// loop while waiting for input. normally we would do something useful here
while (STOP == FALSE)
{
//
// after receiving SIGIO, wait_flag = FALSE, input is available and can be read */
if (wait_flag == FALSE) //if input is available
{
res = read(fd,buf,255);
if (res > 0)
{
for (i=0; i < res; i++) //for all chars in string
{
//printf("%c", buf[i]);
if(gps_encode(buf[i]))
newdata = 1;
}
if (newdata)
{
float flat, flon;
unsigned long age;
gps_f_get_position(&flat, &flon, &age);
printf("LAT= %f LON= %f SAT=%d PREC=%d \n",
flat, flon, gps_satellites(), gps_hdop());
newdata = 0;
}
}
wait_flag = TRUE; /* wait for new input */
}
}
close(fd);
}
//
/***************************************************************************
* signal handler. sets wait_flag to FALSE, to indicate above loop that *
* characters have been received. *
***************************************************************************/
//
void signal_handler_IO (int status)
{
//printf("received SIGIO signal.\n");
wait_flag = FALSE;
}