我正在使用两块 ESP32 板进行 ESP-Now 通信,其中一个 ESP 正在发送数据包,而另一个正在接收它们,同时将这些数据与 GPS 一起记录,以测量特定坐标处的吞吐量,以便我在移动时获得吞吐率和发送方和接收方之间的变化率。
ESP32 接收器和 GPS 模块连接到 Rpi,Rpi 正在运行从串行 com 端口读取数据、解析数据并将其写入文件的脚本。
import serial
import time
import pandas as pd
# Receiver
esp_com = '/dev/ttyUSB1'
esp_baud = 115200
ser = serial.Serial(port=esp_com, baudrate=esp_baud, timeout=0.1)
# GPS
gps_com = '/dev/ttyUSB0'
gps_baud = 4800
sergps = serial.Serial(port=gps_com, baudrate=gps_baud, timeout=0.1)
# title of the file to write data in
title = 'ESP-NOW LoRa'
# time step at which data is logged
timestep = 1
# create lists
XCV = []
# initial dropped/received packets data
drp = 0
rcv = 0
counter = 0
waiting_for_recv = False
# outputs empty data table
df = pd.DataFrame()
# outputs time in seconds since the timer started
then = time.time()
then1 = time.time()
# clear ESP buffers
ser.flushInput()
ser.flushOutput()
# initial data
RECEIVED = 0
RECEIVED = []
LAT = []
LON = []
T = []
t = 0
gpsdata = ''
# loop
while True:
try:
xcv = ser.readline().decode() # decodes string
if xcv != '':
xcv = xcv.split("\r\n")
for x in xcv:
if x != '':
print(x)
XCV.append(x)
if 'RECV' in x:
RECEIVED = int(x[4:]) # sliced first 4 characters
while True:
cvb = sergps.readline()
if cvb != b'':
if 'GNGGA' in cvb.decode():
gpsdata = cvb.decode()
else:
break
now = time.time()
if now > then+timestep:
print(now)
df.at[counter, 'timestamp'] = now
df.at[counter, 'received'] = RECEIVED
drp = 0
rcv = 0
RECEIVED = 0
then = now
counter += 1
vbn = gpsdata.split(',')
if('GNGGA' in vbn[0]) & (vbn[2] != "") & (len(vbn) >= 5):
print('GPS has a fix')
tt = vbn[1]
lat = vbn[2]
lon = vbn[4]
lat = int(lat[0:2])+float(lat[2:])/60
lon = int(lon[0:3])+float(lon[3:])/60
LAT.append(lat)
LON.append(lon)
T.append(t)
t += 1
print('LAT: {}, LON: {}'.format(lat, lon))
else:
print('GPS does not have a fix')
LAT.append(0)
LON.append(0)
# write data to file every 30 seconds
if now > then1+30:
then1 = now
# print GPS dataframe to columns
df['lat'] = LAT
df['lon'] = LON
df.to_excel(title+'.xlsx') # writes data to excel file
except Exception as e:
print(e)
我遇到了一个问题,一旦 GPS 修复,并开始发送带有坐标的完整 NMEA 消息,接收/丢弃的数据包数据丢失,整个脚本挂起。我可以在我的变量中看到 GPS NMEA 数据被正确解析,当 GPS 没有修复时,一切正常,但是一旦它开始发送带有坐标的完整 NMEA 消息,脚本就会中断。
我自己也搞不清楚为什么,我认为代码本身需要优化。
我看到已经有一段时间了,但我偶然发现了这篇文章。
所以我看到一个问题:
while True:
cvb = sergps.readline()
if cvb != b'':
if 'GNGGA' in cvb.decode():
gpsdata = cvb.decode()
else:
break
在这里,当任何数据来自 GPS 模块时,您将陷入循环。得到
gpsdata
.后,你可能也想打破
另一件事在这里你有布尔值所以使用
and
(逻辑)而不是&
(按位):
vbn = gpsdata.split(',')
if('GNGGA' in vbn[0]) & (vbn[2] != "") & (len(vbn) >= 5):
print('GPS has a fix')
在这里阅读更多内容:'and'(布尔值)与'&'(按位)