因此,我正在使用GPS module,Python和MySQL database進行應用程序。Python GPS數據捕獲
所以我寫了一些代碼,嘗試從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一個串行讀數[示例](https://github.com/Knio/pynmea2/blob/master/examples/serial.py) repo –
的確如此,除非你會讀完整個問題,否則你會看到你無法解析速度。 – Robin