【问题标题】:Reading I2C data from GPS从 GPS 读取 I2C 数据
【发布时间】:2015-03-05 00:23:16
【问题描述】:

我有一个 ublox M8 GPS 通过 I2C 连接到我的 Raspberry Pi,我正在尝试从中提取数据。

我正在使用 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

【问题讨论】:

    标签: gps raspberry-pi i2c


    【解决方案1】:

    我也在尝试这个。

    1. 使用来自 page 的一些关于如何使用 smbus 的信息。
    2. 并使用来自here 的 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": ""
    }
    

    我把它放在室内,远离窗户等。预计数据会很差。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      相关资源
      最近更新 更多