3 # This file is Copyright (c) 2010 by the GPSD project
4 # BSD terms apply: see the file COPYING in the distribution root for details.
6 # Display GPS output. Hexify it if necessary.
8 import os, sys, termios, socket, select, getopt, curses.ascii
9 import gps.packet as sniffer
11 # The spec says 82, but some receivers (TN-200, GSW 2.3.2) output 86 characters
14 # Lowest debug level at which packet getter begins to emit messages, minus one
15 BASELEVEL = sniffer.LOG_IO
21 for (i, ch) in enumerate(str):
22 if curses.ascii.isprint(ord(ch)) or curses.ascii.isspace(ord(ch)):
25 dmp += "\\x%02x" % ord(ch)
30 def reporter(errlevel, msg):
31 if errlevel <= debuglevel:
34 if __name__ == '__main__':
38 (options, arguments) = getopt.getopt(sys.argv[1:], "hps:tD:V")
39 except getopt.GetoptError, msg:
40 print "gpscat: " + str(msg)
48 for (switch, val) in options:
52 if val[-2] in ('N', 'E', 'O'):
54 stopbits = int(val[-1])
61 debuglevel = BASELEVEL + int(val)
63 sys.stderr.write("usage: gpscat [-s speed] serial-port\n")
66 if "rfcomm" in arguments[0]: # Bluetooth special case
67 s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
68 s.connect((arguments[0], 1))
70 else: # Ordinary device
71 tty = os.open(arguments[0], os.O_RDWR)
74 (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(tty)
76 ispeed = ospeed = eval("termios.B%d" % speed)
77 except AttributeError:
78 sys.stderr.write("gpscat: unknown baud rate %d\n" % speed)
81 cflag &= ~termios.CSIZE
82 cflag |= (termios.CS8, termios.CS7)[stopbits-1]
85 iflag &= ~termios.PARENB
86 iflag &= ~termios.INPCK
88 iflag |= termios.INPCK
89 cflag |= termios.PARENB
90 cflag |= termios.PARODD
92 iflag |= termios.INPCK
93 cflag |= termios.PARENB
94 cflag &= ~termios.PARODD
95 termios.tcsetattr(tty, termios.TCSANOW,
96 [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
98 poller = select.poll()
99 poller.register(tty, select.POLLIN)
103 getter = sniffer.new()
104 sniffer.register_report(reporter)
107 (fd, event) = poller.poll()[0]
108 if fd == tty and event == select.POLLIN:
110 buf += os.read(tty, NMEA_MAX)
111 sys.stdout.write(hexdump(buf))
114 (length, ptype, packet) = getter.get(tty)
119 sys.stdout.write(`ptype` + " (" + `length` + "): " + hexdump(packet))
120 sys.stdout.write("\n")
122 sys.stdout.write(hexdump(packet) + "\n")
123 except KeyboardInterrupt:
125 sys.stdout.write("\n")