Mon, 25 Feb 2008 23:22:20 +0100
Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
pygpibtoolkit/gpibcontroller.py | file | annotate | diff | comparison | revisions | |
pygpibtoolkit/pygpib.py | file | annotate | diff | comparison | revisions |
--- a/pygpibtoolkit/gpibcontroller.py Mon Feb 25 23:21:08 2008 +0100 +++ b/pygpibtoolkit/gpibcontroller.py Mon Feb 25 23:22:20 2008 +0100 @@ -34,30 +34,32 @@ self._address = address self._controller = controller - self._registry = self._cmd_register() # singleton self._cache = {} for pname, param in self.__class__.__dict__.items(): if isinstance(param, AbstractCommand) and not param._readonly: self._cache[pname] = param._init_value def _get(self, name): - assert name in self._registry if name in self._cache: if self._cache[name] is None: # where to get the info? # ... compute value - value = self._registry[name].get_value(self._cnx) + param = getattr(self.__class__, name) + cmd = param.build_get_cmd() + value = self._controller.send_command(self._address, cmd) + value = param.convert_from(value) self._cache[name] = value return self._cache[name] def _set(self, name, value): - assert name in self._registry - res = self._registry[name].set_value(self._cnx, value) + param = getattr(self.__class__, name) + cmd = param.build_set_cmd(value) + res = self._controller.send_command(self._address, cmd) if name in self._cache: self._cache[name] = value return res - def manage_srq(self): + def manage_srq(self, statusbyte): pass def send_command(self, cmd): @@ -141,7 +143,7 @@ if polled[add] & 0x40: print "device %s (#%s) requested attention"%(self._devices[add]._idn, add) # TODO: check what to do... - self._devices[add].manage_sqr() + self._devices[add].manage_srq(polled[add]) def _cnx_loop(self): while 1:
--- a/pygpibtoolkit/pygpib.py Mon Feb 25 23:21:08 2008 +0100 +++ b/pygpibtoolkit/pygpib.py Mon Feb 25 23:22:20 2008 +0100 @@ -115,41 +115,42 @@ return instance._set(self.__class__.__name__, value) class Command(AbstractCommand): - @classmethod - def get_value(self, cnx): - res = cnx.send_command(self.__class__.__name__) - return self.convert_from(res) + def build_get_cmd(self): + return self.__class__.__name__ - @classmethod - def set_value(self, cnx, value): + def build_set_cmd(self, value): raise ValueError, "Can't set value for command '%s'"%self.__class__.__name__ - @classmethod def convert_from(self, value): - return value + return value.strip() class AbstractValue(Command): _readonly = False - @classmethod - def set_value(self, cnx, value): + def build_get_cmd(self): + return self.__class__.__name__ + "?" + + def build_set_cmd(self, value): value = self.convert_to(value) - res = cnx.send_command("%s %s"%(self.__class__.__name__, value) - return res - @classmethod + cmd = "%s %s"%(self.__class__.__name__, value) + return cmd + def convert_to(self, value): return str(value) + + def convert_from(self, value): + return self._type(value.strip()) class BoolValue(AbstractValue): _type = bool - @classmethod def convert_from(self, value): return value and value.lower() in ['1','true','yes','on'] - @classmethod def convert_to(self, value): return value and "1" or "0" class IntValue(AbstractValue): _type = int + def convert_from(self, value): + return int(float(value.strip())) class FloatValue(AbstractValue): _type = float