Mar-04-2020, 12:01 PM
Okay, this is how far I've gotten (see below).
I decided to make a Class called "Instrument" that initializes with the instrumentID, the name string I want for the instrument, and empty lists for x-y coords for the plot. It also takes in the necessary terminator string which is different for a few of the gauges I have, as well as a type (manufacturer) identifier to tell it what comms protocol to use, because that, again, is different between the different gauge types.
The Instrument class also has an update method which does the pressure reading query, adds the new data to the plot, and writes it to the log file, preceded by a tab. This way, in the animate loop, I can write the current time to the log file, then iterate through a list of all the gauges I have active, running the update and then plotting.
I also made an init function for the animate loop that writes the header line I wanted, using the name property from each Instrument.
Comments would be greatly appreciated.
Thanks,
Mike
I decided to make a Class called "Instrument" that initializes with the instrumentID, the name string I want for the instrument, and empty lists for x-y coords for the plot. It also takes in the necessary terminator string which is different for a few of the gauges I have, as well as a type (manufacturer) identifier to tell it what comms protocol to use, because that, again, is different between the different gauge types.
The Instrument class also has an update method which does the pressure reading query, adds the new data to the plot, and writes it to the log file, preceded by a tab. This way, in the animate loop, I can write the current time to the log file, then iterate through a list of all the gauges I have active, running the update and then plotting.
I also made an init function for the animate loop that writes the header line I wanted, using the name property from each Instrument.
Comments would be greatly appreciated.
Thanks,
Mike
import visa from datetime import datetime import time import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation class Instrument: def __init__(self, InstrumentIdentifierString, InstrumentName, terminator, type): global rm self.title = InstrumentName self.type = type self.ID = rm.open_resource(InstrumentIdentifierString) self.ID.read_termination = terminator self.ID.write_termination = terminator self.x = [] self.y = [] def update(self): global logfile tempstr = "" counter = 0 while len(tempstr)<1: if self.type == "Pfeiffer": self.ID.query('PR1') tempstr = self.ID.query('\x05') elif self.type == "Jevatec": tempstr = self.ID.query('RPV\x2C1') counter += 1 if counter = 10: tempstr = " ,'----------" tempstr = tempstr.split(',')[1] self.x.append(datetime.now().replace(microsecond=0)) self.y.append(float(tempstr)) logfile.write('\t' + tempstr) def animinit(GaugeList): global logfile global fig global axlist logfile.write("# Time") for iter,instr in enumerate(GaugeList): axlist[0][iter].set_title(instr.title) axlist[0][iter].set_xlabel("Time") axlist[0][iter].set_ylabel("Pressure(mbar)") logfile.write('\t "' + instr.title + '"') logfile.write('\n') logfile.flush() def animate(i, GaugeList): current_time = datetime.now().strftime("%H:%M:%S") logfile.write(current_time) for iter,instr in enumerate(GaugeList): instr.update() axlist[0][iter].clear() axlist[0][iter].plot(instr.x,instr.y) logfile.write('\n') logfile.flush() time.sleep(60) plt.draw() date = datetime.today().strftime('%Y-%m-%d--%Hh-%Mm') + '.txt' logfile = open(date,'a') # Create or open a log file with today's date rm = visa.ResourceManager() # Open up the visa RM that looks for connected instruments GaugeList = [] GaugeList.append(Instrument('ASRL4::INSTR','Analysis Chamber','\r\n','Pfeiffer')) #GaugeList.append(Instrument('ASRL5::INSTR','Detector','\r','Jevatec')) fig,axlist = plt.subplots(1, len(GaugeList),squeeze=False) anim = animation.FuncAnimation(fig, animate, frames=200, init_func=animinit(GaugeList), fargs=(GaugeList,), interval=20) # Do the animation plt.show() # Show the plot for instr in GaugeList: instr.ID.close() logfile.close() raise SystemExit