# Script to read from channels on the channel mask. # The script is triggered by changes to the local variable asociated with the input text widget. # Orlando Soto, March 2014. from org.csstudio.opibuilder.scriptUtil import PVUtil, ConsoleUtil from org.csstudio.utility.pv import PVFactory import time from java.lang import Thread, Runnable from BCAL_LEDControl_parameters import delay_time # Waiting time for retry. mask = int(widget.getParent().getMacroValue("chmask"),16) # Getting the channel mask from the macro variable "chmask". pref = widget.getMacroValue("pref") # Getting the macro value for "pref". The prefix for the IOC. par = widget.getMacroValue("par") # Getting the macro value for "par", the parameter macro. This allow to copy & paste code and muliple uses. base = 1 # Base channel. ########## Class to handle the connection ####### class PVconnect(Runnable): # Class inherit from Runnable, this allow to run it in threads. def __init__(self,pv): self.pv = pv # PV object self.value = 0.0 # Variable to store value. return def run(self): pv = self.pv pv.start() # Starting the PV. cnt = 0 # Retrying counter. status = pv.isConnected() # Getting connected status. while ( not status) and (cnt<5) : # Retry if is not connected and retry counter is below 5 cnt += 1 Thread.sleep(delay_time) # Wait delay_time for retry. status = pv.isConnected() # Get new connected status. if cnt == 5: # If time out is reached. ConsoleUtil.writeInfo("Error: Timeout to connect with PV %s --- %d (ms)" % (pv.getName(), cnt*delay_time)) value = PVUtil.getDouble(pv) # Retrieving value of PV. pv.stop() #### Switch case for parameter selection.#### if par == 'width': value *= 10 elif par == 'period': if value > 0: value = 100.0e6/(value) else: value = 100.0e6 elif par == 'npulses': value = int(value) else: ConsoleUtil.writeInfo("Parameter %s not found" % par) self.value = value # Storing value #ConsoleUtil.writeInfo( "child says: thread: %d %s in cnt: %d running " % (Thread.currentThread().getId(), pv.getValue(),cnt)) return ################################################# class runInThread(Runnable): # Class to run the script on a thread. def run(self): thread = [] # Thread array. con = [] # PVconnector array. PVs = [] # Array to store proccess variables. Left here in case any modification is needed could be just a regular variable. count = 0; # Counter. for i in range(32): if not (mask&(1< 0 else 0 # Taking the average. Could be done with len(con) or other counter written to make it clear. ### Peparing the output text ### if par == 'width': text_value = "%d (ns)" % int(value) elif par == 'period': if value/1e3 < 1: text_value = "%.3f (Hz)" % value elif value/1e6 < 1: text_value = "%.3f (KHz)" % (value/1.0e3) else: text_value = "%.3f (MHz)" % (value/1.0e6) elif par == 'npulses': if value <> -1: text_value = "# %d" % int(value) else: text_value = "INF" else: ConsoleUtil.writeInfo("Parameter %s not found" % par) pvs[0].setValue(text_value) # Updating the reading value. th = Thread(runInThread()) # Creating the thread. th.start() # Running the thread.