2014-02-05 120 views
1

我得到了返回NMEA數據的GPS模塊。 當我試圖打印所有使用以下代碼返回的數據時,我得到this從python的串口讀取數據

while True: 
try: 
    rcv = port.read() 
    print rcv 

然後,我做了一些修改,將讀取NMEA數據清理器。它看起來像這樣:

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=10.0) 

line = [] 
print("connected to: " + port.portstr) 

while True: 
    try: 
     rcv = port.read() 
    except: 
     rcv = '' 


    line.append(rcv) 

    if rcv == '\n': 
     line = "".join(line)   
     print line 
     line = [] 

輸出看起來像這樣:

$ GPGGA,183345.000,5023.3424,N,01857.3817,E,1,7,1.25,313.3,男,42.1,男, * 53

$ GPGSA,A,3,09,26,28,08,15,18,17 ,,,,,, 1.52,1.25,0.88 * 06

$ GPRMC,183345.000,A ,5023.3424,N,01857.3817,E,0.40,55.07,050214 ,,, A * 54

$ GPVTG,55.07,T,M,0.40,N,0.74,K,A * 0D

$ GPGGA,183346.000,5023.3423,N,01857.3817,E,1,7,1.25,313.3,M,42.1, M,* 57

$ GPGSA,A,3,09,26,28,08,15,18,17 ,,,,,, 1.52,1.25,0.88 * 06

問題有時它會遺漏一些逗號或其他數據,而NMEA解析器讀錯了。有沒有更好更清潔的方式通過串行讀取整個NMEA幀?

回答

3

您可以使用readline的,而不是閱讀,將繼續讀取字符,直到接收到EOL。