Python Forum
Help with applying this instrument monitoring code to multiple inputs.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with applying this instrument monitoring code to multiple inputs.
#2
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

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
Reply


Messages In This Thread
RE: Help with applying this instrument monitoring code to multiple inputs. - by Kid_Meier - Mar-04-2020, 12:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple variable inputs when only one is called for ChrisDall 2 493 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,649 May-07-2023, 12:33 PM
Last Post: deanhystad
  Code freez if more inputs kiko058 2 814 Mar-28-2023, 03:52 PM
Last Post: kiko058
  python multiple try except block in my code -- can we shorten code mg24 10 6,166 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  gspread - applying ValueRenderOption to a range of cells dwassner 0 1,701 Jan-12-2022, 03:05 PM
Last Post: dwassner
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 2,823 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Generate Multiple sql Files With csv inputs vkomarag 13 4,234 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  monitoring the temperature of the CPU with Python apollo 2 8,763 Apr-13-2021, 05:39 PM
Last Post: apollo
  IoT Air Monitoring System project_science 0 1,936 Mar-26-2021, 08:14 PM
Last Post: project_science
  Applying function mapypy 1 2,273 Mar-11-2021, 09:49 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020