-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPS.py
52 lines (41 loc) · 1.34 KB
/
GPS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pyserial library is required for working
import serial
mport = 'COM9' #choose your com port on which you connected your neo 6m GPS
#mport = "/dev/ttyAMA0" #for Raspberry Pi pins
#mport = "/dev/ttyUSB0" #for Raspberry Pi USB
def parseGPS(data):
if data[0:6] == "$GPGGA":
s = data.split(",")
if s[7] == '0' or s[7]=='00':
print ("no satellite data available")
return
time = s[1][0:2] + ":" + s[1][2:4] + ":" + s[1][4:6]
#print("-----------")
lat = decode(s[2])
lon = decode(s[4])
return lat,lon
def decode(coord):
l = list(coord)
for i in range(0,len(l)-1):
if l[i] == "." :
break
base = l[0:i-2]
degi = l[i-2:i]
degd = l[i+1:]
#print(base," ",degi," ",degd)
baseint = int("".join(base))
degiint = int("".join(degi))
degdint = float("".join(degd))
degdint = degdint / (10**len(degd))
degs = degiint + degdint
full = float(baseint) + (degs/60)
#print(full)
return full
ser = serial.Serial(mport,9600,timeout = 2)
while True:
try:
dat = ser.readline().decode()
mylat,mylon = parseGPS(dat)
print(mylat , " " , mylon)
except:
print("error")