#!/usr/bin/env pythonimport pylabfrom pylab import *import Tkinterfrom Tkinter import *import serialfrom numpy import *from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAggroot = Tkinter.Tk()root.wm_title("Chocolate Oscilloscope (By Wenzhe_Qiu)")xAchse=pylab.arange(0,100,1)yAchse=pylab.array([0]*100)fig = pylab.figure(1)ax = fig.add_subplot(111)ax.grid(True)ax.set_title("Chocolate Oscilloscope Frontend (COM4)")ax.set_xlabel("Time (ms)")ax.set_ylabel("Amplitude (V)")ax.axis([0,100,-1.5,1.5])line1=ax.plot(xAchse,yAchse,'-')canvas = FigureCanvasTkAgg(fig, master=root)canvas.show()canvas.get_tk_widget().pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)toolbar = NavigationToolbar2TkAgg( canvas, root )toolbar.update()canvas._tkcanvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)values=[]values = [0 for x in range(100)]run=1sampling_freq=1000sampling_time=float(1000/float(sampling_freq))NumberSamples=100 #s=serial.Serial(port='COM4') #open port COM4s=serial.Serial(port='COM4', baudrate=115200)#Receive data by pc.putc()def waveformGenerator(): global values _next=s.readline() _next=float(_next)/1000 if(run==1): if(_next<=1): _next=3.3 * _next values.append(_next) root.after(0,waveformGenerator) #def RealtimePloter():# global values,wScale,NumberSamples,sampling_time# NumberSamples=min(len(values),wScale.get())# real_time=NumberSamples*sampling_time;# interval=sampling_time# CurrentXAxis=linspace(0,real_time,NumberSamples)# line1[0].set_data(CurrentXAxis,pylab.array(values[-NumberSamples:]))# ax.axis([0,real_time,-0.5,4])# canvas.draw()# root.after(25,RealtimePloter)def RealtimePloter(): global values,wScale,sampling_freq,interval NumberSamples=min(len(values),wScale.get()*sampling_freq/1000) interval=float(float (wScale.get())/float (NumberSamples)) CurrentXAxis=linspace(0,wScale.get(),NumberSamples) line1[0].set_data(CurrentXAxis,pylab.array(values[-NumberSamples:])) ax.axis([0,wScale.get(),-0.5,4]) canvas.draw() root.after(25,RealtimePloter) def _quit(): print "Bye" s.close() # close the port root.quit() # stops mainloop root.destroy() # this is necessary on Windows to prevent # Fatal Python Error: PyEval_RestoreThread: NULL tstatedef _set(): global sampling_time s.write(str(wScale_sample_freq.get())+'\n') sampling_freq=wScale_sample_freq.get() sampling_time=float(float(1/float(sampling_freq))*1000) print "The current sampling frequency is " + str(wScale_sample_freq.get()) + "Hz !" print "The sampling time interval is " + str(sampling_time) + " ms" def _PS(): s.write("2000\n")def _STOP_IMAGE(): global run run=~rundef _channel_change_to_15(): s.write("1010\n") print"The current channel is 15 !"def _channel_change_to_16(): s.write("1020\n") print"The current channel is 16 !"def _channel_change_to_17(): s.write("1030\n") print"The current channel is 17 !"def _channel_change_to_18(): s.write("1040\n") print"The current channel is 18 !"def _channel_change_to_19(): s.write("1050\n") print"The current channel is 19 !"def _channel_change_to_20(): s.write("1060\n") print"The current channel is 20 !"########## frame_widget_definition #########frame_1 = Frame(height=10, bd=1, relief=SUNKEN)frame_2 = Frame(height=10, bd=1, relief=GROOVE)frame_3 = Frame(master=frame_2, height=10, bd=1, relief=GROOVE)#relief:Border decoration. The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE#frame widget reference:http://effbot.org/tkinterbook/frame.htmframe_1.pack(side=Tkinter.LEFT)frame_2.pack(side=Tkinter.RIGHT)frame_3.pack(side=Tkinter.BOTTOM)########## button_widget_definition #########button = Tkinter.Button(master=frame_3, text=' SET ', command=_set)button_PS = Tkinter.Button(master=frame_3, text=' PAUSE/START CHANNEL ', command=_PS)button_SI = Tkinter.Button(master=frame_3, text=' STOP/RUN IMAGE ', command=_STOP_IMAGE)button_quit = Tkinter.Button(master=frame_3, text=' QUIT ', command=_quit)button.pack(side=Tkinter.LEFT)button_PS.pack(side=Tkinter.LEFT)button_SI.pack(side=Tkinter.LEFT)button_quit.pack(side=Tkinter.RIGHT)########## wScale_widget_definition #########wScale = Tkinter.Scale(master=frame_2,label="Time Scale (ms):", from_=1, to=1000,sliderlength=30,length=ax.get_frame().get_window_extent().width, orient=Tkinter.HORIZONTAL)wScale_sample_freq = Tkinter.Scale(master=frame_2,label="Sampling Frequency (Hz): (Click SET button for comfirmation)", from_=3, to=1000,sliderlength=30,length=ax.get_frame().get_window_extent().width, orient=Tkinter.HORIZONTAL)wScale.pack(side=Tkinter.TOP)wScale_sample_freq.pack(side=Tkinter.TOP)wScale.set(500)wScale_sample_freq.set(1000)########## radiobutton_widget_definition #########v = IntVar()v.set(1)print "The default channel is 15 !"Radiobutton(frame_1, text="Channel 15", variable=v, value=1, command=_channel_change_to_15).pack(anchor=W)Radiobutton(frame_1, text="Channel 16", variable=v, value=2, command=_channel_change_to_16).pack(anchor=W)Radiobutton(frame_1, text="Channel 17", variable=v, value=3, command=_channel_change_to_17).pack(anchor=W)Radiobutton(frame_1, text="Channel 18", variable=v, value=4, command=_channel_change_to_18).pack(anchor=W)Radiobutton(frame_1, text="Channel 19", variable=v, value=5, command=_channel_change_to_19).pack(anchor=W)Radiobutton(frame_1, text="Channel 20", variable=v, value=6, command=_channel_change_to_20).pack(anchor=W)# radiobutton widget reference:http://effbot.org/tkinterbook/radiobutton.htmroot.after(100,waveformGenerator)root.after(100,RealtimePloter)Tkinter.mainloop()