forked from lmandres/GT06ScannerClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpsScanner.py
196 lines (142 loc) · 5.85 KB
/
gpsScanner.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import _thread
import math
import re
import time
import serial
class GPSScanner():
serialPort = None
serialBaud = None
gpsSerial = None
locationHash = None
runLocationThread = None
def __init__(self, serialPortIn=None, serialBaudIn=None):
if serialPortIn:
self.serialPort = serialPortIn
if serialBaudIn:
self.serialBaud = serialBaudIn
def resetConnection(self):
try:
self.gpsSerial.close()
self.gpsSerial = serial.Serial(
self.serialPort,
self.serialBaud
)
except:
pass
def getSerialBaud(self):
return self.serialBaud
def setSerialBaud(self, serialBaudIn):
self.serialBaud = serialBaudIn
def getSerialPort(self):
return self.serialPort
def setSerialPort(self, serialPortIn):
self.serialPort = serialPortIn
def getLocationHash(self):
return self.locationHash
def runLocate(self):
def doThreadLoop():
self.locationHash = self.doDecodeGPS()
while self.runLocationThread:
self.locationHash.update(self.doDecodeGPS())
self.locationHash = {}
self.runLocationThread = True
self.gpsSerial = serial.Serial(self.serialPort, self.serialBaud)
_thread.start_new_thread(doThreadLoop, ())
def stopLocate(self):
self.runLocationThread = False
if self.gpsSerial is not None:
self.gpsSerial.close()
def doDecodeGPS(self):
def doGPSCheckSum(stringIn):
checkSum = 0
for charIndex in stringIn:
checkSum ^= ord(charIndex)
return str(hex(checkSum)).upper()[2:]
returnHash = {}
match = None
while True:
serialLine = b""
try:
serialLine = self.gpsSerial.readline()
except:
self.resetConnection()
match = re.match(
b'^\$(GPGGA|GPRMC|GPGLL|GPGSV),(.*)\*(.*)',
serialLine
)
if match:
checkStr = "{},{}".format(
match.group(1).decode("utf-8"),
match.group(2).decode("utf-8")
)
funcSum = doGPSCheckSum(checkStr)
gpsSum = str(match.group(3).decode("utf-8"))[:2]
if funcSum == gpsSum:
break
if match.group(1) == b'GPRMC':
returnHash = self.decodeGPRMC(match.group(2).decode('utf-8'))
elif match.group(1) == b'GPGGA':
returnHash = self.decodeGPGGA(match.group(2).decode('utf-8'))
elif match.group(1) == b'GPGLL':
returnHash = self.decodeGPGLL(match.group(2).decode('utf-8'))
elif match.group(1) == b'GPGSV':
returnHash = self.decodeGPGSV(match.group(2).decode('utf-8'))
return returnHash
def decodeGPGLL(self, stringIn):
returnHash = {}
gpsArray = stringIn.split(',')
try:
returnHash['latitude'] = ((float(gpsArray[0]) - math.floor(float(gpsArray[0]) / 100) * 100) / 60) + math.floor(float(gpsArray[0]) / 100)
returnHash['latitudeDir'] = gpsArray[1]
returnHash['longitude'] = ((float(gpsArray[2]) - math.floor(float(gpsArray[2]) / 100) * 100) / 60) + math.floor(float(gpsArray[2]) / 100)
returnHash['longitudeDir'] = gpsArray[3]
returnHash['time'] = str(gpsArray[4][0:2]) + ':' + str(gpsArray[4][2:4]) + ':' + str(gpsArray[4][4:6])
returnHash['timestamp'] = gpsArray[4]
except IndexError:
pass
except ValuerError:
pass
return returnHash
def decodeGPGGA(self, stringIn):
returnHash = {}
gpsArray = stringIn.split(',')
try:
returnHash['latitude'] = ((float(gpsArray[1]) - math.floor(float(gpsArray[1]) / 100) * 100) / 60) + math.floor(float(gpsArray[1]) / 100)
returnHash['latitudeDir'] = gpsArray[2]
returnHash['longitude'] = ((float(gpsArray[3]) - math.floor(float(gpsArray[3]) / 100) * 100) / 60) + math.floor(float(gpsArray[3]) / 100)
returnHash['longitudeDir'] = gpsArray[4]
returnHash['altitude'] = float(gpsArray[8])
returnHash['altitudeUnits'] = gpsArray[9]
returnHash['time'] = str(gpsArray[0][0:2]) + ':' + str(gpsArray[0][2:4]) + ':' + str(gpsArray[0][4:6])
returnHash['timestamp'] = gpsArray[0]
except IndexError:
pass
except ValueError:
pass
return returnHash
def decodeGPRMC(self, stringIn):
returnHash = {}
gpsArray = stringIn.split(',')
try:
returnHash['latitude'] = ((float(gpsArray[2]) - math.floor(float(gpsArray[2]) / 100) * 100) / 60) + math.floor(float(gpsArray[2]) / 100)
returnHash['latitudeDir'] = gpsArray[3]
returnHash['longitude'] = ((float(gpsArray[4]) - math.floor(float(gpsArray[4]) / 100) * 100) / 60) + math.floor(float(gpsArray[4]) / 100)
returnHash['longitudeDir'] = gpsArray[5]
returnHash['groundSpeedKnots'] = float(gpsArray[6])
returnHash['course'] = float(gpsArray[7])
returnHash['date'] = str(int(gpsArray[8][4:6])) + '-' + str(gpsArray[8][2:4]) + '-' + str(gpsArray[8][0:2])
returnHash['time'] = str(gpsArray[0][0:2]) + ':' + str(gpsArray[0][2:4]) + ':' + str(gpsArray[0][4:6])
returnHash['timestamp'] = gpsArray[0]
except IndexError:
pass
except ValueError:
pass
return returnHash
def decodeGPGSV(self, stringIn):
returnHash = {}
gpsArray = stringIn.split(',')
try:
returnHash["satellitesInView"] = gpsArray[2]
except IndexError:
pass
return returnHash