def __init__(self, ch=0, ch_scanner='melba_020:scan:'): QObject.__init__(self) # Initiate pyqtSignal self.ch = ch self.scanner = ch_scanner self.data = pd.Series() self.delay = 0.5 # Predefined delay in seconds between processing of record self.pv = { 'nos': PV(self.scanner + 'datach' + str(self.ch) + '_noofsamples_get'), 'samples': PV(self.scanner + 'datach' + str(self.ch) + '_samples_get') } self.pv['nos'].get() time.sleep(0.2) self.pv['samples'].get() time.sleep(0.2) # Debug for k in self.pv.keys(): print("ch{} : self.pv[{}].value = {}".format( self.ch, k, self.pv[k].value)) print("ch{} : type(self.pv[{}].value) = {}".format( self.ch, k, type(self.pv[k].value)))
def connect_to_pv(pv_name, n_connection_attempts=3): """ Start a connection to a PV. :param pv_name: PV name to connect to. :param n_connection_attempts: How many times you should try to connect before raising an exception. :return: PV object. :raises ValueError if cannot connect to PV. """ pv = PV(pv_name, auto_monitor=False) for i in range(n_connection_attempts): if pv.connect(): return pv sleep(0.1) raise ValueError("Cannot connect to PV '%s'." % pv_name)
def restore_pvs(filepath, debug=False): """ Restore pvs from a save file via Channel Access debug - Set to True if you want a line printed for each value set Returns True if all pvs were restored successfully. """ success = True fout = open(filepath, 'r') lines = fout.readlines() fout.close() for line in lines: if line.startswith('<END'): break if line.startswith('#'): continue pvname, value = [w.strip() for w in line[:-1].split(' ', 1)] if value.startswith('<JSON>:'): # for older version, could be deprecated value = value.replace('<JSON>:', '@array@') if value.startswith('@array@'): value = value.replace('@array@', '').strip() if value.startswith('{') and value.endswith('}'): value = value[1:-1] value = json.loads(value) if debug: print( "Setting %s to %s..." % (pvname, value)) try: thispv = PV(pvname) pvconnectedstatus=thispv.wait_for_connection() if not pvconnectedstatus: print("Cannot connect to %s" % (pvname)) success = False elif not thispv.write_access: print("No write access to %s" % (pvname)) success = False else: thispv.put(value, wait=False) except: exctype, excvalue, exctrace = sys.exc_info() print("Error restoring %s to %s : %s" % (pvname, value, exctype, excvalue)) success = False return success
def restore_pvs(filepath, debug=False): """ Restore pvs from a save file via Channel Access debug - Set to True if you want a line printed for each value set Returns True if all pvs were restored successfully. """ success = True values = [x for x in sav_file.parseFile(filepath).asList() if len(x) > 0] print("Restoring %d values..." % (len(values))) for v in values: if debug: print("Setting %s to %s..." % (v[0], v[1])) try: pv = PV(v[0]) pv.put(v[1]) # hopefully this is good enough as conversions go except Exception(e): print("Error restoring %s to %s : %s" % (v[0], v[1], e)) success = False return success
def restore_pvs(filepath, debug=False): """ Restore pvs from a save file via Channel Access debug - Set to True if you want a line printed for each value set Returns True if all pvs were restored successfully. """ success = True values = [ x for x in sav_file.parseFile(filepath).asList() if len(x) > 0 ] print( "Restoring %d values..." % ( len(values) )) for v in values: if debug: print( "Setting %s to %s..." % (v[0], v[1])) try: pv = PV(v[0]) pv.put(v[1]) # hopefully this is good enough as conversions go except Exception(e): print( "Error restoring %s to %s : %s" % (v[0], v[1], e)) success = False return success
def save_pvs(request_file, save_pvs, debug=False): """ Save pvs from a request file to a save file, via Channel Access Set debug=True to print a line for each PV saved. With throw an exception if any PV cannot be found. """ pvnames = _parse_request_file(request_file) pv_vals = [(pvname, PV(pvname).get()) for pvname in pvnames] if debug: for (pv, val) in pv_vals: print("PV %s = %s" % (pv, val)) f = open(save_pvs, "w") f.write("# File saved automatically by save_restore.py on %s\n" % datetime.datetime.now().isoformat()) f.write("# Edit with extreme care.\n") f.writelines(["%s %s\n" % v for v in pv_vals]) f.write("<END>\n") f.close()
def restore_pvs(filepath, debug=False): """ Restore pvs from a save file via Channel Access debug - Set to True if you want a line printed for each value set Returns True if all pvs were restored successfully. """ success = True fout = open(filepath, 'r') lines = fout.readlines() fout.close() for line in lines: if line.startswith('<END'): break if line.startswith('#'): continue pvname, value = [w.strip() for w in line[:-1].split(' ', 1)] if value.startswith( '<JSON>:'): # for older version, could be deprecated value = value.replace('<JSON>:', '@array@') if value.startswith('@array@'): value = value.replace('@array@', '').strip() if value.startswith('{') and value.endswith('}'): value = value[1:-1] value = json.loads(value) if debug: print("Setting %s to %s..." % (pvname, value)) try: thispv = PV(pvname) thispv.connect() if not thispv.connected: print("Cannot connect to %s" % (pvname)) elif not thispv.write_access: print("No write access to %s" % (pvname)) else: thispv.put(value, wait=False) except: exctype, excvalue, exctrace = sys.exc_info() print("Error restoring %s to %s : %s" % (pvname, value, exctype, excvalue)) success = False return success
def save_pvs(request_file, save_pvs, debug=False): """ Save pvs from a request file to a save file, via Channel Access Set debug=True to print a line for each PV saved. Will print a warning if a PV cannot connect. """ pv_vals = [] pvobjs = [PV(pvn) for pvn in _parse_request_file(request_file)] [pv.connect() for pv in pvobjs] for thispv in pvobjs: pvname = thispv.pvname thispv.connect() if not thispv.connected: print("Cannot connect to %s" % (pvname)) continue if thispv.count == 1: value = str(thispv.get()) elif thispv.count > 1 and thispv.type == 'char': value = thispv.get(as_string=True) elif thispv.count > 1 and thispv.type != 'char': value = '@array@ %s' % json.dumps(thispv.get().tolist()) pv_vals.append((pvname, value)) if debug: for (pv, val) in pv_vals: print("PV %s = %s" % (pv, val)) f = open(save_pvs, "w") f.write("# File saved by pyepics autosave.save_pvs() on %s\n" % datetime.datetime.now().isoformat()) f.write("# Edit with extreme care.\n") f.writelines(["%s %s\n" % v for v in pv_vals]) f.write("<END>\n") f.close()
if len(sys.argv) > 2: N0 = int(sys.argv[2]) master = EpicsArchiver.MasterDB() all_pvs = master.cache.select('*') pv_names = [i['pvname'] for i in all_pvs[N0:N0+NMAX]] # create pvs = [] t0 = time.time() for pvname in pv_names: o = PV(pvname) pvs.append(o) t1 = time.time() # connect unconnected = 0 for o in pvs: o.connect(timeout=0.1) if not o.connected: unconnected += 1 t2 = time.time() # get out = []
NMAX = int(sys.argv[1]) N0 = 0 if len(sys.argv) > 2: N0 = int(sys.argv[2]) master = EpicsArchiver.MasterDB() all_pvs = master.cache.select('*') pv_names = [i['pvname'] for i in all_pvs[N0:N0 + NMAX]] # create pvs = [] t0 = time.time() for pvname in pv_names: o = PV(pvname) pvs.append(o) t1 = time.time() # connect unconnected = 0 for o in pvs: o.connect(timeout=0.1) if not o.connected: unconnected += 1 t2 = time.time() # get out = []