2015-06-01 177 views
0

我做了一個python程序,從串口讀取gps數據。插入USB時,GPS球盤連續播放NMEA數據語句。我的程序打開端口,然後嘗試讀取數據,解析數據,然後將其寫入文本文件以及從Arduino中提取的其他數據。python從串口讀取GPS

我遇到的問題是,偶爾在第一次運行程序時無法讀取數據。我放了一些Try/Exception捕獲,發現它沒有以某種方式從GPS串口讀取數據。
如果我點擊Cntrl-C幾次,這似乎把它從它遇到的問題中解放出來,然後開始正常工作。我傾向於認爲它是一個定時問題,當流媒體中的GPS和程序試圖讀取串行緩衝區時。

很明顯,我在代碼中做了一些不正確的事情。我儘可能地將它打在了一起,盡我所能,併爲我的目的,它可以正常工作,但可以做一些幫助,使其更堅如磐石,使其他人可能會使用它不會被其片狀行爲困惑。

這裏是它下面的(由於Notepad ++的複製粘貼,一些縮進是錯誤的)。任何幫助都會很棒。

import serial 
import pynmea2 
import time 
#####Global Variables###################################### 
#be sure to declare the variable as 'global var' in the functions 

ID = 0 
arduino = 0 
ser2 = 0 
fh = "" 
rssi_dB = 0 
gps = "NaN" 

# User configurable 

gps_com_port = 19 # com 19 Shop7 at Hm 8 
arduino_com_port = 18 # com 18 Shop7 at Hm 6 

# MCS2000 specific conversion rates 
# DON'T CHANGE!!! 
slope1 = 0.0170 
slope2 = 0.008 
slope3 = 0.020 
slope4 = 0.000 
cutoff1 = 700 
cutoff2 = 430 
cutoff3 = 380 
cutoff4 = 0 
cutoff5 = 0 
y_int1 = 3.399 
y_int2 = 2.692 
y_int3 = 3.949 


#####FUNCTIONS############################################# 
#initialize serial connection 
def init_serial(): 
COMNUM1 = arduino_com_port #set you COM port # here 
COMNUM2 = gps_com_port 
global arduino #must be declared in each fxn used 
global ser2 
arduino = serial.Serial(
        port = COMNUM1 -1, 
        baudrate = 9600, 
        timeout = 1 
        ) 
ser2 = serial.Serial(
        port = COMNUM2 -1, 
        baudrate = 4800, 
        timeout = 1 
        ) 

if arduino.isOpen(): 
    print 'Open: ' + arduino.portstr 
if ser2.isOpen(): 
    print 'Open: ' + ser2.portstr 

def init_file(): 
filename = raw_input('Enter save file[name.txt]:') 
global fh 
fh = open(filename,"w") 

def rssi_convert(rssi): 
#print ("rssi_convert\n")  
if rssi<=cutoff1 and rssi>=cutoff2: 
    rssi_dB=((rssi*0.004888)-y_int1)/slope1 
if rssi<=cutoff2 and rssi>=cutoff3: 
    rssi_dB=((rssi*0.004888)-y_int2)/slope2 
if rssi<=cutoff3 and rssi>=cutoff4: 
    rssi_dB=((rssi*0.004888)-y_int3)/slope3 
#if rssi<=cutoff4 and rssi>=cutoff5: 
# rssi_dB=((rssi*0.004888)-2.047)/slope4 

return float(rssi_dB) 


#####SETUP################################################ 
#this is a good spot to run your initializations 
init_file() 
init_serial() 
time.sleep(2) 
data_log = "TOD,Lat,Long,Alt,Qual,Ref_ID,Num_Sat,Hor_Dil,RSSI\n" 
fh.writelines(data_log) #write header to file 
rssi = arduino.readline()  
while str(rssi) == "A": 
arduino.write("q") 
rssi = arduino.readline() 

#####MAIN LOOP############################################ 
while 1: 

arduino.flushInput() 
try: 
    gps = ser2.readline() 
except: 
    print("Read GPS FAILED\n") 

try:  
    gps_msg = pynmea2.parse(gps) 
except: 
    print("Failed to parse GPS\n") 

try:  
    if gps_msg.sentence_type == 'GGA': 
     arduino.write("q") 
     time.sleep(.2) 
     rssi = arduino.readline() 

     try: 
      rssi_dB = rssi_convert(float(rssi.strip('\0'))) 
     except: 
      print("RSSI Conversion FAILED\n") 
     try: 
      data_log = str(gps_msg.timestamp) + "," + str(gps_msg.latitude) + "," + str(gps_msg.longitude) + "," + str(gps_msg.altitude) + "," + str(gps_msg.gps_qual) + "," + str(gps_msg.ref_station_id) + "," + str(gps_msg.num_sats) + "," + str(gps_msg.horizontal_dil) + "," + str(rssi_dB) + "\n" 
      print str(ID) + data_log 
      fh.writelines(data_log) #write data to file 
      ID = int(ID) + 1 
     except: 
      pass#ID=ID+1 
except: 
    print("GPS Sentence Loop Failed") 

回答

0

您可以通過打印獲得有關例外的其他信息。

try: 
    gps = ser2.readline() 
except Exception as e: 
    print('Read GPS failed: {0}'.format(e)) 

您也可以嘗試將arduino.flushInput()置於循環之外。根據時間的不同,您可能會丟失一些數據。