def generate_live_data(duration, period, pvnames): pvnamelist = tuple(pvnames) cols = list(pvnamelist) cols.insert(0, 'timestamp') pvconn = {} alldata = [] ca.initialize_libca() for name in pvnamelist: chid = ca.create_channel(name, connect=False, auto_cb=False) pvconn[name] = (chid, None) for key in list(pvconn): state = ca.connect_channel(pvconn.get(key)[0]) if not state: print('PV:', key, 'not connected') pvconn.pop(key) ca.poll() while duration > 0 and pvconn: pvdata = [] for name, data in pvconn.items(): ca.get(data[0], wait=False) ca.poll() pvdata.append(datetime.datetime.now()) for name, data in pvconn.items(): val = ca.get_complete(data[0]) pvdata.append(val) alldata.append(pvdata) time.sleep(period) duration = duration - period ca.finalize_libca(maxtime=1.0) df = pd.DataFrame(alldata, columns=cols) return df
def monitor( mon_line ): #this is run as a thread to monitor a specific pv for many elements global pvdata global pvf while mon_line > -1: #ie infinite loop if not pvdata[mon_line][pvf.EN].get(): #if not enabled just wait 1s print "Not enabled: ", pvdata[mon_line][ pvf.TOP] + ":" + pvdata[mon_line][pvf.NAME] + "_wf" time.sleep(1.0) else: #if enabled for chid in pvdata[mon_line][pvf.ID]: #for all the chids if chid > 0: #if the channel is connected ca.get(chid, wait=False ) #Tell them all to start getting but don't wait for n, chid in enumerate( pvdata[mon_line][pvf.ID]): #for all the chids if chid > 0: #if the channel is connected pvdata[mon_line][pvf.ARRAY][n] = ca.get_complete( chid) #get the value to the array pvdata[mon_line][pvf.WF].put( pvdata[mon_line][pvf.ARRAY]) #put to the waveform #print "mon_line",mon_line,pvdata[mon_line][pvf.PERIOD] time.sleep(float( pvdata[mon_line][pvf.PERIOD])) #wait for the refresh period
def test_xArray1(self): write('Array Test: get(wait=False) / get_complete()') chid = ca.create_channel(pvnames.double_arrays[0]) val0 = ca.get(chid) aval = ca.get(chid, wait=False) self.assertTrue(aval is None) val1 = ca.get_complete(chid) self.assertTrue(all(val0 == val1))
def batch_get(pv_list): chids = {} pvdata = {} for name in pv_list: chid = ca.create_channel(name, connect=False, auto_cb=False) chids[name] = chid for name, chid in chids.items(): ca.connect_channel(chid) ca.poll() for name, chid in chids.items(): ca.get(chid, wait=False) ca.poll() for name, chid in chids.items(): val = ca.get_complete(chid) pvdata[name] = val return pvdata
def monitor(mon_line): #this is run as a thread to monitor a specific pv for many elements global pvdata global pvf while mon_line > -1: #ie infinite loop if not pvdata[mon_line][pvf.EN].get(): #if not enabled just wait 1s print "Not enabled: ",pvdata[mon_line][pvf.TOP]+":"+pvdata[mon_line][pvf.NAME]+"_wf" time.sleep(1.0) else: #if enabled for chid in pvdata[mon_line][pvf.ID]: #for all the chids if chid > 0: #if the channel is connected ca.get(chid, wait=False) #Tell them all to start getting but don't wait for n,chid in enumerate(pvdata[mon_line][pvf.ID]): #for all the chids if chid > 0: #if the channel is connected pvdata[mon_line][pvf.ARRAY][n]=ca.get_complete(chid)#get the value to the array pvdata[mon_line][pvf.WF].put(pvdata[mon_line][pvf.ARRAY])#put to the waveform #print "mon_line",mon_line,pvdata[mon_line][pvf.PERIOD] time.sleep(float(pvdata[mon_line][pvf.PERIOD])) #wait for the refresh period
def _compare_pvs(pvs, values): """.""" while True: vals = [] for pv in pvs: ca.get(pv.chid, wait=False) for pv in pvs: val = ca.get_complete(pv.chid) if val is None: raise ValueError('val is None') vals.append(val) vals = np.asarray(vals, dtype=float) eq = np.allclose(vals, values, atol=1e-7) if eq: break # else: # print(np.max(vals - values)) time.sleep(0.001)
def run_synchronous_get_optimized(pvs): """Run synchronous optimized get test.""" pvschid = [] for pvn in pvs: chid = ca.create_channel(pvn, connect=False, auto_cb=False) pvschid.append(chid) for chid in pvschid: ca.connect_channel(chid) ca.poll() for i in range(600): t0 = time.time() for chid in pvschid: ca.get(chid, wait=False) out = [] for chid in pvschid: out.append(ca.get_complete(chid)) print(f'dtime {(time.time()-t0)*1000:.1f}ms pos {out[0]:.0f}nm')
def _set_params2(self): """Set run params...""" functionName = '_set_params' pvdata = {} for name in self._pvnamelist: chid = ca.create_channel(name, connect=False, auto_cb=False) # note 1 pvdata[name] = [chid, None] for name, data in pvdata.items(): ca.connect_channel(data[0]) ca.poll() for name, data in pvdata.items(): ca.get(data[0], wait=False) # note 2 ca.poll() for name, data in pvdata.items(): val = ca.get_complete(data[0]) pvdata[name][1] = val #return { name: data[1] for name, data in pvdata.items()} ret = dict((name, data[1]) for name, data in pvdata.items()) print(ret) return ret
time.sleep(0.001) add("connected to PVs with connect_channel") ca.pend_event(1.e-2) for name in pvnames: chid = results[name]['chid'] val = ca.get(chid, wait=False) results[name]['value'] = val add("did ca.get(wait=False)") ca.poll(2.e-3, 1.0) add("ca.poll() complete") for name in pvnames: results[name]['value'] = ca.get_complete(results[name]['chid']) add("ca.get_complete() for all PVs") f = open('fastconn_pvdata.sav', 'w') for name, val in results.items(): f.write("%s %s\n" % (name.strip(), val['value'])) f.close() add("wrote PV values to disk") dt.show() time.sleep(0.01) ca.poll()
dt.add("connected to PVs with connect_channel") ca.pend_event(1.e-2) for name in pvnames: chid = results[name]['chid'] val = ca.get(chid, wait=False) results[name]['value'] = val dt.add("did ca.get(wait=False)") ca.pend_event(1.e-2) dt.add("pend complete") for name in pvnames: chid = results[name]['chid'] val = ca.get_complete(chid) results[name]['value'] = val dt.add("unpacked PV values") f = open('fastconn_pvdata.sav', 'w') for name, val in results.items(): f.write("%s %s\n" % (name.strip(), val['value'])) f.close() dt.add("wrote values PVs") dt.show() time.sleep(0.01) ca.poll()
def main(): parser = argparse.ArgumentParser() # parser for the inputs - must keep the 2 lines below. parser.add_argument("top", help="top node") # must have a top node, -v VERBOSITY is an opion parser.add_argument("-v", "--verbosity", help="increase verbosity", nargs='?', const=0, default=0) #add extra args and opts here parser.add_argument("pv", help="pvname") args = parser.parse_args() #get the input args from the parser global verbose verbose = args.verbosity global pvname global nChid pvname = args.pv #This calls the doNode() (bottom of this file) that works recursively through the tree and calls do_node() #which you may overwrite by calling your own function (eg my_node() shown here #In this case, it calls my_node() for every element - see below. doNode.doNode(args.top,do_node=my_node, v=verbose) wave=numpy.zeros(nChid) #create an array for all the values pvname_wf = args.top+":"+pvname+"_wf" #make the names of the PVs for waveform and enable switch pvname_en = args.top+":"+pvname+"_en" print pvname_wf,pvname_en pvwf = PV(pvname_wf) #set the PV pven = PV(pvname_en) #set the PV pven.put(1) for i in range(len(allids)): #connect call the channels if not ca.connect_channel(allids[i],timeout=0.001): allids[i]=0 print "Warning didn't connect to PV =", allnames[i], "ignoring this one" ca.poll() #Not sure exactly what this does! n=0 while n < 100000: for i in range(len(allids)): #print i, allids[i] if allids[i] > 0: ca.get(allids[i], wait=False) #Tell them all to start getting but don't wait ca.poll() for i in range(len(allids)): #now finish getting if allids[i] > 0: val = ca.get_complete(allids[i]) wave[i]=val if verbose: #print allnames[i],val pvwf.put(wave) time.sleep(0.5) if not pven.get(): exit() #print n,pven.get() n+=1
if str(sys.argv[1]) == 'clean': config = open('./config/pv_list_clean.json', 'r') else: config = open('./config/pv_list.json', 'r') else: sys.exit('at most two arguments allowed.') result = {} pv_list = json.loads(config.read()) start = time.time() for pv_name in pv_list: ch = ca.create_channel(pv_name, connect=False, auto_cb=False) result[pv_name] = [ch, None, None] for pv_name, data in result.items(): result[pv_name][1] = ca.connect_channel(data[0], timeout=1.0) ca.poll() for pv_name, data in result.items(): if result[pv_name][1]: ca.get(data[0], wait=False) ca.poll() for pv_name, data in result.items(): if result[pv_name][1]: val = ca.get_complete(data[0]) result[pv_name][2] = val else: result[pv_name][2] = 'not connected' duration = time.time() - start print int(round(duration * 1000))
time.sleep(0.001) dt.add("connected to PVs with connect_channel") ca.pend_event(1.e-2) for name in pvnames: chid = results[name]['chid'] val = ca.get(chid, wait=False) results[name]['value'] = val dt.add("did ca.get(wait=False)") ca.poll(2.e-3, 1.0) dt.add("ca.poll() complete") for name in pvnames: results[name]['value'] = ca.get_complete(results[name]['chid']) dt.add("ca.get_complete() for all PVs") f = open('fastconn_pvdata.sav', 'w') for name, val in results.items(): f.write("%s %s\n" % (name.strip(), val['value'])) f.close() dt.add("wrote PV values to disk") dt.show() time.sleep(0.01) ca.poll()
def main(): parser = argparse.ArgumentParser() # parser for the inputs - must keep the 2 lines below. parser.add_argument("top", help="top node") # must have a top node, -v VERBOSITY is an opion parser.add_argument("-v", "--verbosity", help="increase verbosity", nargs='?', const=0, default=0) #add extra args and opts here parser.add_argument("pv", help="pvname") args = parser.parse_args() #get the input args from the parser global verbose verbose = args.verbosity global pvname global nChid pvname = args.pv #This calls the doNode() (bottom of this file) that works recursively through the tree and calls do_node() #which you may overwrite by calling your own function (eg my_node() shown here #In this case, it calls my_node() for every element - see below. doNode.doNode(args.top,do_node=my_node, v=verbose) wave=numpy.zeros(nChid) #create an array for all the values pvname_wf = args.top+":"+pvname+"_wf" #make the names of the PVs for waveform and enable switch pvname_en = args.top+":"+pvname+"_en" print pvname_wf,pvname_en pvwf = PV(pvname_wf) #set the PV pven = PV(pvname_en) #set the PV pven.put(1) for i in range(len(allids)): #connect call the channels if not ca.connect_channel(allids[i],timeout=0.001): allids[i]=0 print "Warning didn't connect to PV =", allnames[i], "ignoring this one" ca.poll() #Not sure exactly what this does! n=0 while n < 100000: for i in range(len(allids)): #print i, allids[i] if allids[i] > 0: ca.get(allids[i], wait=False) #Tell them all to start getting but don't wait ca.poll() for i in range(len(allids)): #now finish getting if allids[i] > 0: val = ca.get_complete(allids[i]) wave[i]=val #if verbose: #print allnames[i],val pvwf.put(wave) time.sleep(0.5) if not pven.get(): exit() #print n,pven.get() n+=1