安装相应的python包, apt-get install python-tk,apt-get install python-serial,pip install image.
代码如下
#encoding=utf-8
from Tkinter import *
import tkMessageBox
from PIL import Image, ImageTk
from serial import *
import threading
import time
import datetime
from decimal import Decimal
'''
self.j=1
if self.j==1:
data="$GPGSV,3,1,10,18,84,067,23,09,67,067,27,22,49,312,28,15,47,231,30*70 "
if self.j==2:
data="$GPGSV,3,2,10,21,32,199,23,14,25,272,24,05,21,140,32,26,14,070,20*7E "
if self.j==3:
data="$GPGSV,3,3,10,29,07,074,,30,07,163,28*7D "
if self.j==4:
data="$GPRMC,025620.20,A,2602.33722,N,11911.49176,E,0.100,,281211,,,D*79 "
if self.j==5:
data="$GPGGA,121253.000,3937.3090,N,11611.6057,E,1,06,1.2,44.6,M,-5.7,M,,0000*72 "
if self.j==6:
data="$GPVTG,359.95,T,,M,15.15,N,28.0,K,A*04 "
if self.j==7:
data="$GPGSA,A,3,14,15,05,22,18,26,,,,,,,2.1,1.2,1.7*3D "
self.j=self.j+1
if(self.j==8):
self.j=1;
'''
class GUI(Frame):
def __init__(self, frame=None):
Frame.__init__(self, frame)
self.ser = Serial()
self.ser.baudrate=9600
self.ser.port='/dev/ttyUSB0'
self.gprmc_item=['UTC时间','定位状态','纬度','纬度半球','经度','经度半球','地面速率','地面航向', 'UTC日期','磁偏角','磁偏角方向']
self.gpgga_item=['UTC时间','纬度','纬度半球','经度','经度半球','定位状态','定位使用卫星','HDOP水平精度因子', '海拔高度','高度','差分时间','差分站ID']
self.gpgsa_item=['模式','定位类型','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','卫星编号','PDOP位置精度因子','HDOP水平精度因子','VDOP垂直精度因子']
self.gpgsv_item_one=['GSV语句总数','GSV编号','可见卫星','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比']
self.gpgsv_item_two=['GSV语句总数','GSV编号','可见卫星','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比']
self.gpgsv_item_three=['GSV语句总数','GSV编号','可见卫星','卫星编号','卫星仰角','卫星方位角','信噪比','卫星编号','卫星仰角','卫星方位角','信噪比']
self.gpvtg_item=['地面航向(真北)','地面航向(地磁)','地面速率(节)','地面速率(km/h)']
self.gprmc_value=['','','','','','','','', '','','','']
self.gpgga_value=['','','','','','','','', '','','','']
self.gpgsa_value=['','','','','','','','', '','','','','','','','','', '','','']
self.gpgsv_value_one=['','','','','','','','', '','','','','','','','','','','', '','']
self.gpgsv_value_two=['','','','','','','','', '','','','','','','','','','','', '','']
self.gpgsv_value_three=['','','','','','','','', '','','','','','','','','','','', '','']
self.gpvtg_value=['','','','','','','','', '','','','']
for i in range(0,len(self.gprmc_item)):
self.gprmc_value[i]=StringVar()
Label(frame,relief="flat",text=self.gprmc_item[i], width=10).grid(row = i,column = 0,sticky = W)
Label(frame,relief="groove",textvariable=self.gprmc_value[i], width=13).grid(row = i,column = 1,sticky = W)
for i in range(0,len(self.gpgga_item)):
self.gpgga_value[i]=StringVar()
Label(frame,relief="flat",text=self.gpgga_item[i], width=15).grid(row = i,column = 2,sticky = W)
Label(frame,relief="groove",textvariable=self.gpgga_value[i], width=10).grid(row = i,column = 3,sticky = W)
for i in range(0,len(self.gpgsa_item)):
self.gpgsa_value[i]=StringVar()
Label(frame,relief="flat",text=self.gpgsa_item[i], width=15).grid(row = i,column = 4,sticky = W)
Label(frame,relief="groove",textvariable=self.gpgsa_value[i], width=8).grid(row = i,column = 5,sticky = W)
for i in range(0,len(self.gpvtg_item)):
self.gpvtg_value[i]=StringVar()
Label(frame,relief="flat",text=self.gpvtg_item[i], width=13).grid(row = i,column = 6,sticky = W)
Label(frame,relief="groove",textvariable=self.gpvtg_value[i], width=8).grid(row = i,column = 7,sticky = W)
for i in range(0,len(self.gpgsv_item_one)):
self.gpgsv_value_one[i]=StringVar()
Label(frame,relief="flat",text=self.gpgsv_item_one[i], width=10).grid(row = i,column = 8,sticky = W)
Label(frame,relief="groove",textvariable=self.gpgsv_value_one[i], width=8).grid(row = i,column = 9,sticky = W)
for i in range(0,len(self.gpgsv_item_two)):
self.gpgsv_value_two[i]=StringVar()
Label(frame,relief="flat",text=self.gpgsv_item_two[i], width=10).grid(row = i,column = 10,sticky = W)
Label(frame,relief="groove",textvariable=self.gpgsv_value_two[i], width=8).grid(row = i,column = 11,sticky = W)
for i in range(0,len(self.gpgsv_item_three)):
self.gpgsv_value_three[i]=StringVar()
Label(frame,relief="flat",text=self.gpgsv_item_three[i], width=10).grid(row = i,column = 12,sticky = W)
Label(frame,relief="groove",textvariable=self.gpgsv_value_three[i], width=8).grid(row = i,column = 13,sticky = W)
def analysisgpsdata(self,data):
list=data.split(',')
if list[0]=='$GPGSA':
for i in range(0,len(self.gpgsa_item)):
if i==0:
if(cmp(list[1],'A')==0):
list[1]='自动'
else:
list[1]='手动'
if i==1:
if(cmp(list[2],'1')==0):
list[2]='未定位'
if(cmp(list[2],'2')==0):
list[2]='二维定位'
if(cmp(list[2],'3')==0):
list[2]='三维定位'
if i==len(self.gpgsa_item)-1:
tmp_list=list[len(self.gpgsa_item)].split('*')
list[len(self.gpgsa_item)]=tmp_list[0]
self.gpgsa_value[i].set(list[i+1])
elif list[0]=='$GPGSV':
if(cmp(list[2],'1')==0):
for i in range(0,len(self.gpgsv_item_one)):
if i==(len(self.gpgsv_item_one)-1):
tmp_list=list[i+1].split('*')
list[i+1]=tmp_list[0]
self.gpgsv_value_one[i].set(list[i+1])
if(cmp(list[2],'2')==0):
for i in range(0,len(self.gpgsv_item_two)):
if i==(len(self.gpgsv_item_two)-1):
tmp_list=list[i+1].split('*')
list[i+1]=tmp_list[0]
self.gpgsv_value_two[i].set(list[i+1])
if(cmp(list[2],'3')==0):
for i in range(0,len(self.gpgsv_item_three)):
if i==(len(self.gpgsv_item_three)-1):
tmp_list=list[i+1].split('*')
list[i+1]=tmp_list[0]
self.gpgsv_value_three[i].set(list[i+1])
elif list[0]=='$GPGGA':
for i in range(0,len(self.gpgga_item)):
if i==0:
hour=int(float(list[1])/10000)
hour=hour+8
if hour>24:
hour=hour-24
list[1]=str(hour)+':'+str(int(float(list[1])%10000/100))+':'+str(int(float(list[1])%100))
if i==1:
du=float(list[2])//100
du=du+(float(list[2])-du*100)/float(60)
du=Decimal(du).quantize(Decimal('0.00000'))
list[2]=du
if i==3:
du=float(list[4])//100
du=du+(float(list[4])-du*100)/float(60)
du=Decimal(du).quantize(Decimal('0.00000'))
list[4]=du
if i==5:
if(cmp(list[6],'0')==0):
list[6]='未定位'
if(cmp(list[6],'1')==0):
list[6]='非差分定位'
if(cmp(list[6],'2')==0):
list[6]='差分定位'
if(cmp(list[6],'6')==0):
list[6]='正在估算'
if i==9:
list[i+1]=list[i+1+1]
if i==(len(self.gpgga_item)-2):
list[i+1]=list[i+3]
if i==(len(self.gpgga_item)-1):
tmp_list=list[i+3].split('*')
list[i+1]=tmp_list[0]
self.gpgga_value[i].set(list[i+1])
elif list[0]=='$GPRMC':
for i in range(0,len(self.gprmc_item)):
if i==0:
hour=int(float(list[1])/10000)
hour=hour+8
if hour>24:
hour=hour-24
list[1]=str(hour)+':'+str(int(float(list[1])%10000/100))+':'+str(int(float(list[1])%100))
if i==1:
if(cmp(list[2],'A')==0):
list[2]='有效'
else:
list[2]='无效'
if i==2:
du=float(list[3])//100
du=du+(float(list[3])-du*100)/float(60)
du=Decimal(du).quantize(Decimal('0.00000'))
list[3]=du
if i==4:
du=float(list[5])//100
du=du+(float(list[5])-du*100)/float(60)
du=Decimal(du).quantize(Decimal('0.00000'))
list[5]=du
if i==8:
date=int(list[i+1])
list[i+1]=str(2000+date%100%100)+'年'+str(date%10000//100) +'月'+str(date//10000)+'日'
self.gprmc_value[i].set(list[i+1])
elif list[0]=='$GPVTG':
for i in range(0,len(self.gpvtg_item)):
if i==1:
list[i+1]=list[i+2]
if i==2:
list[i+1]=list[i+3]
if i==3:
list[i+1]=list[i+4]
self.gpvtg_value[i].set(list[i+1])
else:
print 'unkown data'
print data
print list
def getgpsdata(self):
serialdata = self.ser.readline()
self.analysisgpsdata(serialdata);
self._timer = self.after(1, self.getgpsdata)
def open(self):
try:
self.ser.open()
except Exception, e:
print 'serial open err'
if self.ser.is_open==True:
print(self.ser)
self.getgpsdata()
def close(self):
self.showSerial.delete(0.0,END)
self.ser.close()
self.showSerial.insert(0.0,"Serial close ok")
class ITEM_BAR(object):
def __init__(self,parent):
self.root=parent;
option = Menu(self.root)
self.root.config(menu=option)
filemenu = Menu(option,tearoff=0)
option.add_cascade(label="选项", menu=filemenu)
filemenu.add_command(label="分析数据", command=self.analyze_data)
filemenu.add_command(label="退出", command=self.root.quit)
helpmenu = Menu(option,tearoff=0)
option.add_cascade(label="帮助", menu=helpmenu)
helpmenu.add_command(label="关于软件", command=self.about)
def analyze_data(self):
GUI().open()
def about(self):
tkMessageBox.showinfo("关于软件", "http://blog.csdn.net/mike8825/")
root = Tk()
root.title("GPS Analyzer")
root.resizable(False, False)
app = GUI(root)
ITEM_BAR(root)
root.mainloop()
效果图