0

我正在尝试使用此代码将我的 gps 数据流式传输到谷歌地球。我已经尝试过python 3.4的版本,但是我出错了,有人说gps3对python3真的不起作用,所以我尝试使用python2

用于在 Google 地球中显示 GPS 流的 python 2.7 代码 #!/usr/bin/python

# Copyright (C) 2007 by Jaroslaw Zachwieja <grok!warwick.ac.uk>
# Copyright (C) 2008 by TJ <linux!tjworld.net>
# Published under the terms of GNU General Public License v2 or later.
# License text available at http://www.gnu.org/licenses/licenses.html#GPL

import serial 
import string
import sys
import getopt

def usage():
                print ("Usage:")
                print (" -p | --port <device>   e.g. /dev/serial0")
                print (" -b | --baud <speed>    e.g. 9600")
                print (" -f | --file <filename> e.g. /tmp/gps.kml")
                print (" -h | --help     display options")

def main():
            # defaults
            serial_port = "/dev/serial0"
            serial_baud = 9600
            file = './realtime/Realtime GPS.kml'

        try:
                opts, args = getopt.getopt(sys.argv[1:], "p:b:f:h", ["port=", "baud=", "file=", "help"])
        except getopt.GetoptError:
                usage()
                sys.exit(1)
        else:
                for opt, arg in opts:
                        if opt in ("-p", "--port"):
                                serial_port = arg
                        elif opt in ("-b", "--baud"):
                                serial_baud = string.atof(arg)
                        elif opt in ("-f", "--file"):
                                file = arg
                        elif opt in ("-h", "--help"):
                                usage()
                                sys.exit(0)
                        else:
                                print ("Unknown option")
        gps = serial.Serial(serial_port, serial_baud, timeout=1)

        print "Serving data from %s (%d baud) to %s" % (serial_port, serial_baud, file)

        latitude = 0
        longitude = 0
        speed = 0
        heading_in = 0
        altitude = 0
        range = 1000
        tilt = 30
        while 1:
            line = gps.readline()
            datablock = line.split(',')
            print datablock
            if line[0:6] == '$GPRMC':
                latitude_in = string.atof(datablock[3])
                longitude_in = string.atof(datablock[5])
                try:
                    altitude = string.atof(datablock[8])
                except ValueError: # use last good value
                        altitude = altitude
                        speed_in = string.atof(datablock[7])
                        try:
                            heading_in = string.atof(datablock[8])
                        except ValueError:
                                # use last good value
                                heading_in = heading_in
                        if datablock[4] == 'S':
                                latitude_in = -latitude_in
                        if datablock[6] == 'W':
                                longitude_in = -longitude_in

                        latitude_degrees = int(latitude_in/100)
                        latitude_minutes = latitude_in - latitude_degrees*100

                        longitude_degrees = int(longitude_in/100)
                        longitude_minutes = longitude_in - longitude_degrees*100

                        latitude = latitude_degrees + (latitude_minutes/60)
                        longitude = longitude_degrees + (longitude_minutes/60)

                        speed = int(speed_in * 1.852)
                        range = ( ( speed / 100  ) * 350 ) + 650
                        tilt = ( ( speed / 120 ) * 43 ) + 30
                        heading = heading_in

                        if speed < 10:
                                range = 200
                                tilt = 30
                                heading = 0

                        output = """<?xml version="1.0" encoding="UTF-8"?>
        <kml xmlns="http://earth.google.com/kml/2.0">
                <Placemark>
                        <name>%s km/h</name>
                        <description>^</description>
                        <LookAt>
                                <longitude>%s</longitude>
                                <latitude>%s</latitude>
                                <range>%s</range>
                                <tilt>%s</tilt>
                                <heading>%s</heading>
                        </LookAt>
                        <Point>
                                <coordinates>%s,%s,%s</coordinates>
                        </Point>
                </Placemark>
        </kml>""" % (speed,longitude,latitude,range,tilt,heading,longitude,latitude,altitude)

                        f=open(file, 'w')
                        f.write(output)
                        f.close()

        ser.close()

if __name__ == "__main__":
            main()

我添加了行打印数据块,在这里您可以看到它打印的内容。

