我想本GPS位置讀數,當我運行在覆盆子PI下面的代碼,程序打印10-12輸出,然後將其顯示錯誤如下解析:
回溯(最近呼叫最後):
文件 「simplegpsparsing.py」,第24行,在
get_present_gps()
文件 「simplegpsparsing.py」,第16行,在get_present_gps
LAT,_,LON = line.split( '' )[2:5]
ValueError:需要多個值才能解包
i wa nt當前的GPS值(緩衝區應該用即時GPS更新),以便可以知道當前的GPS值。 我的代碼去如下:
NMEA數據使用python 2.7
import os
import serial
def get_present_gps:
ser= serial.Serial('/dev/ttyAMA0',9600)
ser.open()
while True :
f=open('/home/pi/Desktop/gps1','w')
data=ser.read(ser.inWaiting()) # read no.of bytes in waiting
f.write(data) #write data into file
f.flush() # flush(method) from buffer into os buffer
os.fsync(f.fileno()) #ensure to write from os buffer(internal buffers)into disk
f = open('/home/pi/Desktop/gps1','r') # fetch the required file
for line in f.read().split('\n') :
if line.startswith('$GPGGA') :
lat, _, lon = line.strip().split(',')[2:5]
try :
lat = float(lat)
lon = float(lon)
print lat
print lon
except :
pass
# something wrong happens with your data, print some error messages
get_present_gps()
如果串行端口是開放的,但不關閉,將它造成任何問題?上述代碼是否符合我的要求即獲得瞬時值?
你應該等到你有一個完整的行......即不要處理一個不以'\ n'結尾的行。但是,不應該擔心你打開串口。 – Floris
爲什麼要將數據寫入文件?你不能直接解析'data'變量嗎?這可能會更容易... – Floris
@弗洛伊斯你能指導我如何做到這一點,是的嘗試尋找它,不幸的是我得到了一些其他結果.. –