#! /usr/bin/python #--------------------------------- # Test of serial port # # O.M., 05 July 2008 #--------------------------------- import serial from time import gmtime, strftime COM_PORT = 'COM4' print ''' ================================================= Test of GPS with USB output. Use TeraTerm to veryfy what COM port is used, and change the constant "COM_PORT" if necessary. CTRL-C to break. UiB, Dept. of Earth Science 05 July 2008 ================================================= ''' counter = 0 try: Interval = int(raw_input('Logging interval [seconds]: ')) except: print 'Input not valid' exit(0) FileName = "c:/GPS/" + strftime("GPS-data-%Y-%j-%H%M%S.txt") print "Logging data to", FileName f = open(FileName, "w") print #-- Open serial port try: ser = serial.Serial(port=COM_PORT, baudrate=4800, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=None ) except: print "Cannot open", COM_PORT, '- use TeraTerm to veryfy what COM port is used by the GPS.' exit(0) counter = Interval #---------- M A I N L O O P ------------- while True: # Runs forever line = ser.readline() if '$GPRMC' in line: s = line.split(',') #---- Split this line into array (use ',' as field delimiter) if counter == Interval: print line, try: print ' ', s[9], s[1].split('.')[0], s[3], s[4], s[5], s[6] except: pass f.write(line) #--- Write to file f.flush() counter = 0 counter += 1 ser.close() exit(0)