python 采集GPS发送到共享内存
py
# -*- 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])