【问题标题】:python reading from serial port GPSpython从串口GPS读取
【发布时间】:2015-08-15 08:38:21
【问题描述】:

我做了一个从串口读取gps数据的python程序。当插入 USB 时,GPS 圆盘会连续传输 NMEA 数据语句。我的程序打开端口,然后尝试读取数据、解析数据,然后将其与从 Arduino 中提取的其他数据一起写入文本文件。

我遇到的问题是,当我第一次运行程序时,有时它无法读取数据。我放入了一些 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")

【问题讨论】:

    标签: python serial-port


    【解决方案1】:

    您可以通过打印获取有关异常的更多信息。

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

    您也可以尝试将arduino.flushInput() 放在循环之外。根据时间的不同,您可能会丢失一些数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2015-01-06
      相关资源
      最近更新 更多