py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | # -*- coding: utf-8 -* # 117.410465,31.768830 # 使用前先使用u-center设置GPS输出,去掉多余的输出 # sudo chmod 666 /dev/ttyUSB0 #打不开串口需要给于权限 import os import time import serial import serial.tools.list_ports import numpy as np import time import ctypes import os #1载入库 libLoad = ctypes.cdll.LoadLibrary sharelib = libLoad( "./build/libpython2share.so" ) #python创建结构体 class py_Struct_GPS(ctypes.Structure): _fields_ = [ ( 'flag' , ctypes.c_int), #c++ int ( 'msg' , ctypes.c_char_p), #c++ char * ( 'longitude' , ctypes.c_float), # c++ float ( 'latitude' , ctypes.c_float),# c++ float ( 'high' , ctypes.c_float),# c++ float ( 'time' , ctypes.c_int)# c++ float ] #c++ char *[] #python结构体赋予初值 struct_gps=py_Struct_GPS() struct_gps.flag=ctypes.c_int(0) struct_gps.msg=ctypes.c_char_p( 'GPS DATA' .encode()) struct_gps.longitude=ctypes.c_float(0.) struct_gps.latitude=ctypes.c_float(0.) struct_gps.high=ctypes.c_float(0.) struct_gps.time=ctypes.c_int(0) print( "结构体gps_old \n" ,struct_gps) print(struct_gps.flag,struct_gps.msg.decode(),struct_gps.longitude,struct_gps.latitude,struct_gps.high,struct_gps.time) #0 GPS DATA 0.0 0.0 0.0 0.0 def SetGPSToShare_struct(struct_gps,flag,msg,longitude,latitude,high,time): #print("========",flag) struct_gps.flag=ctypes.c_int( int (flag)) #print("struct_gps.flag",struct_gps.flag) struct_gps.msg=ctypes.c_char_p(msg.encode()) struct_gps.longitude=ctypes.c_float(( float (longitude))) struct_gps.latitude=ctypes.c_float(( float (latitude))) struct_gps.high=ctypes.c_float(( float (high))) struct_gps.time=ctypes.c_int( int (time)) #从c++获取数据-数据保存在 共享内存或C++直接生成 # 定义返回类型为结构体类型 sharelib.py_Set_cgg_gps_Struct_.restype = py_Struct_GPS # 调用获取gps数据-返回数据结构体 struct_gps= sharelib.py_Set_cgg_gps_Struct_(struct_gps) #print(struct_gps.flag) print(struct_gps.msg.decode()) #必须解码 #print("结构体gps_new \n",struct_gps) #print(struct_gps.flag,struct_gps.msg.decode(),struct_gps.longitude,struct_gps.latitude,struct_gps.high,struct_gps.time) #1 new data 1.559999942779541 2.559999942779541 3.559999942779541 5.559999942779541 def gps_Get_data(ShareImages, lock ,gps_debug=1): os.system( 'cls' ) serial_portlist = list(serial.tools.list_ports.comports()) print( "如果串口打不开,请执行 sudo chmod 666 /dev/ttyUSB0 后面是可用串口号" ) if len(serial_portlist) <= 0 : print( '未发现可用串口' ) else : for i in range (len(serial_portlist)): print( "可用串口:" ,serial_portlist[i]) #port_gps = '/dev/ttyUSB'+input('请输入GPS模块所在串口:') #port_gps = 'COM'+input('请输入GPS模块所在串口:') #port_gps="/dev/ttyUSB0" port_gps=serial_portlist[0][0] print(port_gps) ser = serial.Serial(port_gps,115200,timeout = 2) #---------------------------------------------- os.system( 'cls' ) init = 3 #数据 weidu_xy=0.0#纬度 jingdu_xy=0.0 #经度 high=0.0 #海拔高 gps_use=0 #gps是否可用 id_=000000 #帧号 while True : if ser.in_waiting > 0 : line = str(ser.readline().decode( "utf-8" )) GETSTR_List=[] GETSTR_List.append(line) GPRMC_List = GETSTR_List[0].split( ',' ) #print('$GPRMC 有 %d 个字段'%len(GPRMC_List)) #检查','分割情况 13 #print(len(GPRMC_List)) if len(GPRMC_List)!=15:#检查数据完整度 #if gps_debug==1: print('当前卫星定位无效,数据不完整') pass #经纬度维持上传一次最后的数据 #weidu_xy=0 #纬度 #jingdu_xy=0 #经度 #high=0 #海拔高 gps_use=0 #gps是否可用 #id_=000000 #帧号 time_= int (time.time()) ShareImages[1]=gps_use ShareImages[2]=weidu_xy ShareImages[3]=jingdu_xy ShareImages[4]=high ShareImages[5]=time_ continue #跳过本次循环 #------------------------------------------------ #print('') if GPRMC_List[6] == '0' :#0=Invalid, 1=2D/3D, 2=DGNSS, 4=Fixed RTK, 5=Float RTK, 6=Dead Reckoning if gps_debug==1: print( '当前卫星定位无效,数据完整但无效' ) #经纬度维持上传一次最后的数据 weidu_xy=0 #纬度 jingdu_xy=0 #经度 high=0 #海拔高 gps_use=0 #gps是否可用 #id_=000000 #帧号 time_= int (time.time()) ShareImages[1]=gps_use ShareImages[2]=weidu_xy ShareImages[3]=jingdu_xy ShareImages[4]=high ShareImages[5]=time_ elif GPRMC_List[6] == '1' or GPRMC_List[6] == '2' or GPRMC_List[6] == '3' or GPRMC_List[6] == '4' or GPRMC_List[6] == '5' : #if gps_debug==1: print('定位正常') #$GNGGA,063911.10,3402.02555,N,10845.82698,E,1,06,3.41,467.0,M,-27.9,M,,*6C #if gps_debug==1: print(GETSTR_List[0]) #print('') #-------------------------------------------- UTC = GPRMC_List[1][0:2] + ':' + GPRMC_List[1][2:4] + ':' + GPRMC_List[1][4:6] #UTC = UTC +' '+ GPRMC_List[9][0:2] + '/' + GPRMC_List[9][2:4] + '/20' + GPRMC_List[9][4:6] #print('卫星UTC时间:'+UTC) #--------------------------------------------- weidu_xy = int (GPRMC_List[2][0:2])+ float (GPRMC_List[2][2:11])/60 weidu_xy=format(weidu_xy, '.9f' ) jingdu_xy = int (GPRMC_List[4][0:3])+ float (GPRMC_List[4][3:12])/60 jingdu_xy=format(jingdu_xy, '.9f' ) high = float (GPRMC_List[9]) #米 high=format(high, '.2f' ) gps_use=1 time_= int (time.time()) ShareImages[1]=gps_use ShareImages[2]=weidu_xy ShareImages[3]=jingdu_xy ShareImages[4]=high ShareImages[5]=time_ #高度: 467.00 经度 108.7637830 纬度 34.0337592 if gps_debug==1: print( "有效" , gps_use, "纬度" ,weidu_xy, "经度" , jingdu_xy , "高度" ,high, "时间戳" ,time_) else : if gps_debug==1: print( '当前卫星定位无效,数据完整但无效,数据有误,等待刷新' ) gps_use=0 #GPS标志位可以使用为0 #weidu_xy=0 #纬度 #jingdu_xy=0 #经度 time_= int (time.time()) ShareImages[1]=gps_use ShareImages[2]=weidu_xy ShareImages[3]=jingdu_xy ShareImages[4]=high ShareImages[5]=time_ else : # if gps_debug==1: print('当前卫星定位无效,GPS串口没有数据') #gps_use=0 #GPS标志位可以使用为0 #weidu_xy=0.0#纬度 #jingdu_xy=0.0#经度 #high=0.0 #海拔高 #id_=000000 #帧号 time_= int (time.time()) #其他数据不更新 维持原样 ShareImages[1]=gps_use ShareImages[2]=weidu_xy ShareImages[3]=jingdu_xy ShareImages[4]=high ShareImages[5]=time_ from multiprocessing import Process,Manager if __name__ == '__main__' : #1---初始化共享内存数据 lock = Manager().Lock()#创建共享内存容器 ShareImages=Manager().dict()#存str类型数据 ShareImages[1]=0 # "是否可用" ShareImages[2]=0.0# "经度" ShareImages[3]=0.0# "纬度" ShareImages[4]=0.0# "高度" ShareImages[5]=0# "时间" ShareImages[9]=0#用于关闭所有进程 #gps_Get_data(ShareImages,lock) #3采集GPS p_gps = Process(target=gps_Get_data, args=(ShareImages, lock ,1))#开启进程 p_gps.deamon=True #伴随主进程关闭而关闭,但是有时候还是关闭不了,单独搞个标志位来控制 p_gps.start()#开始 sharelib.Set_ImgFalg_(0) #允许发送 t1=time.time() cnt=0 while 1: if sharelib.Get_ImgFlag_()==0: #发送gps数据-结构体模式 SetGPSToShare_struct(struct_gps, int (ShareImages[1]), "gps msg" ,ShareImages[2],ShareImages[3],ShareImages[4],ShareImages[5]) sharelib.Set_ImgFalg_(1)#存图结束,允许读图 #time.sleep(1) t2=time.time() if (t2-t1)>cnt: cnt=cnt+1 print( "有效" , int (ShareImages[1]), "纬度" , float (ShareImages[2]), "经度" , float (ShareImages[3]), "高度" , float (ShareImages[4]), "时间戳" ,ShareImages[5]) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