ValueError:无法将字符串从 /usr/lib/python2.7/string.py 转换为浮点数



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

python 2.7的代码,用于在谷歌地球中显示GPS流 #!/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
['x14xd5xd4x95x15x89bx8abx8ax9abx82x8abx8ax8abx82x82x8abbx82xb2bx8ax8abx8ax82x9abbx8ax82bx8ax82bx92xbax92bbx8ax92bxb2x92bx92x92x82bx92x92Rxban']
['5)x91x1dAx1dMYxb1xd1xb1xc9xb1xc5xcdxb1xc5xd1xb1xc5xe5xb1xcdxc9xc5xb1xb1xc5xd5xb1xc5xc1xb1xc5xe1xd9xb1xb1xc5xddxb1xcdxd9xb1xc1xd5xd5xb1xb1xc5xe5xb1xd1xddxb1xc1xe1xc5xb1xc9xc1xa9xddxe55)x91x1dAx1dMYxb1xd1xb1xcdxb1xc5xcdxb1xc9xc9xb1xc1xe5xb1xc1xc1xd1xb1xb1xc9xd1xb1xd9xddxb1xc5xd9xd9xb1xc9xe1xb1xc9xd5xb1xc9xc1xb1xc9xd1xc5xb1xc9xc1xb1xc9xd9xb1xb1xb1xc9xc9xa9xd1xe55)x91x1dAx1dMYxb1xd1xb1xd1xb1xc5xcdxb1xcdxc9xb1xcdxd5xb1xcdxc1xd1xb1xc9xc9xa9xd1t5)x91x1dAx1d11xb1xb1xb1xb1xb1xc5xc5xd5xd5xcdxddxb9xc1xc1xb1Yxb19xa9xd1x155)x91x1dAx1dMQxb1xc5xc5xd5xd5xcdxddxb9xc1xc1xb1xc1xb9xc1xc1xc1xc1xb1xb1xb1xb1xc5xcdxc1xd9xb1xc5xc5xe5xd1xb1xc5xc9xc9xd5xa9xd9x155)x91x1dAix11x05xb1xc5xc5xd5xd5xcdxddxb9xc1xc1xb1xc9xe1xb1xc1xd1xb1xc9xc1xc5xddxb1xc1xc1xb1xc1xc1xa9xd9xe15)x91x1dAx1dtMxb1xc5xc5xd5xd5xcdxddxb9xc1xc1xb1xb1xb1xb1xb1xb1xb1xa9xd9t5)xff$GPRMC', '115538.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7Ern']
['$GPVTG', '', '', '', '', '', '', '', '', 'N*30rn']
['$GPGGA', '115538.00', '', '', '', '', '0', '00', '99.99', '', '', '', '', '', '*6Drn']
['$GPGSA', 'A', '1', '', '', '', '', '', '', '', '', '', '', '', '', '99.99', '99.99', '99.99*30rn']
['$GPGSV', '4', '1', '13', '01', '11', '001', '', '06', '11', '103', '', '10', '10', '272', '', '12', '62', '220', '22*7Arn']
['$GPGSV', '4', '2', '13', '14', '19', '321', '', '15', '10', '186', '21', '17', '36', '055', '', '19', '47', '081', '21*7Brn']
['$GPGSV', '4', '3', '13', '22', '09', '004', '', '24', '67', '166', '29', '25', '20', '241', '18', '26', '', '', '22*43rn']
['$GPGSV', '4', '4', '13', '32', '35', '304', '22*4Brn']
['$GPGLL', '', '', '', '', '115538.00', 'V', 'N*41rn']
['$GPGST', '115538.00', '0.0000', '', '', '', '1305', '1193', '1224*64rn']
['$GPZDA', '115538.00', '28', '04', '2017', '00', '00*67rn']
['$GPGBS', '115538.00', '', '', '', '', '', '', '*64rn']
['$GPRMC', '115539.00', 'V', '', '', '', '', '', '', '280417', '', '', 'N*7Frn']
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: 
>>> 

请帮忙

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*7Frn']

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

我会将您的支票更改为:

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

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

深入了解

GPRMC 导航接收器警告:

状态为V表示 GPS 的有效定位低于内部质量阈值,例如,因为精度稀释过高或高程模板测试失败。

最新更新