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 |
#encoding=utf-8 __author__ = 'ggrarea' from threading import * from time import * from tkinter import * from serial import * import tkinter.ttk as ttk from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure class GUI(Frame): def __init__(self,master): # 构造串口的属性 self.x = [0.0] self.y = [0.0] self.alive = False frame = Frame(master) frame.pack() #串口设置相关变量 self.port = 'COM12' self.baudrate = 115200 #串口号提示 self.lab1 = Label(frame,text = 'Serial Number') self.lab1.grid(row = 0,column = 0,sticky = W) #串口号选择下拉菜单 self.boxValue = StringVar() self.boxChoice = ttk.Combobox(frame,textvariable = self.boxValue,state = 'readonly') self.boxChoice['value'] = ('COM1','COM2','COM3','COM4','COM5','COM6','COM7','COM8','COM12') self.boxChoice.current(8) self.boxChoice.bind('<<ComboboxSelected>>',self.Choice) self.boxChoice.grid(row = 1,column = 0,sticky = W) #波特率选择提示 self.lab2 = Label(frame,text = 'Baudrate Set') self.lab2.grid(row = 2,column = 0,sticky = W) #波特率选择下拉菜单 self.boxValueBaudrate = IntVar() self.BaudrateChoice = ttk.Combobox(frame,textvariable = self.boxValueBaudrate,state = 'readonly') self.BaudrateChoice['value'] = (9600,115200) self.BaudrateChoice.current(1) self.BaudrateChoice.bind('<<ComboboxSelected>>',self.ChoiceBaudrate) self.BaudrateChoice.grid(row = 3,column = 0,sticky = W) #输出框提示 self.lab3 = Label(frame,text = 'Message Show') self.lab3.grid(row = 0,column = 1,sticky = W) #输出框 self.show = Text(frame,width = 40,height = 5,wrap = WORD) self.show.grid(row = 1,column = 1,rowspan = 4,sticky = W) #输入框提示 self.lab4 = Label(frame,text = 'Input here,please!') self.lab4.grid(row = 5,column = 1,sticky = W) #输入框 self.input = Entry(frame,width = 40) self.input.grid(row = 6,column = 1,rowspan = 4,sticky = W) #输入按钮 self.button1 = Button(frame,text = "发送",command = self.Submit) self.button1.grid(row = 11,column = 1,sticky = E) #串口开启按钮 self.button2 = Button(frame,text = 'Open Serial',command = self.open) self.button2.grid(row = 7,column = 0,sticky = W) #串口关闭按钮 self.button3 = Button(frame,text = 'Close Serial',command = self.close) self.button3.grid(row = 10,column = 0,sticky = W) #串口信息提示框 self.showSerial = Text(frame,width = 20,height = 2,wrap = WORD) self.showSerial.grid(row = 12,column = 0,sticky = W) #串口初始化配置 self.ser = Serial() self.ser.setPort(self.port) self.ser.port = self.port self.ser.baudrate = self.baudrate #增加的GUI图表界面*************************** self.f = Figure(figsize=(5, 4), dpi=100) self.canvas = FigureCanvasTkAgg(self.f, master=frame) self.canvas.show() #GUI.canvas.grid(row=13) # 第0行,占用3列 self.canvas.get_tk_widget().grid(row=13, columnspan=3) #self.ser.setBaudrate(self.baudrate) #self.ser.open() #print self.ser.isOpen() #print self.ser def Choice(self,event): context = self.boxValue.get() list = ['COM1','COM2','COM3','COM4','COM5','COM6','COM7','COM8','COM12'] if context in list: self.port = context self.ser.setPort(self.port) #self.ser.timeout = 2 #设置等待时间,若超出这停止等待 print (self.port) def ChoiceBaudrate(self,event): self.baudrate = self.boxValueBaudrate.get() #self.ser.setBaudrate(self.baudrate) self.ser.baudrate = self.baudrate print (self.baudrate) def Submit(self): context1 = self.input.get() self.ser.write(bytes(context1, encoding="utf8")) #output = self.ser.read(n) #print (output) #self.show.delete(0.0,END)#清除显示 #self.show.insert(0.0,output) def FirstReader(self): data ="" while self.alive: time.sleep(0.1) n = self.ser.in_waiting if n: newdata = self.ser.read(n).decode('utf-8','ignore'); data = data + newdata dataend = data.rfind('\r\n'); datastart = data.rfind('Roll=', 0, dataend) + len('Roll='); if dataend != -1: # 有数据 roll = float(data[datastart: dataend]); self.x.append(self.x[-1]+1) self.y.append(roll) print('get roll from serial port:', roll) self.f.clf() self.a = self.f.add_subplot(111) self.a.plot(self.x, self.y, label="val", color='r') leg = self.a.legend(loc='upper right', ncol=1, mode="None", shadow=True, fancybox=True) leg.get_frame().set_alpha(0) self.a.set_title('roll') self.canvas.show() self.show.delete(0.0, END) # 清除显示 self.show.insert(END, data) def open(self): self.ser.open() if self.ser.isOpen() == True: self.showSerial.delete(0.0,END) self.showSerial.insert(0.0,"Serial has been opend!") self.alive = True self.thread_read = Thread(target=self.FirstReader) self.thread_read.setDaemon(True) #守护线程 self.thread_read.start() def close(self): #self.thread_read. self.alive = False #关闭当前线程 # join的作用是保证当前线程执行完成后,再执行其它线程。join可以有timeout参数,表示阻塞其它线程timeout秒后,不再阻塞。详见官方文档。 self.thread_read.join() if self.ser.isOpen() == True: self.ser.close() if self.ser.isOpen() == False: self.showSerial.delete(0.0,END) self.showSerial.insert(0.0,"Serial has been closed!") root = Tk() root.title("Serial GUI") #root.geometry("3000x4000") app = GUI(root) root.mainloop() |
这是检测偏航角yaw的测试数据,确实yaw会随着时间漂移。大概20分钟会漂移3~5°的样子,而且是累计增加。。。。。
转载请注明:徐自远的乱七八糟小站 » 【原创】PYTHON上位机三轴输出波形检测