首页 > 编程语言 >python 串口读取gps

python 串口读取gps

时间:2023-05-05 19:33:48浏览次数:38  
标签:ser VTG python global read GGA 串口 print gps

# coding: utf-8 # last modified:20220824 import time import serial import re   utctime = '' lat = '' ulat = '' lon = '' ulon = '' numSv = '' msl = '' cogt = '' cogm = '' sog = '' kph = '' gps_t = 0   ser = serial.Serial("/dev/ttyUSB0", 9600)   if ser.isOpen():     print("GPS Serial Opened! Baudrate=9600") else:     print("GPS Serial Open Failed!")     def Convert_to_degrees(in_data1, in_data2):     len_data1 = len(in_data1)     str_data2 = "%05d" % int(in_data2)     temp_data = int(in_data1)     symbol = 1     if temp_data < 0:         symbol = -1     degree = int(temp_data / 100.0)     str_decimal = str(in_data1[len_data1-2]) + str(in_data1[len_data1-1]) + str(str_data2)     f_degree = int(str_decimal)/60.0/100000.0     # print("f_degree:", f_degree)     if symbol > 0:         result = degree + f_degree     else:         result = degree - f_degree     return result     def GPS_read():         global utctime         global lat         global ulat         global lon         global ulon         global numSv         global msl         global cogt         global cogm         global sog         global kph         global gps_t         if ser.inWaiting():             if ser.read(1) == b'G':                 if ser.inWaiting():                     if ser.read(1) == b'N':                         if ser.inWaiting():                             choice = ser.read(1)                             if choice == b'G':                                 if ser.inWaiting():                                     if ser.read(1) == b'G':                                         if ser.inWaiting():                                             if ser.read(1) == b'A':                                                 #utctime = ser.read(7)                                                 GGA = ser.read(70)                                                 GGA_g = re.findall(r"\w+(?=,)|(?<=,)\w+", str(GGA))                                                 # print(GGA_g)                                                 if len(GGA_g) < 13:                                                     print("GPS no found")                                                     gps_t = 0                                                     return 0                                                 else:                                                     utctime = GGA_g[0]                                                     # lat = GGA_g[2][0]+GGA_g[2][1]+'°'+GGA_g[2][2]+GGA_g[2][3]+'.'+GGA_g[3]+'\''                                                     lat = "%.8f" % Convert_to_degrees(str(GGA_g[2]), str(GGA_g[3]))                                                     ulat = GGA_g[4]                                                     # lon = GGA_g[5][0]+GGA_g[5][1]+GGA_g[5][2]+'°'+GGA_g[5][3]+GGA_g[5][4]+'.'+GGA_g[6]+'\''                                                     lon = "%.8f" % Convert_to_degrees(str(GGA_g[5]), str(GGA_g[6]))                                                     ulon = GGA_g[7]                                                     numSv = GGA_g[9]                                                     msl = GGA_g[12]+'.'+GGA_g[13]+GGA_g[14]                                                     #print(GGA_g)                                                     gps_t = 1                                                     return 1                             elif choice == b'V':                                 if ser.inWaiting():                                     if ser.read(1) == b'T':                                         if ser.inWaiting():                                             if ser.read(1) == b'G':                                                 if gps_t == 1:                                                     VTG = ser.read(40)                                                     VTG_g = re.findall(r"\w+(?=,)|(?<=,)\w+", str(VTG))                                                     cogt = VTG_g[0]+'.'+VTG_g[1]+'T'                                                     if VTG_g[3] == 'M':                                                         cogm = '0.00'                                                         sog = VTG_g[4]+'.'+VTG_g[5]                                                         kph = VTG_g[7]+'.'+VTG_g[8]                                                     elif VTG_g[3] != 'M':                                                         cogm = VTG_g[3]+'.'+VTG_g[4]                                                         sog = VTG_g[6]+'.'+VTG_g[7]                                                         kph = VTG_g[9]+'.'+VTG_g[10]                                                 #print(kph)   try:     while True:         if GPS_read():             print("*********************")             print('UTC Time:'+utctime)             print('Latitude:'+lat+ulat)             print('Longitude:'+lon+ulon)             print('Number of satellites:'+numSv)             print('Altitude:'+msl)             print('True north heading:'+cogt+'°')             print('Magnetic north heading:'+cogm+'°')             print('Ground speed:'+sog+'Kn')             print('Ground speed:'+kph+'Km/h')             print("*********************") except KeyboardInterrupt:     ser.close()     print("GPS serial Close!")

