gps.py can avoid ascii errors on parsing i2c byte data
This commit is contained in:
+26
-42
@@ -1,64 +1,42 @@
|
|||||||
#!/bin/python
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# http://stackoverflow.com/questions/28867795/reading-i2c-data-from-gps/31010266
|
# http://stackoverflow.com/questions/28867795/reading-i2c-data-from-gps/31010266
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import smbus
|
import smbus
|
||||||
import logging
|
import logging
|
||||||
|
import pynmea2
|
||||||
|
|
||||||
BUS = None
|
BUS = None
|
||||||
address = 0x42
|
address = 0x42
|
||||||
gpsReadInterval = 0.1
|
gpsReadInterval = 1
|
||||||
LOG = logging.getLogger()
|
LOG = logging.getLogger()
|
||||||
|
|
||||||
# GUIDE
|
|
||||||
# http://ava.upuaut.net/?p=768
|
|
||||||
|
|
||||||
GPSDAT = {
|
|
||||||
'strType': None,
|
|
||||||
'fixTime': None,
|
|
||||||
'lat': None,
|
|
||||||
'latDir': None,
|
|
||||||
'lon': None,
|
|
||||||
'lonDir': None,
|
|
||||||
'fixQual': None,
|
|
||||||
'numSat': None,
|
|
||||||
'horDil': None,
|
|
||||||
'alt': None,
|
|
||||||
'altUnit': None,
|
|
||||||
'galt': None,
|
|
||||||
'galtUnit': None,
|
|
||||||
'DPGS_updt': None,
|
|
||||||
'DPGS_ID': None
|
|
||||||
}
|
|
||||||
|
|
||||||
def connectBus():
|
def connectBus():
|
||||||
global BUS
|
global BUS
|
||||||
BUS = smbus.SMBus(1)
|
BUS = smbus.SMBus(1)
|
||||||
|
|
||||||
def parseResponse(gpsLine):
|
def parseResponse(gpsBytes):
|
||||||
global lastLocation
|
global lastLocation
|
||||||
gpsChars = ''.join(chr(c) for c in gpsLine)
|
#gpsLine = "".join(map(chr, gpsBytes))
|
||||||
if "*" not in gpsChars:
|
#gpsLine = "".join([chr(byte) for byte in gpsBytes])
|
||||||
|
gpsLine = "".join([chr(byte) for byte in gpsBytes])
|
||||||
|
|
||||||
|
|
||||||
|
if "*" not in gpsLine:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
gpsStr, chkSum = gpsChars.split('*')
|
print(gpsLine)
|
||||||
gpsComponents = gpsStr.split(',')
|
gpsObject = pynmea2.parse(gpsLine)
|
||||||
gpsStart = gpsComponents[0]
|
print(gpsObject)
|
||||||
if (gpsStart == "$GNGGA"):
|
|
||||||
chkVal = 0
|
# logs
|
||||||
for ch in gpsStr[1:]: # Remove the $
|
lt = time.asctime( time.localtime(time.time()) )
|
||||||
chkVal ^= ord(ch)
|
with open('/home/alarm/gps.log', 'a') as f:
|
||||||
if (chkVal == int(chkSum, 16)):
|
f.write(lt+' : '+gpsLine+'\n')
|
||||||
for i, k in enumerate(
|
|
||||||
['strType', 'fixTime',
|
|
||||||
'lat', 'latDir', 'lon', 'lonDir',
|
|
||||||
'fixQual', 'numSat', 'horDil',
|
|
||||||
'alt', 'altUnit', 'galt', 'galtUnit',
|
|
||||||
'DPGS_updt', 'DPGS_ID']):
|
|
||||||
GPSDAT[k] = gpsComponents[i]
|
|
||||||
print(gpsChars)
|
|
||||||
print(json.dumps(GPSDAT, indent=2))
|
|
||||||
|
|
||||||
def readGPS():
|
def readGPS():
|
||||||
c = None
|
c = None
|
||||||
@@ -71,12 +49,17 @@ def readGPS():
|
|||||||
elif c == 10:
|
elif c == 10:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
#print(c)
|
||||||
response.append(c)
|
response.append(c)
|
||||||
|
|
||||||
parseResponse(response)
|
parseResponse(response)
|
||||||
|
|
||||||
except IOError:
|
except IOError:
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
connectBus()
|
connectBus()
|
||||||
|
|
||||||
except (Exception) as e:
|
except (Exception) as e:
|
||||||
|
print("Exception")
|
||||||
print(e)
|
print(e)
|
||||||
LOG.error(e)
|
LOG.error(e)
|
||||||
|
|
||||||
@@ -84,3 +67,4 @@ connectBus()
|
|||||||
while True:
|
while True:
|
||||||
readGPS()
|
readGPS()
|
||||||
time.sleep(gpsReadInterval)
|
time.sleep(gpsReadInterval)
|
||||||
|
#readGPS()
|
||||||
|
|||||||
Reference in New Issue
Block a user