#!/usr/bin/python ############################################################## # # CommunicateWithVME.py # Handles the communication with the VME crate # Sends commands to the VME crate # # # Michael Rissi 05/2009 # ############################################################# import GlobalVariables import threading import _VME import time import datetime def GetStatus(): print "Trigger thresholds: ", GlobalVariables.TriggerThresholds print "Hex Pattern: ", GlobalVariables.HexPattern print "Majority Level: ",GlobalVariables.MajorityLevel def GetRate(module,channel): _VME.V560_Clear_Scales( module) beginCounter=_VME. V560_Read_Counter( module, channel) GlobalVariables.ServerMessage = "Evaluating rates (2 seconds)..." time.sleep(2) endCounter = _VME. V560_Read_Counter( module, channel) return (endCounter-beginCounter)/2000. def GetRates(module): _VME.V560_Clear_Scales( module) beginCounter =[0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] endCounter = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] rates = ["rates", 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] for i in range(0,16): beginCounter[i]=_VME. V560_Read_Counter( module, i) #GlobalVariables.ServerMessage = str(beginCounter[i]) #GlobalVariables.ServerMessage = "Evaluating rates..." time.sleep(2) for i in range(0,16): endCounter[i] = _VME. V560_Read_Counter( module, i) #GlobalVariables.ServerMessage = str(endCounter[i]) rates[i+1] =(endCounter[i]-beginCounter[i])/2000. return rates class GimmeRates(threading.Thread): rates=["rates", 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] module = 1 def run(self): oldcommand=GlobalVariables.UserInput while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"): time.sleep(0.001) newrates = GetRates(self.module) self.rates=newrates class WriteOutput(threading.Thread): #thread that writes the rates and threshold to a file. rates=["rates", 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0] interval = 2 filename = "test.txt" #gimmeRates =GimmeRates() gimmeRates=GimmeRates() #File=open(filename,'a') def __init__(self, filename="test.txt", interval = 10): threading.Thread.__init__(self) #self.OpenNewFile(filename) def writeToFile(self): File = open(self.filename,'a') #print "writing", (GlobalVariables.Rates) #year = datetime.date.today().year #month = datetime.date.today().month #day = datetime.date.today().day year = time.localtime(time.time())[0] month =time.localtime(time.time())[1] day = time.localtime(time.time())[2] hour = time.localtime(time.time())[3] minute = time.localtime(time.time())[4] seconds = time.localtime(time.time())[5] ms = time.localtime(time.time())[6] #date=str(year) #if (hour>=12): # day = day+1 t = datetime.datetime.now() EpochSeconds=int(time.mktime(t.timetuple())) outstring = "Trigger " + " ThreshRate "+str(year) + " " + str(month) +" " + str(day) +" "+str(hour)+" " + str(minute) + " " + str(seconds)+ " "+str(ms)+" "+str(EpochSeconds)+" Thresh: " File.write(outstring) #print outstring File.write(str(GlobalVariables.TriggerThresholds[0:]).strip('[]').replace(',', '')) File.write(" rate: ") GlobalVariables.Rates = self.gimmeRates.rates File.write(str(GlobalVariables.Rates[0:]))#.strip('[]').replace(',', '')) #print str(GlobalVariables.Rates[0:]).strip('[]').replace(',', '')) #print str(GlobalVariables.TriggerThresholds[0:]).strip('[]').replace(',', '') #print "\n" File.write("\n") File.close(); def run(self): self.gimmeRates.start() while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"): time.sleep(0.001) time.sleep(self.interval) self.writeToFile() class ParseUserInput(threading.Thread): gimmeRates=GimmeRates() writeOutput=WriteOutput() def Parse(self,Command): Command = Command[1:] #GlobalVariables.ServerMessage = "PARSING: " + Command sCommand=Command.split() error_code=0 #python lacks switch... try: VMEModule=sCommand[0] if(VMEModule == "help"): GlobalVariables.ServerMessage = "please use \' help\' " #GlobalVariables.ServerMessage="please use \' help\' " # now do the stuff for the V560: if(VMEModule == "V560"): V560Command=sCommand[1] if(V560Command=="help"): GlobalVariables.ServerMessage = "available functions are: \n" GlobalVariables.ServerMessage += "V560 GetRate \n" GlobalVariables.ServerMessage += "V560 GetRates =13): MyTime+=12*60*60 year = time.gmtime(MyTime)[0] month =time.gmtime(MyTime)[1] day = time.gmtime(MyTime)[2] date=str(year) #if (hour>=12): # day = day+1 date+="%(month)02d" % {"month":month} date+="%(day)02d" % {"day":day} #date+="_" #date+="%(hour)02d" % {"hour":hour} #date+="%(minute)02d" % {"minute":minute} self.writeOutput.filename="/ct3data/SlowData/Trigger_"+date+".slow" #self.writeOutput.filename = "/ct3data/SlowData/Trigger_" #self.writeOutput.filename if(oldcommand!=CommandToParse): self.Parse(CommandToParse) oldcommand=CommandToParse time.sleep(0.05) GlobalVariables.ServerMessage = "EXITING PARSER...press to exit" time.sleep(0.05)