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 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 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)
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 = [] for o in pvs: if o.connected: v = o.get() out.append('%s = %s' % (o.pvname, repr(v))) t3 = time.time() print " %i PVS connected and fetched in %.4f seconds. %i PVs did not connect." %(NMAX, t3-t0, unconnected)
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 = [] for o in pvs: if o.connected: v = o.get() out.append('%s = %s' % (o.pvname, repr(v))) t3 = time.time() print " %i PVS connected and fetched in %.4f seconds. %i PVs did not connect." % (