def caget_cached(PV_name): """Value of Channel Access (CA) Process Variable (PV)""" from CA import caget, camonitor camonitor(PV_name, callback=CA_cache_update) value = caget(PV_name, timeout=0) if value is None: if cache.exists(PV_name): value = cache.get(PV_name) if value is None: value = caget(PV_name) if value is None: warn("Failed to get PV %r" % PV_name) return value
def read(self,nchar=1): """Receive data, until either nchar bytes have received or a timeout has occurred""" caput(self.port+".TMOD",2) # 2 = read caput(self.port+".IEOS","\0",wait=True) # no input terminator caput(self.port+".NRRD",nchar,wait=True) # For unknown reason, following fails at the first try, but succeed at # the second try. try: caput(self.port+".PROC","1",wait=True) # this will cause it to wait for data except: caput(self.port+".PROC","1",wait=True) try: return caget(self.port+".TINP") except: return caget(self.port+".TINP")
def get_integer_register(self, i): """Target position in dial units""" t0 = time() msg = False register = caget("NIH:ENSEMBLE.integer_registers[%d]" % i) while register is None: if time() - t0 > 1 and not msg: debug("Ensemble: reading integer register %d: retrying..." % i) msg = True sleep(0.1) register = caget("NIH:ENSEMBLE.integer_registers[%d]" % i) if time() - t0 > 1: debug("Ensemble: integer register %d read" % i) return register
def query(self, string, terminator="\n", count=0): """Receive data, until either the terminator character was received, the number of bytes given by 'count' have have received or a timeout has occurred.""" # The first 'caget' after python statup always fails. As a work-around, use a # dummy caget just in case. if caget(self.port + ".TMOD") != 0: caput(self.port + ".TMOD", 0, wait=True) # 0 = write/read if string.endswith("\n"): caput(self.port + ".OEOS", "\n", wait=True) string = string[0:-1] elif caget(self.port + ".OEOS") != "": caput(self.port + ".OEOS", "\0", wait=True) # not output terminator if count: if caget(self.port + ".IEOS") != "": caput(self.port + ".IEOS", "\0", wait=True) # "\0" = none elif terminator: if caget(self.port + ".IEOS") != terminator: caput(self.port + ".IEOS", terminator, wait=True) # input terminator if caget(self.port + ".IFMT") != 1: caput(self.port + ".IFMT", 1, wait=True) # 1 = hybrid if caget(self.port + ".SCAN") != 0: caput(self.port + ".SCAN", 0, wait=True) # 0 = passive if caget(self.port + ".NRRD") != count: caput(self.port + ".NRRD", count, wait=True) # number of chars (0 = unlimited) # If case string contains binary data, EpicsCA would strip away trailing # null characters. Sending non-ASCII chacters as except codes # makes sure that null characters are sent, too. encoded_string = repr(string)[1:-1] caput(self.port + ".AOUT", encoded_string, wait=True) n = caget(self.port + ".NORD") if n == None: n = 0 if n == 0: return "" # nothing read # With EpicsCA it is (as of June 2009) not possible to reliably retreive # special characters from the BINP (binary input) variable, because # EpicsCA strips off trailing carriage return and null characters # before passing back a string. Thus, I read the TINP (Text input) # variable instead. reply = caget(self.port + ".TINP") if not reply: reply = "" # Special characters in the TINP field are encoded as octal # escape sequences. Decode them. ##print "reply",repr(reply) reply = eval("'" + reply + "'") if caget(self.port + ".EOMR") == 2: # 2 = EOS return reply + terminator # terminator was stripped off, add it back return reply
def remote_shutter_state(): """Tell the status of 'Remote Shutter' (in beamline frontend). Return 'open' or 'closed'""" from CA import caget state = caget("PA:14ID:A_SHTRS_CLOSED.VAL") if state == 1: return "closed" if state == 0: return "open"
def get_readback_PV(self): """Process variable value for the actual position as measured. Ususually the value of the RBV process variable, but may me overriden.""" if not ":" in self.__readback__: return getattr(self, self.__readback__) else: return caget(self.__readback__)
def get_value(self): PV_state = caget(self.state_PV) if PV_state == 1: state =True elif PV_state == 0: state = False elif PV_state is None: state = nan else: state = False return state
def save_images(self): from PIL import Image import logging from tempfile import gettempdir #/var/folders/y4/cw92kt415kz7wtk13fkjhh2r0000gn/T/samplr_frozen_opt.log' import os file_path = gettempdir( ) + "/Images/Optical_images_march4/log.log" # gettempdir + "/Optical_images/log.log" directory = os.path.dirname(file_path) try: os.stat(directory) except: os.mkdir(directory) for i in range(360): sleep(10) while self.is_new_image() != True: sleep(0.05) if self.is_new_image(): img = Image.fromarray( camera.RGB_array.transpose((-1, 0, 1)).transpose( (-1, 0, 1))) temp = str(caget("NIH:TEMP.RBV")) img.save(directory + '/_T_' + temp + '_t_' + str(time()) + '.tiff') print('saving', directory + '_T_' + temp + '_t_' + str(time()) + '.tiff')
def scan_once(dT=5, T=22.0, mode=''): (P, I, D) = caget('NIH:LIGHTWAVE.PCOF'), caget( 'NIH:LIGHTWAVE.ICOF'), caget('NIH:LIGHTWAVE.DCOF') if mode == 'advance': caput('NIH:TEMP.VAL_ADV', T) cawait('NIH:TEMP.VAL_ADV') else: caput('NIH:TEMP.VAL', T) cawait('NIH:TEMP.VAL') sleep(1) while not caget('NIH:TEMP.DMOV'): sleep(0.05) sleep(1) caget('NIH:TEMP.DMOV') t_start = time() if mode == 'advance': caput('NIH:TEMP.VAL_ADV', T + dT) cawait('NIH:TEMP.VAL_ADV') else: caput('NIH:TEMP.VAL', T + dT) cawait('NIH:TEMP.VAL') sleep(1) while not caget('NIH:TEMP.DMOV'): sleep(0.05) t_end = time() sleep(5) current = caget('NIH:TEMP.I') res = {} res['temperature'] = T + dT res['current'] = current res['t start'] = t_start res['t end'] = t_end res['dt'] = t_end - t_start res['dT'] = dT res['P'] = P res['I'] = I res['D'] = D res['oasis_T'] = caget('NIH:CHILLER.VAL') if mode == 'advance': res['mode'] = 1 else: res['mode'] = 0 res['data_rbv'] = asarray( channel_archiver.history('NIH:LIGHTWAVE.RBV', t_start, t_end)) res['data_current'] = asarray( channel_archiver.history('NIH:LIGHTWAVE.I', t_start, t_end)) res['data_val'] = asarray( channel_archiver.history('NIH:LIGHTWAVE.VAL', t_start, t_end)) return res
def get_auto(self): from CA import caget from numpy import nan PV_state = caget(self.auto_PV_name) state = nan if PV_state == 0: state = False if PV_state == 1: state = True return state
def _connect(self): try: debug("Creating socket") self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) debug("Connecting to %r on port 9900" % self.ip_address_server) from CA import caget try: port = int(caget('NIH:DI245.SOCKET')[1]) ip_address = caget('NIH:DI245.SOCKET')[0] except: error(traceback.format_exc()) port = nan self.server.connect((self.ip_address_server, port)) debug("Connection success!") except socket.error, msg: error("Failed to create and connect. Error code: %s , Error message : %r" % (str(msg[0]), msg[1]))
def test(self, event): """Check if PVs are working b yreading their current value""" from CA import caget for i in range(0, self.Table.GetNumberRows()): enabled = self.Table.GetCellValue(i, 0) == "Yes" PV = str(self.Table.GetCellValue(i, 2)) value = str(caget(PV)) if (PV and enabled) else "" self.Table.SetCellValue(i, 3, value) self.update_layout()
def get_value(self): """Is the laser shutter open?""" from CA import caget PV_state = caget("14IDB:B1Bi0.VAL") from numpy import nan if PV_state == 0: state = True elif PV_state == 1: state = False elif PV_state is None: state = nan else: state = False return state
def get(self): from timing_system import timing_system from CA import caget PV_name = eval("timing_system.%s.PV_name" % name) # Use "caget" to circumvent caching in the "timing_system" module value = caget(PV_name) try: value = type(default_value)(value) except: value = default_value return value
def value(name): """The value of a process variable, if name is a process variable. Otherwise, name is assumed to be the name of a Python object and its 'value' property is used. name: string, name of EPICS process variable or name of Python variable """ from CA import caget try: x = eval(name) except: return tofloat(caget(name)) return tofloat(getattr(x, "value", x))
def get(self): from CA import caget value = caget(prefix(self) + name) if value is None: value = default_value if type(value) != type(default_value): if type(default_value) == list: value = [value] else: try: value = type(default_value)(value) except: value = default_value return value
def monitor_PVS(pv): from numpy import nan from CA import caget from time import sleep dic[pv] = (0, 0) while True: sleep(0.5) value = caget(pv) if type(value) == None or value == nan: print('WARNING: %r IS %r' % (pv, value)) dic[pv] = (dic[pv][0], dic[pv][1] + 1) else: dic[pv] = (dic[pv][0] + 1, dic[pv][1])
def get_integer_registers(self): """Target position in dial units""" t0 = time() msg = False registers = None while registers is None: registers = caget("NIH:ENSEMBLE.integer_registers[:]") if registers is None: if time() - t0 > 1 and not msg: debug("Ensemble: reading integer registers: retrying...") msg = True sleep(0.1) if time() - t0 > 1: debug("Ensemble: read integer registers") return asarray(registers)
def get_value(self): """Current position in user units""" n = self.motor_number value = tofloat(caget("NIH:ENSEMBLE.values[%d]" % n)) value = round_next(value, self.stepsize) return value
def get_moving(self): return (caget(self.rec_name+":Busy.VAL") != 0)
def get_name(self): name = caget(self.rec_name+":Device") if name == None: name = "" return name
def get_readback_value(self): value = caget(self.rec_name+":Gap.VAL") if value == None: value = NaN return value
def get_command_value(self): value = caget(self.rec_name+":GapSet.VAL") if value == None: value = NaN return value
def get_count_time(self): return caget(self.ioc_name + ".S1") / 1e7
def get_name(self): return caget(self.ioc_name + ".NM" + self.channel)
def get_count(self): return caget(self.ioc_name + ".S" + self.channel)
def get_value(self): """Is the illuminator currently in the X-ray beam?""" from CA import caget inserted = (caget("14IDB:Dliepcr1:Out1Mbbi") == 1) if self.moving: return not inserted return inserted
def get_command_PV(self): """Process variable value for the motor target position. Ususually the value of the VAL process variable, but may me overriden.""" if not ":" in self.__command__: return getattr(self, self.__command__) else: return caget(self.__command__)
def get_enabled(self): """Is holding the current turned on?""" n = self.motor_number return tobool(caget("NIH:ENSEMBLE.enabled[%d]" % n))
def get_moving(self): """Target position""" n = self.motor_number return tobool(caget("NIH:ENSEMBLE.moving[%d]" % n))