/* Last modified on Sat Jan 8 2011, Hal Murray */ /* * This is a hack to grab sentences from NMEA GPS receivers and * print them out with the time the final CR/NL arrived. */ char *default_tty = "/dev/ttyUSB7"; #include #include #include #include #include #include #include #include /* prototypes */ int SetupTTY(char *tty, int baud); int fd = -1; int SetupTTY(char *tty, int baud) { int err, i; struct termios mode; int baud_code; char buffer[100]; printf("Listening to %s at %d baud\n", tty, baud); switch (baud) { case 4800: baud_code = B4800; break; case 19200: baud_code = B19200; break; default: printf("# Unrecognized baud rate: %d\n", baud); sleep(10); return(1); } /* need NONBLOCK to avoid hanging if LOCAL flag isn't set */ /* At least on real TTYs. */ fd = open(tty, O_RDONLY | O_NONBLOCK); if (fd < 0) { printf("# Can't open %s: %s\n", tty, strerror(errno)); sleep(10); return(1); } /* Setup all the mode bits. Check with: stty -a -F /dev/ttyUSB1 */ err = tcgetattr(fd, &mode); if (err < 0) { printf("# tcsetattr failed: %s\n", strerror(errno)); sleep(10); return(1); } mode.c_iflag = IGNBRK | IGNPAR | IGNCR; mode.c_oflag = 0; mode.c_cflag = CS8 | CLOCAL | CREAD; mode.c_lflag = ICANON; /* Line-at-a-time mode */ for (i=0; itm_hour, nowtm->tm_min, nowtm->tm_sec, (int)now.tv_usec/1000, buffer); } }