while True:
print inst.ask('channel')
time.sleep(1)
然后,您可以 ctrl-c 在您认为合适时停止循环。
上面的脚本很简单——它只是在屏幕上显示数字,直到你杀死它。我发现使用 matplotlib 实时绘制来自 PyVISA 的数据很有用。我发现这在 pyplot 模式下有问题(当我关闭交互模式时,我有很多空白屏幕,ymmv)所以我将它嵌入到 tkinter 窗口中,如下所示:
import matplotlib
matplotlib.use('TkAgg') # this has to go before the other imports
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import Tkinter as Tk
import visa
# set up a PyVISA instrument and a list for the data
data = []
keithley = visa.instrument('GPIB0::whatever')
# make a Tkinter window
root = Tk.Tk()
# add a matplotlib figure to the Tk window
fig = Figure()
ax = fig.add_subplot(111)
canv = FigureCanvasTkAgg(fig, master=root)
canv.show()
canv.get_tk_widget().pack(fill='both', expand=True)
# a function that is called periodically by the event loop
def plot_update():
# add a new number to the data
data.append(keithley.ask('SCPI:COMM:AND?'))
# replot the data in the Tk window
ax.clear()
ax.plot(data)
fig.tight_layout()
canv.draw()
# wait a second before the next plot
root.after(1000, plot_update)
root.after(1000, plot_update)
root.mainloop()
它可能看起来不多,但我们逐渐将这样的短脚本开发成一个相当强大的仪器控制程序;)