【发布时间】:2015-07-04 04:33:55
【问题描述】:
我是一个可怜的程序员,所以请原谅我的简单问题。 我正在尝试构建一个从串行接口读取数据并将其显示在屏幕上的小程序。我已经能够在 iPython notebook 和 matplotlib 中做到这一点,并且我已经能够在屏幕上添加按钮来控制进入界面的数据请求: 按钮点击 -> ser.write、ser.read、draw
我现在正在努力设计程序,以便按下按钮将以固定的时间步长开始重复数据收集,直到再次关闭按钮。有人可以帮我画出这样一个程序的草图吗?
到目前为止:
%pylab
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Button
import serial
import binascii
import struct
from time import sleep
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.axis([0, 10, 0, 255])
ser = serial.Serial('COM5', 1000000, timeout=0)
ser.write("0".encode())
sleep(0.1)
if ser.read()== b'\xf1':
print ("Interface is responding")
else:
print ("Interface is NOT responding")
ser.flush()
ser.close()
exit(1)
t = np.linspace(0, 10, 2048)
line1, line2 = plt.plot(t,0*t, t,0*t, lw=2)
def ShowChannel(Channel):
if Channel==1:
ser.write("11".encode())
elif Channel==2:
ser.write("12".encode())
sleep(0.05)
Header = ser.read().decode("utf-8")
# print(Header)
if Header == "1":
data = ser.read(2048)
y = struct.unpack('2048B', data)
# print(y)
if Channel==1:
line1.set_ydata(y)
elif Channel==2:
line2.set_ydata(y)
fig.canvas.draw()
def one(event):
ShowChannel(1)
def two(event):
ShowChannel(2)
axone = plt.axes([0.1, 0.05, 0.1, 0.075])
axtwo = plt.axes([0.21, 0.05, 0.1, 0.075])
axstart = plt.axes([0.40, 0.05, 0.1, 0.075])
axstop = plt.axes([0.51, 0.05, 0.1, 0.075])
bone = Button(axone, '1')
bone.on_clicked(one)
btwo = Button(axtwo, '2')
btwo.on_clicked(two)
按照 cmets 中引用的示例,我添加了以下内容
# Build check button axes
rax = plt.axes([0.7, 0.05, 0.1, 0.1], aspect='equal')
labels = ('go!',)
check = CheckButtons(rax, labels, (False, ))
KeepShowing = False
def func(event):
global KeepShowing
KeepShowing = not KeepShowing
# print(event, KeepShowing)
check.on_clicked(func)
while True:
if KeepShowing:
ShowChannel(1)
sleep(1)
但是底部的循环不是怎么做的。当我用它启动程序时,图形窗口打开,但不显示任何内容。只有当我在 ipython 中中断内核时,屏幕才会生成。
【问题讨论】:
-
您能否编辑您的问题以包含您目前编写的代码?
-
我期待一个
while循环——在散文中,“当按钮状态打开时,(阅读、绘图、等待几秒钟,重复)”。按下按钮将在 ON 和 OFF 之间切换按钮状态。 (或使用单选按钮等本质上是开/关之一。) -
@cphlewis 好吧,我试过了,但没有用。我不知何故不明白回调的概念。当我将程序发送到循环时,整个事情都冻结了,不想再听按钮了。当我手动中断循环时,按钮可以再次使用。
-
checked_buttons 示例甚至不使用回调(显式)。
标签: python button matplotlib