Ejemplo n.º 1
0
    def set_pv_value(self, name, value, wait=False, timeout=TIMEOUT):
        """Set the PV to a value.
           When getting a PV value this call should be used, unless there is a special requirement.

        Parameters:
            name - the PV name
            value - the value to set
            wait - wait for the value to be set before returning
        """
        name = MYPVPREFIX + ':' + name
        if name not in CACHE.keys():
            chan = CaChannel(name)
            chan.setTimeout(TIMEOUT)
            #Try to connect - throws if cannot
            chan.searchw()
            CACHE[name] = chan
        else:
            chan = CACHE[name]
        if wait:
            chan.putw(value)
        else:

            def putCB(epics_args, user_args):
                #Do nothing in the callback
                pass

            ftype = chan.field_type()
            ecount = chan.element_count()
            chan.array_put_callback(value, ftype, ecount, putCB)
            chan.flush_io()
Ejemplo n.º 2
0
    def get_pv_value(self, name, to_string=False, timeout=TIMEOUT):
        """Get the current value of the PV"""
        name = MYPVPREFIX + ':' + name

        if name not in CACHE.keys():
            chan = CaChannel(name)
            chan.setTimeout(TIMEOUT)
            #Try to connect - throws if cannot
            chan.searchw()
            CACHE[name] = chan
        else:
            chan = CACHE[name]
        ftype = chan.field_type()
        if ca.dbr_type_is_ENUM(ftype) or ca.dbr_type_is_CHAR(
                ftype) or ca.dbr_type_is_STRING(ftype):
            to_string = True
        if to_string:
            if ca.dbr_type_is_ENUM(ftype):
                value = chan.getw(ca.DBR_STRING)
            else:
                value = chan.getw(ca.DBR_CHAR)
            #Could see if the element count is > 1 instead
            if isinstance(value, list):
                return self._waveform2string(value)
            else:
                return str(value)
        else:
            return chan.getw()
    def _initialise_channel(self, name):
        """Initialises the channel by creating a connection callback.

        Args:
            name (string): The name of the PV to connect to.
        """
        def _block_connection_callback(epics_args, user_args):
            connection_state = epics_args[1]
            if connection_state == ca.CA_OP_CONN_UP:
                print ca.name(epics_args[0]),  "UP"
                # First time through creates the monitor
                if ca.name(epics_args[0]) not in self.curr_values:
                    self._create_monitor(user_args[0], ca.name(epics_args[0]))
            else:
                print ca.name(epics_args[0]), "DOWN"
                self.curr_lock.acquire()
                try:
                    self.curr_values[ca.name(epics_args[0])] = ("*** disconnected", None)
                except:
                    # Don't care, it is all about releasing the lock
                    pass
                finally:
                    # Must release lock
                    self.curr_lock.release()  
    
        chan = CaChannel()
        chan.setTimeout(EXISTS_TIMEOUT)
        print "initialising %s" % name
        try:
            chan.search_and_connect(name, _block_connection_callback, chan)
            chan.pend_event(PEND_EVENT_TIMEOUT)
            return chan
        except:
            print "Could not connect"
            return None
Ejemplo n.º 4
0
 def _caget(pvname, as_string=False):
     """Retrieve an EPICS PV value"""
     try:
         from CaChannel import CaChannel, CaChannelException, ca
     except ImportError:
         raise RuntimeError("CaChannel must be installed to use this algorithm. "
                            "For details, see https://www.mantidproject.org/CaChannel_In_Mantid")
     if as_string:
         dbr_type = ca.DBR_STRING
     else:
         dbr_type = None
     try:
         chan = CaChannel(pvname)
         chan.setTimeout(5.0)
         chan.searchw()
         return chan.getw(dbr_type)
     except CaChannelException as e:
         raise RuntimeError("Error reading EPICS PV \"{}\": {}".format(pvname, str(e)))