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()
大したことではないように思えるかもしれませんが、私たちはこのような短いスクリプトを徐々に開発して、かなり有能な計測器制御プログラムにしました ;)