Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带有tkinter GUI和matplotlib的Arduino(pySerial)_Python_Matplotlib_Tkinter_Arduino_Pyserial - Fatal编程技术网

Python 带有tkinter GUI和matplotlib的Arduino(pySerial)

Python 带有tkinter GUI和matplotlib的Arduino(pySerial),python,matplotlib,tkinter,arduino,pyserial,Python,Matplotlib,Tkinter,Arduino,Pyserial,我试图显示一些用Arduino获得的读数(在这个例子中,我只是在玩电位计)。问题在于,我的程序无法检测锅的变化,从而导致在图形上连续显示。有没有办法解决这个问题,或者我需要给它另一种方法?下面是我创建的用于显示数据的类 data = np.array([]) cond = False def plot_start(): global cond cond = True def plot_stop(): global cond cond = False class Win3: def __i

我试图显示一些用Arduino获得的读数(在这个例子中,我只是在玩电位计)。问题在于,我的程序无法检测锅的变化,从而导致在图形上连续显示。有没有办法解决这个问题,或者我需要给它另一种方法?下面是我创建的用于显示数据的类

data = np.array([])
cond = False

def plot_start():
global cond
cond = True


def plot_stop():
global cond
cond = False

class Win3:
def __init__(self, master):
    self.master = master
    self.master.resizable(0, 0)
    self.master.title('Anxiety test')
    self.master.geometry('1920x1080')
    # ===== Image background =====
    imageLogin = PhotoImage(file = 'testGUI.png')
    self.w = w = tk.Label(master, image = imageLogin)
    w.imageLogin = imageLogin
    # ===== Place of the elements =====
    w.pack(pady = 0, padx = 0)
    self.createWidgets()

# ===== Arduino reader =====
def plot_data(self):
    global cond, data
    if cond:
        a = self.s.readline()
        a.decode()
        if len(data) < 100:
            data = np.append(data, float(a[0:4]))
        else:
            data[0:99] = data[1:100]
            data[99] = float(a[0:4])
        self.linesx.set_xdata(np.arange(0, len(data)))
        self.linesx.set_ydata(data)
        self.canvas.draw()
    self.master.after(1, self.plot_data)

# ===== Figures for plot =====
def createWidgets(self):
    fig = Figure(figsize = (15, 5))
    ax = fig.add_subplot(311)
    ay = fig.add_subplot(312)
    az = fig.add_subplot(313)
    # ===== Labels and grids =====
    ax.set_ylabel('Voltage1 [V]')
    ay.set_ylabel('Voltage2 [V]')
    az.set_ylabel('Voltage3 [V]')
    ax.grid()
    ay.grid()
    az.grid()
    # ===== Axis limits =====
    ax.set_xlim(0, 150)
    ax.set_ylim(0, 1024)
    ay.set_xlim(0, 150)
    az.set_xlim(0, 150)

    fig.tight_layout()

    # ===== Lines to plot =====
    self.linesx = ax.plot([], [])[0]
    self.linesy = ay.plot([], [])[0]
    self.linesz = az.plot([], [])[0]

    canvas = FigureCanvasTkAgg(fig, master = self.master)
    canvas.get_tk_widget().place(x = 170, y = 550, width = 1700, height = 450)
    canvas.draw()
    self.canvas = canvas
    # ===== Buttons to start or stop plots =====
    self.master.update()
    start = tk.Button(self.master, text = 'Start', font = ('Verdana', 12), width = 12, command = lambda: plot_start())
    start.place(x = 250, y = 480)
    self.master.update()
    stop = tk.Button(self.master, text = 'Stop', font = ('Verdana', 12), width = 12, command = lambda: plot_stop())
    stop.place(x = start.winfo_x() + start.winfo_reqwidth() + 20, y = 480)
    # ===== Arduino =====
    self.s = sr.Serial('COM1', 9600)
    self.s.reset_input_buffer()
    self.master.after(1, self.plot_data)
data=np.array([])
cond=False
def plot_start():
全局条件
cond=真
def plot_stop():
全局条件
cond=False
Win3类:
定义初始(自我,主):
self.master=master
self.master.reshable(0,0)
self.master.title(“焦虑测试”)
自主几何学('1920x1080')
#====图像背景=====
imageLogin=PhotoImage(文件='testGUI.png')
self.w=w=tk.Label(master,image=imageLogin)
w、 imageLogin=imageLogin
#=======图元的位置=====
w、 包装(pady=0,padx=0)
self.createWidgets()
#======Arduino阅读器=====
def plot_数据(自身):
全球条件、数据
如果条件:
a=self.s.readline()
a、 解码()
如果len(数据)<100:
data=np.append(数据,浮点(a[0:4]))
其他:
数据[0:99]=数据[1:100]
数据[99]=浮点(a[0:4])
self.linesx.set_扩展数据(np.arange(0,len(数据)))
self.linesx.set_ydata(数据)
self.canvas.draw()
self.master.after(1,self.plot_数据)
#======用于绘图的数字=====
def createWidgets(自):
fig=图(figsize=(15,5))
ax=图添加子批次(311)
ay=图添加_子批次(312)
az=图添加_子批次(313)
#====标签和网格=====
ax.set_ylabel('Voltage1[V]”)
ay.set_ylabel('Voltage2[V]”)
az.set_ylabel(“电压3[V]”)
ax.grid()
ay.grid()
az.grid()
#=======轴限制=====
ax.set_xlim(0,150)
ax.set_ylim(0,1024)
ay.set_xlim(0,150)
az.set_xlim(0,150)
图1紧_布局图()
#======要打印的行=====
self.linesx=ax.plot([],[])[0]
self.linesy=ay.plot([],[])[0]
self.linesz=az.plot([],[])[0]
canvas=FigureCanvasTkAgg(图,master=self.master)
canvas.get_tk_widget().place(x=170,y=550,width=1700,height=450)
canvas.draw()
self.canvas=画布
#====开始或停止绘图的按钮=====
self.master.update()
start=tk.Button(self.master,text='start',font=('Verdana',12),width=12,command=lambda:plot_start())
起始位置(x=250,y=480)
self.master.update()
stop=tk.Button(self.master,text='stop',font=('Verdana',12),width=12,command=lambda:plot_stop())
stop.place(x=start.winfo_x()+start.winfo_reqwidth()+20,y=480)
#=======Arduino=====
self.s=sr.Serial('COM1',9600)
自复位输入缓冲区()
self.master.after(1,self.plot_数据)

不清楚您在问什么。。。至少对meMy来说,问题是当移动罐子时,我的情节没有变化;就好像它读取的第一个值一直在重复,尽管我一直在更改pot值。谢谢你的回复!我认为您可能需要使用
linesx.set_data(x,y)
,而不是分别设置每个数据。如果单独设置,但更改数据点的长度,则不会更新