forked from heldersepu/GMapCatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialGPS.py
277 lines (262 loc) · 9.03 KB
/
serialGPS.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/python
# -*- encoding: utf-8 -*-
available = True
try:
import serial
except:
available = False
from threading import Thread, Event
from math import floor
import platform
from mapConst import MODE_NO_FIX
BAUDRATES = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]
## Scans available serial ports
## (COMx on Windows, tty[AMA, S, USB], rfcomm on Linux)
def serialPortScan():
availableSerialPorts = ['none']
if platform.system() == 'Windows':
for i in range(256):
try:
COMport = serial.Serial(i)
availableSerialPorts.append(COMport.portstr)
COMport.close()
except:
pass
elif platform.system() == 'Linux':
import os
typicalPorts = ['ttyAMA', 'ttyS', 'ttyUSB', 'rfcomm']
devs = os.listdir('/dev')
for port in typicalPorts:
for dev in devs:
if dev.startswith(port):
availableSerialPorts.append('/dev/%s' % dev)
return availableSerialPorts
## GPSd-compatible gpsfix
class gpsfix:
def __init__(self):
self.mode = MODE_NO_FIX
self.time = None
self.ept = None
self.latitude = self.longitude = None
self.epx = None
self.epy = None
self.altitude = None # Meters
self.epv = None
self.track = None # Degrees from true north
self.speed = None # Knots
self.climb = None # Meters per second
self.epd = None
self.eps = None
self.epc = None
self.satellites = 0 # Not in GPSd
## Serial port GPS -module for mapGPS.py
class SerialGPS(Thread):
def __init__(self, port='/dev/ttyS0', baudrate=9600, timeout=3):
Thread.__init__(self)
self.port = port
self.baudrate = baudrate
self.timeout = timeout
self.ser = None
self.buf = ''
self.dataToProcess = list()
self.fix = gpsfix()
self.__stop = Event()
self.available = False
try:
self.ser = serial.Serial(
port=self.port,
baudrate=self.baudrate,
timeout=self.timeout)
self.ser.flushInput()
self.available = True
except serial.SerialException:
self.__stop.set()
self.ser = None
self.fix.mode = MODE_NO_FIX
self.available = False
print "Unable to open port"
def run(self):
try:
# read buffer while stop is called
while not self.__stop.isSet():
# if the serial port connection exists...
if self.ser:
try:
# read 40 characters from port to buffer
self.buf += self.ser.read(40)
except TypeError:
pass
# if the buffer includes row change
if '\n' in self.buf:
# split the buffer by lines
lines = self.buf.split('\n')
# and for the lines
# (except the last one, that is probably not complete)
for line in lines[0:-1]:
# handle data
self.dataHandler(line)
# set the buffer to only include the last line
self.buf = lines[-1]
else:
self.__stop.set()
self.fix.mode == MODE_NO_FIX
self.available = False
self.stop()
except serial.SerialException:
self.fix.mode = MODE_NO_FIX
self.available = False
print "Unable to open port"
self.stop()
def stop(self):
self.__stop.set()
if self.ser:
self.ser.close()
## Data handler for NMEA-data
# currently handles $GPRMC, $GPGGA and $GPGSA
def dataHandler(self, line):
# print line # for debug
data = line.strip().split(',')
# $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
# 1: Fix taken time, UTC
# 2: Status A=active or V=Void
# 3: Latitude
# 4: Latitude hemisphere
# 5: Longitude
# 6: Longitude hemisphere
# 7: Speed over the ground in knots
# 8: Track angle in degrees True
# 9: Date DDMMYY
# 10-11: Magnetic Variation
if data[0] == '$GPRMC':
try:
self.fix.time = data[1]
except:
pass
try:
if data[2] == 'V': # If data is VOID, report as NO_FIX
self.fix.mode = MODE_NO_FIX
except:
pass
try:
hemisphere = data[4]
except:
hemisphere = None
# if on the southern hemisphere, latitude is negative
if hemisphere == 'S':
try:
self.fix.latitude = -self.convertDegrees(float(data[3]))
except:
pass
elif hemisphere:
try:
self.fix.latitude = self.convertDegrees(float(data[3]))
except:
pass
try:
hemisphere = data[6]
except:
hemisphere = None
# if on the western hemisphere, longitude is negative
if hemisphere == 'W':
try:
self.fix.longitude = -self.convertDegrees(float(data[5]))
except:
pass
elif hemisphere:
try:
self.fix.longitude = self.convertDegrees(float(data[5]))
except:
pass
try:
self.fix.speed = float(data[7])
except:
pass
try:
self.fix.track = float(data[8])
except:
pass
# $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
# 1: Fix taken time, UTC
# 2: Latitude
# 3: Latitude hemisphere
# 4: Longitude
# 5: Longitude hemisphere
# 6: Fix quality: 0 = invalid, 1 = GPS fix (SPS), 2 = DGPS fix
# 7: Number of satellites being tracked
# 8: Horizontal dilution of position
# 9-10: Altitude, Meters, above mean sea level
# 11-12: Height of geoid (mean sea level) above WGS84 ellipsoid
# 13: time in seconds since last DGPS update
# 14: DGPS station ID number
elif data[0] == '$GPGGA':
try:
self.fix.time = data[1]
except:
pass
try:
hemisphere = data[3]
except:
hemisphere = None
# if on the southern hemisphere, latitude is negative
if hemisphere == 'S':
try:
self.fix.latitude = -self.convertDegrees(float(data[2]))
except:
pass
elif hemisphere:
try:
self.fix.latitude = self.convertDegrees(float(data[2]))
except:
pass
try:
hemisphere = data[5]
except:
hemisphere = None
# if on the western hemisphere, longitude is negative
if hemisphere == 'W':
try:
self.fix.longitude = -self.convertDegrees(float(data[4]))
except:
pass
elif hemisphere:
try:
self.fix.longitude = self.convertDegrees(float(data[4]))
except:
pass
try:
if int(data[6]) == 0:
self.fix.mode = MODE_NO_FIX
except:
pass
try:
self.fix.satellites = int(data[7])
except:
pass
try:
self.fix.altitude = float(data[9])
except:
pass
# $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
# 1: Auto selection of 2D or 3D fix (M = manual)
# 2: 3D fix - values include: 1 = no fix, 2 = 2D fix, 3 = 3D fix
# 3-14: PRNs of satellites used for fix (space for 12)
# 15: PDOP (dilution of precision)
# 16: Horizontal dilution of precision (HDOP)
# 17: Vertical dilution of precision (VDOP)
# 18: the checksum data, always begins with *
elif data[0] == '$GPGSA':
try:
self.fix.mode = int(data[2])
except:
pass
def convertDegrees(self, degrees):
return float(
int(floor(degrees / 100)) +
(degrees / 100 - int(floor(degrees / 100))) / 60 * 100)
if __name__ == '__main__':
import time
print serialPortScan()
gps = SerialGPS('/dev/ttyUSB0', 4800)
gps.start()
time.sleep(10)
gps.stop()