1.5.2. fejezet, GPS információk kiolvasása

Ebben a mintaprogramban egy PDA-ba szerelt GPS-ből olvasom ki az NMEA szabványnak megfelelő adatokat, és ezekből nyerem ki a koordinátákat. Soros portot nyitok az eszközhöz, és SQLite adatbázisba mentem az adatokat.

import serial
from time import localtime,strftime
 
latin2_decoder = lambda s: str(s) #unicode(s, "iso8859-2", "replace")
 
def latconv(lat):
  plat=lat.split('.');
  latdegree=int(plat[0]);
  latplusmin=float(plat[1])/60;
  return (latdegree+latplusmin);
 
def lonconv(lon):
  plong=lon.split('.');
  longdegree=int(plong[0]);
  longplusmin=float(plong[1])/60;
  return (longdegree+longplusmin);
 
 
def gprmc_test(row_ary):
    return (row_ary[0] == "$GPRMC") and (row_ary[1]!="") and (row_ary[3]!="") and (row_ary[5]!="")
 
def get_pos(row_ary):
    return {'rtime':float(row_ary[1]),'lon':lonconv(row_ary[3]),'lat':latconv(row_ary[5])}
 
def rec_pos(data):
    cur.execute("insert into coords_rec (rectime,long,lat) values (%f,%f,%f)"%(data['rtime'],data['lon'],data['lat']))
 
 
from sqlite3 import dbapi2 as sqlite
con=sqlite.connect("cdata.db",isolation_level=None)
con.text_factory=latin2_decoder
cur=con.cursor()
 
#cur.execute("create table coords_rec (rectime float,long float,lat float)")
 
s=serial.Serial(port=40,baudrate=9600,timeout=3)
s.open()
of=open('tmp.log',"w")
rv=s.readline()
cnt=0
while cnt<=100000:
  if rv!=None:
    gps_ary=rv.split(',')
    if (gprmc_test(gps_ary)):
      data=get_pos(gps_ary)
      rec_pos(data)
      of.write("(%f,%f,%f)\r\n"%(data['rtime'],data['lon'],data['lat']));
      cnt+=1
    print gps_ary
  else:
    print "row is null"
  rv=s.readline()
of.close() 
s.close() 
con.close()