1 Star 0 Fork 2

admin/esp32-gps-micropython

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gps_info_format.py 4.18 KB
一键复制 编辑 原始数据 按行查看 历史
hui-shao 提交于 2021-11-25 14:27 +08:00 . [fix] 修复 round 方法不存在的问题
# -*-coding:utf-8 -*-
"""
# Author: Hui-Shao
# Description: 用于格式化 GPS 模块(ublox-neo-6m) 串口信息的一个工具
"""
class GpsInfo:
"""
使用方法: g = GpsInfo(str_in)
"""
raw_dict = {}
class P:
position = [0.00, 0.00] # 默认纬度在前 大体格式 36.4001151, 117.081768
position_s = ["null"] * 5 # 经纬度 大体格式 ['A', '36.4001209', 'N', '117.0817661', 'E'] A 是有效定位的意思
position_f_s = ["null"] * 5 # 以度分秒显示的经纬度(精度低) 大体格式 ['A', '36°24′0″', 'N', '117°4′54″', 'E']
class DT:
date_str = "null"
time_str = "null"
time_ms_str = "null"
def __init__(self, _raw_input: str):
self.raw_dict = self._text_format(_raw_input)
self._generate(self.raw_dict) # 调用 generate 函数 提取部分信息保存至类变量中
@staticmethod
def _text_format(raw_str_in: str) -> dict:
"""
用于将从 gps 模块处接收的 str 转为 dict
:param raw_str_in: 输入字符 多行文本
:return: dict
"""
res = dict()
lines = raw_str_in.splitlines()
for line in lines:
if line.startswith("$G"):
info_arr = line.split(",")
res.update({info_arr[0]: {}})
for i in range(1, len(info_arr), 1):
res[info_arr[0]].update({str(i): info_arr[i]})
else:
continue
return res
def _generate(self, _dict: dict) -> None:
"""
从 _dict 解析数据, 用于给类变量赋值
:param _dict: gps 信息转换得到的 dict
:return: None
"""
def run():
# 生成 self.P.position_s
for i in range(0, 5, 1):
self.P.position_s[i] = _dict.get("$GPRMC", {}).get(str(i + 2), "null")
self.P.position_f_s[i] = _dict.get("$GPRMC", {}).get(str(i + 2), "null")
# 生成 self.P.position_f_s 和 self.position
# 以及 将 self.position_s 小数点左移两位 例如 3640.01151 变为 36.4001151
if "null" not in self.P.position_f_s and all(len(j) > 0 for j in self.P.position_f_s): # 防止下面的类型转换出错
self.P.position = [(float(self.P.position_s[1]) / 100), (float(self.P.position_s[3]) / 100)]
self.P.position_s[1] = str(self.P.position[0])
self.P.position_s[3] = str(self.P.position[1])
for i in range(1, 4, 2):
du = int(float(self.P.position_f_s[i]) / 100)
fen = int((float(self.P.position_f_s[i]) / 100 - du) * 60)
miao = int((((float(self.P.position_f_s[i]) / 100 - du) * 60 - fen) * 60))
self.P.position_f_s[i] = str(du) + "°" + str(fen) + "′" + str(miao) + "″"
# 生成 self.DT.xxx
date = _dict.get("$GPRMC", {}).get("9", "")
time_ = _dict.get("$GPRMC", {}).get("1", "")
if len(date) * len(time_) > 0:
time_ms = time_.split(".")[1] + "0" # 小数点后默认只有两位, 因此补个0
time_ = time_.split(".")[0]
self.DT.date_str = "{}-{}-{}".format(date[4:6], date[2:4], date[0:2])
self.DT.time_str = "{0}:{1}:{2}".format(time_[0:2], time_[2:4], time_[4:6])
self.DT.time_ms_str = self.DT.time_str + ".{}".format(time_ms)
return None
try:
run()
except KeyError:
print("[!] KeyError")
except ValueError:
print("[!] ValueError")
except IndexError:
print("[!] IndexError")
except Exception as e:
print(e)
else:
pass
return None
if __name__ == '__main__': # for debug
f = open("./tests/test.txt", "r", encoding="utf-8")
info = f.read()
f.close()
G = GpsInfo(info)
print(G.raw_dict)
# GpsInfo.P
print(G.P.position)
print(G.P.position_s)
print(G.P.position_f_s)
print("{:.5}".format(G.P.position[0])) # 5 位有效数字
# GpsInfo.DT
print(G.DT.date_str + " " + G.DT.time_ms_str)
# other
print("Finished.")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cehx_0/esp32-gps.git
git@gitee.com:cehx_0/esp32-gps.git
cehx_0
esp32-gps
esp32-gps-micropython
master

搜索帮助