PyVisa and Printing New Data

1.4k Views Asked by At

I am trying to use Pyvisa to capture data from one of my channels of my Keithly 2701 DMM.

I get a static onetime response via temp = keithly.ask('SCPI COmmand'), but what I want to do is constantly print new data without setting any predefined size, i.e capture 300 data points.

I want to determine when to stop the capture if I have seen a trend over 10000 data points, or, in another experiment, I might see a trend after 2500 data points.

from pylab import *
from visa import instrument

inst = SerialInstument(args)

while new data:
    print inst.aks('channel')
1

There are 1 best solutions below

0
On BEST ANSWER
while True:
    print inst.ask('channel')
    time.sleep(1)

You can then ctrl-c to stop the loop when you see fit.

The above script is simple - it just puts numbers on the screen until you kill it. I find it useful to plot the data from PyVISA in real time, using matplotlib. I found this to be buggy in pyplot mode (I got lots of blank screens when I turned off interactive mode, ymmv) so I embedded it into a tkinter window, as follows:

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()

It may not seem like much, but we gradually developed a short script like this into a rather capable instrument control program ;)