Serving data from /dev/serial0 (9600 baud) to ./realtime/Realtime GPS.kml
['\x14\xd5\xd4\x95\x15\x89b\x8ab\x8a\x9ab\x82\x8ab\x8a\x8ab\x82\x82\x8abb\x82\xb2b\x8a\x8ab\x8a\x82\x9abb\x8a\x82b\x8a\x82b\x92\xba\x92bb\x8a\x92b\xb2\x92b\x92\x92\x82b\x92\x92R\xba\n']
['5)\x91\x1dA\x1dMY\xb1\xd1\xb1\xc9\xb1\xc5\xcd\xb1\xc5\xd1\xb1\xc5\xe5\xb1\xcd\xc9\xc5\xb1\xb1\xc5\xd5\xb1\xc5\xc1\xb1\xc5\xe1\xd9\xb1\xb1\xc5\xdd\xb1\xcd\xd9\xb1\xc1\xd5\xd5\xb1\xb1\xc5\xe5\xb1\xd1\xdd\xb1\xc1\xe1\xc5\xb1\xc9\xc1\xa9\xdd\xe55)\x91\x1dA\x1dMY\xb1\xd1\xb1\xcd\xb1\xc5\xcd\xb1\xc9\xc9\xb1\xc1\xe5\xb1\xc1\xc1\xd1\xb1\xb1\xc9\xd1\xb1\xd9\xdd\xb1\xc5\xd9\xd9\xb1\xc9\xe1\xb1\xc9\xd5\xb1\xc9\xc1\xb1\xc9\xd1\xc5\xb1\xc9\xc1\xb1\xc9\xd9\xb1\xb1\xb1\xc9\xc9\xa9\xd1\xe55)\x91\x1dA\x1dMY\xb1\xd1\xb1\xd1\xb1\xc5\xcd\xb1\xcd\xc9\xb1\xcd\xd5\xb1\xcd\xc1\xd1\xb1\xc9\xc9\xa9\xd1\t5)\x91\x1dA\x1d11\xb1\xb1\xb1\xb1\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1Y\xb19\xa9\xd1\x155)\x91\x1dA\x1dMQ\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1\xc1\xb9\xc1\xc1\xc1\xc1\xb1\xb1\xb1\xb1\xc5\xcd\xc1\xd9\xb1\xc5\xc5\xe5\xd1\xb1\xc5\xc9\xc9\xd5\xa9\xd9\x155)\x91\x1dAi\x11\x05\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1\xc9\xe1\xb1\xc1\xd1\xb1\xc9\xc1\xc5\xdd\xb1\xc1\xc1\xb1\xc1\xc1\xa9\xd9\xe15)\x91\x1dA\x1d\tM\xb1\xc5\xc5\xd5\xd5\xcd\xdd\xb9\xc1\xc1\xb1\xb1\xb1\xb1\xb1\xb1\xb1\xa9\xd9\t5)\xff$GPRMC', '115538.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7E\r\n']
['$GPVTG', '', '', '', '', '', '', '', '', 'N*30\r\n']
['$GPGGA', '115538.00', '', '', '', '', '0', '00', '99.99', '', '', '', '', '', '*6D\r\n']
['$GPGSA', 'A', '1', '', '', '', '', '', '', '', '', '', '', '', '', '99.99', '99.99', '99.99*30\r\n']
['$GPGSV', '4', '1', '13', '01', '11', '001', '', '06', '11', '103', '', '10', '10', '272', '', '12', '62', '220', '22*7A\r\n']
['$GPGSV', '4', '2', '13', '14', '19', '321', '', '15', '10', '186', '21', '17', '36', '055', '', '19', '47', '081', '21*7B\r\n']
['$GPGSV', '4', '3', '13', '22', '09', '004', '', '24', '67', '166', '29', '25', '20', '241', '18', '26', '', '', '22*43\r\n']
['$GPGSV', '4', '4', '13', '32', '35', '304', '22*4B\r\n']
['$GPGLL', '', '', '', '', '115538.00', 'V', 'N*41\r\n']
['$GPGST', '115538.00', '0.0000', '', '', '', '1305', '1193', '1224*64\r\n']
['$GPZDA', '115538.00', '28', '04', '2017', '00', '00*67\r\n']
['$GPGBS', '115538.00', '', '', '', '', '', '', '*64\r\n']
['$GPRMC', '115539.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7F\r\n']

Traceback (most recent call last):
  File "/home/pi/gegpsd0.2.2", line 120, in <module>
    main()
  File "/home/pi/gegpsd0.2.2", line 59, in main
    latitude_in = string.atof(datablock[3])
  File "/usr/lib/python2.7/string.py", line 382, in atof
    return _float(s)
ValueError: could not convert string to float: 
>>> 

请帮忙

4

1 回答 1

1

GPRMC 应包含:

  1   220516     Time Stamp
  2   A          validity - A-ok, V-invalid
  3   5133.82    current Latitude
  4   N          North/South
  5   00042.24   current Longitude
  6   W          East/West
  7   173.8      Speed in knots
  8   231.8      True course
  9   130694     Date Stamp
  10  004.2      Variation
  11  W          East/West
  12  *70        checksum

你失败的那一行是:

['$GPRMC', '115539.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7F\r\n']

它显示所有定位参数的空字符串。请注意,datablock[2] 包含一个有效性参数,即Vfor invalid

我会将您的支票更改为:

if line[0:6] == '$GPRMC' and datablock[2] == 'A':

因此,您只需尝试解析有效​​的 GPRMC 消息并试一试。


深入研究GPRMC 导航接收器警告

状态V表示 GPS 具有低于内部质量阈值的有效定位,例如因为精度衰减太高或仰角模板测试失败。

于 2017-04-28T17:57:17.767 回答