-
Notifications
You must be signed in to change notification settings - Fork 1
/
NMEA-GPS_Serial.py
89 lines (70 loc) · 2.19 KB
/
NMEA-GPS_Serial.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import serial
import time
import requests
import pynmea2
def GPS_RMC(msg):
if msg.status == "A":
send("RMCFix", "1")
else:
send("RMCFix", "0")
def GPS_GSA(msg):
# print(repr(msg))
# print(dir(msg))
send('GSA3d', msg.mode_fix_type)
def GPS_GSV(msg):
# print(repr(msg))
send('GSView', msg.num_sv_in_view)
def GPS_GGA(msg):
# print(repr(msg))
send('GGA', msg.num_sats)
dispatch = {
'GGA' : GPS_GGA,
# b'$PRWIZC' : do_sink,
'GSA' : GPS_GSA,
'GSV' : GPS_GSV,
'RMC' : GPS_RMC,
}
def send(datum, value):
payload = "{0!s} value={1!s}".format(datum, value)
try:
r = requests.post("http://192.168.57.103:8086/write?db=GPSRadio0", data=payload)
except requests.exceptions.ConnectionError:
print("Failed to Connect to InfluxDB")
def main(argv):
while(True):
read = b'...'
while(read is not b''):
time.sleep(5)
try:
with serial.serial_for_url('rfc2217://192.168.57.250:4098', baudrate=4800, timeout=2) as ser:
while(read is not b''):
read = ser.readline().strip()
# print(read)
if read.startswith(b'$') and not read.startswith(b'$PRWIZCH'):
try:
msg = pynmea2.parse(read.decode())
except UnicodeDecodeError:
pass
# print(dir(msg))
# print(msg.sentence_type)
try:
dispatch[msg.sentence_type](msg)
except KeyError:
pass
except pynmea2.nmea.ChecksumError:
print("Checksum Error")
except serial.serialutil.SerialException:
print("Connection Failed")
'''
[program:GPS_Serial]
command= python3 NMEA-GPS_Serial.py
user=base
'''
if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
print('Received Ctrl-c')