我有一个通过I2C连接到我的Raspberry Pi的公用箱M8 GPS,我正试图从中提取数据。
我使用python试图通过与GPS模块的I2C连接获取数据,但每次请求我都会得到一个递增的数字。
我不确定我做错了什么。
i2cdetect-y 1返回:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- 42 -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Python代码
import smbus
import time
bus = smbus.SMBus(1)
address = 0x42
bus.read_byte_data(address,0x00)
bus.read_byte(address)
退货:
160
161
我也在试验这个。
- 使用此页面中有关如何使用smbus的一些信息
- 从这里使用arduino代码(注意下面没有使用0xfd和0xff字节)
我可以得到下面的GNGGA字符串($GNGGA意味着美国和俄罗斯的GPS系统都在使用):
import time
import json
import smbus
import logging
BUS = None
address = 0x42
gpsReadInterval = 0.1
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():
global BUS
BUS = smbus.SMBus(1)
def parseResponse(gpsLine):
global lastLocation
gpsChars = ''.join(chr(c) for c in gpsLine)
if "*" not in gpsChars:
return False
gpsStr, chkSum = gpsChars.split('*')
gpsComponents = gpsStr.split(',')
gpsStart = gpsComponents[0]
if (gpsStart == "$GNGGA"):
chkVal = 0
for ch in gpsStr[1:]: # Remove the $
chkVal ^= ord(ch)
if (chkVal == int(chkSum, 16)):
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():
c = None
response = []
try:
while True: # Newline, or bad char.
c = BUS.read_byte(address)
if c == 255:
return False
elif c == 10:
break
else:
response.append(c)
parseResponse(response)
except IOError:
time.sleep(0.5)
connectBus()
except Exception, e:
print e
LOG.error(e)
connectBus()
while True:
readGPS()
time.sleep(gpsReadInterval)
以下是一些输出。
$GNGGA,,,,,,0,00,99.99,,,,,,*56
{
"galt": "",
"DPGS_updt": "",
"lon": "",
"strType": "$GNGGA",
"lat": "",
"alt": "",
"fixTime": "",
"lonDir": "",
"numSat": "00",
"fixQual": "0",
"altUnit": "",
"galtUnit": "",
"horDil": "99.99",
"latDir": "",
"DPGS_ID": ""
}
我把它放在远离窗户的室内等。所以预计数据会很糟糕。希望这能有所帮助。