2017-04-26 93 views
0

我想使用此代碼將我的gps數據流式傳輸到谷歌地球。我已經嘗試過用於python 3.4的版本,但是我得到了錯誤,有人說gps3並沒有真正適用於python3,所以我嘗試使用python2ValueError:無法將字符串轉換爲浮動/usr/lib/python2.7/string.py

python 2.7的代碼用於顯示Google earth中的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 
['\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\x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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: 
>>> 

請幫

+1

'數據塊[3]'不代表十進制數。 –

+1

請提供一個最小,完整和可驗證的示例,請參閱https://stackoverflow.com/help/mcve – Setop

+0

我會嘗試打印'datablock',因爲我懷疑它得到的不是它所期望的(正如Peter Wood提到的)。此代碼已有近十年的歷史。 – TemporalWolf

回答

1

GPRMC should contain

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]包含的V的有效性參數。

我想你的支票更改爲:

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

所以你只嘗試解析有效​​GPRMC消息,並給出一個去。


掘略偏到GPRMC navigation receiver warning

V狀態表示所述GPS具有有效的修復程序可以是更低的內部質量閾值,例如因爲精度的稀釋太高或者高程模板測試失敗。

相關問題