我做了一个python程序从串口读取gps数据。当插入USB接口时,GPS圆盘会连续传输NMEA数据语句。我的程序打开端口,然后尝试读取、解析数据,然后将其与从Arduino中提取的其他数据一起写入文本文件。在
我遇到的问题是,当我第一次运行程序时,它偶尔无法读取数据。我加入了一些Try/Exception捕捉,结果发现从GPS串行端口读取数据失败。
如果我点击Cntrl-C几次,这似乎可以解决它遇到的问题,然后它就可以正常工作了。我倾向于认为这是一个时间问题,当GPS在流媒体和什么时候程序试图读取串行缓冲区。在
显然我在代码中做了一些错误的事情。我只是尽我所能把它拼在一起,就我的目的而言,它运行得很好,但需要一点帮助,使它更坚如磐石,这样其他可能使用它的人不会被它的古怪行为所迷惑。在
下面是它(由于记事本++的复制粘贴,一些缩进是错误的)。任何帮助都会很好。在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")