标签:ser,VTG,python,global,read,GGA,串口,print,gps
From: https://www.cnblogs.com/oftenlin/p/17375176.html

相关文章

  • Python 脚本部署和发布 Django 应用程序的示例代码及注释
    代码说明:1、在脚本中定义了要部署的Django应用程序名称、Docker镜像名称和标签。2、使用字符串模板定义了KubernetesDeployment和Service的YAML文件。在字符串模板中使用了变量,用于替换实际的值。3、将Deployment和Service的YAML文件保存到本地文件中,并使用kube......
  • python如何表格对齐, 表格输出对齐, 输出内容对齐
    #我们输出的要求是对齐,但是名字的长度不一样defdeal_data(var):foriinrange(12-len(var)):#为什么是12呢?必须保证里面单个长度不能大于12,具体多少可以看自己的每个单元格的数据,不超过就可以var+=""returnvarlist1=[["Jacky","79","1......
  • C# 串口通信
    这里浅说一下蓝牙与串口的区别:        蓝牙:连接以mac地址为主,显示名称可以更改,低功耗蓝牙还需要配置服务与特征(服务有读,写,可读可写区别)特点:不同设备连接同一台蓝牙设备,mac地址与显示名称都是唯一的    串口:连接以端口名称为主,例如com1,com2,连接时需要配置参数......
  • 一Python flask框架
    一:Pythonflask框架前言1.Python面向对象的高级编程语言,以其语法简单、免费开源、免编译扩展性高,同时也可以嵌入到C/C++程序和丰富的第三方库,Python运用到大数据分析、人工智能、web后端等应用场景上。2.Python目前主要流行的web框架:flask、Django、Tornado补充一下,我们......
  • Python简易学生管理系统
    Python简易学生管理系统Projectmain.pymanager.pystudent.pystudents_info.json1、学生文件student.py#学生类classStudent(object):#存放学生信息student_info={}#学生初始化方法def__init__(self,id,name,addr,tel):self.id=......
  • Python+Pandas批量合并大量excel文件
    requirments.txtet-xmlfile==1.1.0numpy==1.24.3openpyxl==3.1.2pandas==2.0.1python-dateutil==2.8.2pytz==2023.3six==1.16.0tzdata==2023.3main.pyimportosimportpandasaspddir_path=os.path.dirname(os.path.abspath(__file__))source_location=o......
  • Python实现遍历读取文件或文件夹
    参考:https://www.jb51.net/article/258341.htmos.walk本身已经是遍历读取,包含所有的子文件(夹)path=u'.'#文件路径defnewWalkFile2(file):#main_dir当前路径,sub_dir_list当前路径下的子文件夹是个数组,sub_file_list当前路径下具体文件formain_dir,sub_dir_l......
  • Python自动寻路算法
    一、题目描述在一个迷宫游戏里,有一些小怪物要去攻击主角,现在希望给这些小怪物加上聪明的AI,让他们可以自动绕过迷宫中的障碍物,寻找到主角所在。二、解题思路迷宫游戏里的场景通常都是由小方格组成。假设我们有一个7*5大小的迷宫,图中红色格子是终点,绿色格子是起点,蓝色格子是一堵墙......
  • 虚拟串口工具VSPD简单使用
    1、下载资源下载 [虚拟串口+串口调试工具]2、安装及破解解压vspd7.2.308.zip件如下,点击vspd.exe安装(安装没啥技巧一直下一步就好了,不过需要记住安装地址),在安装完成之后vspdctl.dll覆盖至安装文件夹覆盖掉原有文件(破解) 3、创建虚拟串口运行软件如下:点击新增,虚拟串......
  • Python教程:pandas读写txt文件——DataFrame和Series
    大家用pandas一般都是读写csv文件或者tsv文件,读写txt文件时一般就withopen了,其实pandas数据类型操作起来更加方便,还是建议全用pandas这一套。读txt文件代码如下,主要是设置正则表达式的分隔符(sep参数),和列名取消(header参数),以及不需要列索引(index_col)。1df=pd.read_csv("workl......