3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
7 GPSTest is a simple example using the SerialPort transport and the NMEA 0183
8 and Rockwell Zodiac GPS protocols to display fix data as it is received from
11 from twisted.python import log, usage
14 if sys.platform == 'win32':
15 from twisted.internet import win32eventreactor
16 win32eventreactor.install()
20 def handle_fix(self, *args):
22 handle_fix gets called whenever either rockwell.Zodiac or nmea.NMEAReceiver
23 receives and decodes fix data. Generally, GPS receivers will report a
24 fix at 1hz. Implementing only this method is sufficient for most purposes
25 unless tracking of ground speed, course, utc date, or detailed satellite
26 information is necessary.
28 For example, plotting a map from MapQuest or a similar service only
29 requires longitude and latitude.
32 '\n'.join(map(lambda n: ' %s = %s' % tuple(n), zip(('utc', 'lon', 'lat', 'fix', 'sat', 'hdp', 'alt', 'geo', 'dgp'), map(repr, args)))))
34 class GPSOptions(usage.Options):
36 ['zodiac', 'z', 'Use Rockwell Zodiac (DeLorme Earthmate) [default: NMEA 0183]'],
39 ['outfile', 'o', None, 'Logfile [default: sys.stdout]'],
40 ['baudrate', 'b', None, 'Serial baudrate [default: 4800 for NMEA, 9600 for Zodiac]'],
41 ['port', 'p', '/dev/ttyS0', 'Serial Port device'],
45 if __name__ == '__main__':
46 from twisted.internet import reactor
47 from twisted.internet.serialport import SerialPort
52 except usage.UsageError, errortext:
53 print '%s: %s' % (sys.argv[0], errortext)
54 print '%s: Try --help for usage details.' % (sys.argv[0])
57 logFile = o.opts['outfile']
60 log.startLogging(logFile)
63 from twisted.protocols.gps.rockwell import Zodiac as GPSProtocolBase
66 from twisted.protocols.gps.nmea import NMEAReceiver as GPSProtocolBase
68 class GPSTest(GPSProtocolBase, GPSFixLogger):
71 if o.opts['baudrate']:
72 baudrate = int(o.opts['baudrate'])
76 log.msg('Attempting to open %s at %dbps as a %s device' % (port, baudrate, GPSProtocolBase.__name__))
77 s = SerialPort(GPSTest(), o.opts['port'], reactor, baudrate=baudrate)