GPS数据解析
前言
我的这些所有的博客里面,关于gps解析---这篇我刚成为社畜的时候写的python解析代码,我现在看上去属实是烂。虽然作为一个嵌入式方向的程序员,但是还是要有追求的。
那么这么多人因为gps解析的python代码关注了我,首先谢谢大家的关注,其次,我想把更好的办法推介给大家,希望你们再工程中能够直接应用,而不是去实现,想要看实现,可以直接去学习我要推介的库的源码,也不难。我的实现不值得大家学习。
nmea1803协议
NMEA 0183是美国国家海洋电子协会(National Marine Electronics Association )为海用电子设备制定的标准格式。已成了GPS导航设备统一的RTCM(Radio Technical Commission for Maritime services)标准协议。
既然是标准协议,肯定有相应的标准解析库。
pynema2
# -*- coding: utf-8 -*-
import pynmea2
#keyword=input("input file name:")
inputPath = "J91612358.txt"
#outputPath = keyword + ".log"
file = open(inputPath, encoding='utf-8')
for line in file.readlines():
try:
msg = pynmea2.parse(line)
if line.startswith("$GPRMC") or line.startswith("$GNRMC"):
print(repr(msg))
#if (msg.status == 'V'):
# print(msg.datestamp , msg.timestamp)
except pynmea2.ParseError as e:
#print('Parse error: {}'.format(e))
continue
上面是官方提供的demo,可以解析任何符合nmea1803的gps数据,格式化打印解析 GNRMC的如下:
在你不知道解析后的成员时,这个很有用,有助于后面写代码。
实际应用是,可能要过滤,可以用if或者开关语句 switch来过滤
# -*- coding: utf-8 -*-
import pynmea2
#keyword=input("input file name:")
inputPath = "J91612358.txt"
#outputPath = keyword + ".log"
file = open(inputPath, encoding='utf-8')
for line in file.readlines():
try:
msg = pynmea2.parse(line)
if line.startswith("$GPRMC") or line.startswith("$GNRMC"):
#print(repr(msg))
#if (msg.status == 'V'): #有效的都打出来
print(msg.datestamp , msg.timestamp, msg.lat, msg.lat_dir, msg.lon, msg.lon_dir)
elif line.startswith("$GPGGA"):
print(" TODO")
#TODO.....
except pynmea2.ParseError as e:
#print('Parse error: {}'.format(e))
continue
总结
只需要装个nmea2的库就好了,完了就可以用上面的代码去创造你的小世界了。
对了这里如果对于c语言用来解析nmea有需要的,本人git有。传送门
->>>>>>>点击此处