#! /usr/bin/env python # $Id: gps.py 23 2004-07-22 04:35:36Z peter $ import os import sys import time import csv from pygarmin import garmin import util GPSDevice = '/dev/ttyS0' GPSLogName = 'gps.log' GPSCollectRate = 3 # number of seconds between snapshots def connect( device ): phys = garmin.UnixSerialLink( device ) gps = garmin.Garmin( phys ) gps.pvtLink.dataOn( ) return gps if __name__ == '__main__': while 1: try: gps = connect( GPSDevice ) while 1: start_time = time.time( ) position = gps.pvtLink.getData( ) fullname = util.log_path( str( position.wn_days ), GPSLogName ) writer = csv.writer( file( fullname, 'a' ) ) writer.writerow( ( start_time, position.alt, position.east, position.epe, position.eph, position.epv, position.fix, position.leap_secs, position.msl_height, position.north, position.rlat, position.rlon, position.tow, position.up, position.wn_days ) ) end_time = time.time( ) time_difference = end_time - start_time if time_difference < GPSCollectRate: time.sleep( GPSCollectRate - time_difference ) except garmin.LinkException, e: print 'received a LinkException:', e print 'trying to reestablish the GPS connection' time.sleep( GPSCollectRate )