【发布时间】:2017-05-05 09:42:30
【问题描述】:
所以我正在使用GPS module、Python 和MySQL database 制作应用程序。
所以我编写了一些代码,尝试从 GPS 捕获数据并将其存储在数据库中。我正在使用一个名为“pynmea2”的插件来解析一些数据(经度和纬度)。但是,我需要更多的数据,所以,我已经尝试了很多不同的东西,但是我的程序一直在崩溃。有人可以帮我解决这个问题吗?
大多数时候我从串行连接中获取所有数据,但我希望能够从中剥离数据。所以我得到的例子:[b'$GPGGA,093512.000,,,,,0,3,,,M,,M,,*47\r\n', b'$GPGLL,,,,,093512.000,V,N*76\r\n', b'$GPGSA,A,1,,,,,,,,,,,,,,,*1E\r\n', b'$GPGSV,3,1,11,15,72,214,,24,52,276,,13,48,141,,17,31,093,29*70\r\n', b'$GPGSV,3,2,11,18,28,292,,28,27,049,25,19,24,120,24,12,23,211,13*7E\r\n', b'$GPGSV,3
好吧,从中提取数据并不是那么简单,但使用 pynmea2 库(仅我允许使用的库就可以了。
所以,我现在需要速度、纬度和经度,但现在速度让我很困扰。它给了ValueError: could not convert string to float: "22*49\\r\\n'"
很多次,因为我没有采取正确的方法来查找数据然后“解析”它。
这是我目前正在使用的代码;
from model.GPSParser import GPSParser
from model.DB import DB
import serial
import time
import datetime
import pynmea2
#########################################
# This is the main code to setup the
# serial connection with the GPS module.
# it needs to be OR runt as root OR as
# pi with all the root rights.
#########################################
port = "/dev/ttyAMA0"
ser = serial.Serial(port, 9600, timeout=0)
#########################################
# These are all the global variables
# to be used. All defined and set to
# zero or their standard 'Null' value.
#########################################
lat = 0.0
lon = 0.0
cur_speed = 0.0
while True:
try:
# Get the data from the serial monitor.
data = str(ser.readlines()).lstrip("b'")[:-3]
# print(data)
#########################################
# Find the speed, to check if we're
# standing still or not. Save it in a
# @var speed
#########################################
if data.find('$GPVTG') != -1:
cur_speed = data.split(",")[7]
#########################################
# Get the Latitude and Longitude
#########################################
if data.find('$GPGGA') != -1:
print(data)
# Check whether the data strings are empty or not.
if GPSParser.parseLatitude(data) != "" and GPSParser.parseLongitude(data) != "":
lat = GPSParser.parseLatitude(data)
lon = GPSParser.parseLongitude(data)
# Debug printing
# print("Latitude: " + GPSParser.parseLatitude(data))
# print("Longitude: " + GPSParser.parseLongitude(data))
# print("Speed: " + cur_speed)
#########################################
# Insert the coordinates into the database
# Be sure to check of we are really driving
# So when the speed is higher then 5 km/u
# Store everything into the database.
#########################################
if float(cur_speed) > 5.0:
db = DB()
db.insertCoordinates(lat, lon, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# Wait a bit to not overload the Serial port
time.sleep(0.5)
############################################################
# The error handling
############################################################
except serial.serialutil.SerialException:
ser.close()
port = "/dev/ttyAMA0"
ser = serial.Serial(port, 9600, timeout=0)
continue
except pynmea2.ParseError:
# print("Error on parsing object, continuing...")
continue
except BlockingIOError:
# print("Blocking I/O error, continuing...")
continue
except TypeError:
# print("Type error, continuing...")
continue
except IndexError:
# print("To catch an error...")
continue
except KeyboardInterrupt:
print("\nProgram stopped.")
exit()
所以从模型中导入的作用不大,只有数据库连接和“gps解析器”只是pynmea解析一串数据然后返回。
所以我想要的是这样的:
它每秒获取一次脉冲的所有数据,
然后将其全部拆分为以$GP 变量开头的卡盘,然后我可以搜索第二个变量部分,例如VTG 或GGA。然后我可以使用该字符串转换为正确的值,以便在需要时提取速度、纬度、经度和其他数据。
希望你们能理解我并帮助我。
【问题讨论】:
-
pynmea2 repo 中已经有一个串行读取example
-
确实如此,除非您阅读了整个问题,否则您会发现无法解析速度。