-
Notifications
You must be signed in to change notification settings - Fork 12
/
ejemplo27.py
executable file
·51 lines (44 loc) · 1.37 KB
/
ejemplo27.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import serial, time
from pynmea import nmea
sport='/dev/ttyUSB0'
sbaud=4800
stimeout = 1 # timeout (in seconds) so that the port doesn't hang
ser=0
verbose=1
def init_serial():
global ser
ser = serial.Serial()
ser.baudrate = sbaud
ser.port = sport
ser.timeout = stimeout
ser.open()
print 'Serial port: ' + ser.portstr
#### Main Thread
init_serial()
while 1:
try:
data = ser.readline() ## reads in bytes followed by a newline
print 'NMEA Phrase: ' + data
if data[0:6] == '$GPGGA':
gpgga = nmea.GPGGA()
gpgga.parse(data)
if verbose: print 'GPS NMEA: ' + data ## print to the console
print 'GPS timestamp: ' + gpgga.timestamp
print 'GPS sats,qual: ' + gpgga.num_sats + ',' + gpgga.gps_qual
print 'GPS lon,lat: ' + gpgga.longitude + ',' + gpgga.latitude
if data[0:6] == '$GPRMC':
gprmc = nmea.GPRMC()
gprmc.parse(data)
print 'GPS timestamp: ' + gprmc.timestamp
except serial.SerialException as e: ## There is no new data from serial port
pass
except TypeError as e: ## Disconnect of USB->UART occured
ser.port.close()
break
except KeyboardInterrupt as e: ## Disconnect of USB->UART occured
ser.port.close()
break
else: ## Some data was received
pass