pygpibtoolkit/pygpib.py

Thu, 28 Feb 2008 18:13:55 +0100

author
David Douard <david.douard@logilab.fr>
date
Thu, 28 Feb 2008 18:13:55 +0100
changeset 58
98a94ec5f24e
parent 57
7013c4bebc7b
child 61
6ecb0fd8c129
permissions
-rw-r--r--

make convert_from prototype accepts several arguments (to build command with several arguments)

2
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 """
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
2 gpib: create serial connection to GPIB-USB device (ProLogix is the
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
3 only supported device for now).
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
4 """
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5 import serial
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
6 from serial.serialutil import SerialException
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
7 import time
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
8 from pygpibtoolkit.tools import AbstractRegister
2
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
9
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10 class ConnectionError(Exception):
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 pass
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
13 class Constants(object):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
14 def __init__(self):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
15 self.constants = {}
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
16 self.descriptions = {}
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
17 self.rev_constants = {}
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
18 for v, k, m in self._constants:
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
19 self.k = v
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
20 self.constants[v] = k
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
21 self.rev_constants[k] = v
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
22 self.descriptions[v] = m
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
23
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
24 def __getitem__(self, k):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
25 if isinstance(k, basestring):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
26 return self.rev_constants[k]
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
27 else:
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
28 return self.constants[k]
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
29
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
30 def get_description(self, k):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
31 if isinstance(k, basestring):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
32 k = self.rev_constants[k]
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
33 return self.descriptions[k]
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
34
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
35 class MODE(Constants):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
36 _constants = [(1, "CONTROLLER", "Set device as Controller in Charge"),
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
37 (0, "DEVICE", "Set device as simple listener"),
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
38 ]
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
39
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
40 class ModeCommand(object):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
41 def __init__(self, description, name, condition=None):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
42 self.name = name
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
43 self.description = description
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
44 self.condition = condition
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
45
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
46 class CommandRegister(object):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
47 _instances = {}
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
48 _registered_type = None #Command
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
49 def __new__(cls):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
50 # implements a singleton *per class*
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
51 if cls.__name__ not in cls._instances:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
52 if cls._registered_type is None:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
53 cls._registered_type = Command
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
54 instance = super(CommandRegister, cls).__new__(cls)
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
55 instance.registry = {}
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
56 cls._instances[cls.__name__] = instance
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
57 return cls._instances[cls.__name__]
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
58
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
59 @classmethod
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
60 def add(cls, registered_cls):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
61 for registry in cls._instances.values():
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
62 if registry.__module__ == registered_cls.__module__:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
63 break
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
64 else:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
65 return
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
66
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
67 assert issubclass(registered_cls, registry._registered_type)
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
68 if registered_cls is registry._registered_type:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
69 return
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
70
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
71 name = registered_cls.__name__
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
72 if name not in registry:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
73 registry.registry[name] = registered_cls
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
74
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
75 def __contains__(self, key):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
76 return key in self.registry
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
77 def keys(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
78 return self.registry.keys()
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
79 def items(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
80 return self.registry.items()
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
81 def values(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
82 return self.registry.values()
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
83 def __getitem__(self, key):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
84 return self.registry[key]
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
85
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
86 class MetaCommand(type):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
87 """
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
88 Metaclass for HPIB command. Used to register all commands.
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
89 """
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
90 _commands = {}
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
91 def __init__(cls, name, bases, dct):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
92 # called at class creation
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
93 super(MetaCommand, cls).__init__(name, bases, dct)
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
94 if name not in [ "AbstractCommand","Command"]:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
95 CommandRegister().add(cls)
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
96
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
97 class AbstractCommand(object):
54
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
98 """
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
99 Base class for HPIB command descritption.
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
100
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
101 This is actually a attribute descriptor, which should have
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
102 AbstractGPIBDevice derived classes as owner.
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
103 """
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
104 __metaclass__ = MetaCommand
54
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
105 _readonly = True
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
106 _init_value = None
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
107
54
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
108 def __get__(self, instance, owner):
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
109 if instance is None:
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
110 return self
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
111 return instance._get(self.__class__.__name__)
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
112 def __set__(self, instance, value):
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
113 if instance is None:
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
114 return self
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
115 return instance._set(self.__class__.__name__, value)
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
116
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
117 class Command(AbstractCommand):
57
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
118 def build_get_cmd(self):
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
119 return self.__class__.__name__
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
120
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
121 def build_set_cmd(self, *value):
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
122 raise ValueError, "Can't set value for command '%s'"%self.__class__.__name__
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
123
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
124 def convert_from(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
125 return None
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
126
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
127 class AbstractValue(Command):
54
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
128 _readonly = False
57
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
129 def build_get_cmd(self):
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
130 return self.__class__.__name__ + "?"
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
131
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
132 def build_set_cmd(self, *value):
54
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
133 value = self.convert_to(value)
57
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
134 cmd = "%s %s"%(self.__class__.__name__, value)
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
135 return cmd
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
136
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
137 def convert_to(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
138 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
139 return str(value[0])
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
140 return ""
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
141
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
142 def convert_from(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
143 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
144 return self._type(value[0].strip())
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
145 return None
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
146
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
147 class BoolValue(AbstractValue):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
148 _type = bool
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
149 def convert_from(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
150 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
151 return value[0] and value[0].lower() in ['1','true','yes','on']
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
152 return False
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
153
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
154 def convert_to(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
155 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
156 return value[0] and "1" or "0"
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
157 return "" # XXX is it correct?
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
158
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
159 class IntValue(AbstractValue):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
160 _type = int
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
161 def convert_from(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
162 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
163 # int is resturned as a string representing a float
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
164 return self._type(float(value[0].strip()))
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
165 return None
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
166
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
167 class flag(int):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
168 def __new__(cls, val, constants):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
169 val = int.__new__(cls, val)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
170 val._constants = constants
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
171 for v, name, desc in constants:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
172 if name not in ['', 'N/A']:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
173 setattr(val, name, v)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
174 return val
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
175
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
176 def __str__(self):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
177 return '%s <' + '|'.join([x[1] for x in self._constants if x[0]&self]) + ">"
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
178 def flags(self):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
179 return [x[1] for x in self._constants if x[0] & (self and x[1]) not in ['','N/A']]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
180
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
181 def descriptions(self):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
182 return [x[2] for x in self._constants if (x[0] & self) and x[1] not in ['','N/A']]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
183
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
184 class Flag(IntValue):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
185 _readonly = True
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
186 _constants = []
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
187 _type = flag
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
188 def convert_from(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
189 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
190 return self._type(float(value[0].strip()), _constants)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
191 return None
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
192
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
193 class FloatValue(AbstractValue):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
194 _type = float
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
195
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
196 class PercentageValue(FloatValue):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
197 pass #TODO
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
198
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
199 class FloatUnitValue(FloatValue):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
200 """
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
201 A Float value with unit (frequency, etc.)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
202 """
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
203 def convert_to(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
204 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
205 if len(value)==1:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
206 if isinstance(value[0], basestring):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
207 value = value[0].strip().split()
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
208 if len(value)==1:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
209 freq = float(value)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
210 unit = self._units[0]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
211 elif len(value)==2:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
212 freq = float(value[0])
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
213 unit = value[1]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
214 else:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
215 raise ValueError, "Can't interpret %s as a %s"%(repr(value), self._name)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
216 else:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
217 freq = float(value[0])
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
218 unit = self._units[0]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
219 elif len(value)==2:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
220 freq = float(value[0])
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
221 unit = value[1]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
222 else:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
223 raise ValueError, "Can't interpret %s as a %s"%(repr(value), self._name)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
224 assert unit.lower() in self._units, "Unit is not correct (%s)"%unit
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
225 return "%s%s"%(freq, unit)
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
226 return "" # XXX is it correct?
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
227
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
228 class FrequencyValue(FloatUnitValue):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
229 _units = ['Hz','kHz','mHz',]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
230 _name = "frequency"
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
231
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
232 class VoltageValue(FloatUnitValue):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
233 _units = ['V','mV',]
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
234 _name = "voltage"
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
235
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
236 class DurationValue(FloatUnitValue):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
237 _units = ['sec','msec','usec','min']
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
238 _name = "duration"
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
239
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
240 class StringValue(AbstractValue):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
241 _type = str
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
242 def convert_to(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
243 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
244 return "'%s'"%str(value[0])
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
245 return "''"
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
246
58
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
247 class EnumValue(StringValue):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
248 _values = []
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
249 def convert_to(self, *value):
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
250 if value:
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
251 assert value[0] in self._values
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
252 return "%s"%self._values.index(value[0])
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
253 return "" # XXX
53
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
254
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
255 class Mode(AbstractValue):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
256 pass
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
257
12
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
258 # TODO
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
259 # class STATUS_BYTE(Constants):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
260 # # IEEE 488.2 Status Byte constants
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
261 # MAV = 0x10 # Message AVailable: bit 4 of the Status Byte
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
262 # ESB = 0x20 # Event Status Bit: bit 5 of the Status Byte
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
263 # MSS = 0x40 # Master Summary Status bit: bit 6 of the Status Byte (NOT
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
264 # # sent in response to a serial poll)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
265 # RQS = 0x40 # Request Service: bit 6 of the Status Byte (when sent in
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
266 # # response to a serial poll)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
267 # class SESR(Constants):
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
268 # # SESR constants (Standard Event Status Register)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
269 # PON = 0x80 # Power On: Power has been turned On since last register
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
270 # # read access
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
271 # URQ = 0x40 # User Request: the user has activated some device control
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
272 # # (whatever the Remote Local state is)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
273 # CME = 0x20 # Command Error
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
274 # EXE = 0x10 # Execution Error
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
275 # DDE = 0x08 # Device Dependant Error
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
276 # QYE = 0x04 # QuerY Error (attempt to read data while Output Queue is
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
277 # # empty, or data in the OQ was lost)
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
278 # RQC = 0x02 # Request Control: tell the CiC that the device wants to
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
279 # # become the CiC
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
280 # OPC = 0x01 # Operation Complete: device has completed any pending
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
281 # # operation (ready to accept new commands). This bit is
a04bea92c509 some code refactoring and several improvements in gpib_plotter (should be more robust & quicker)
David Douard <david.douard@logilab.fr>
parents: 9
diff changeset
282 # # generated in response to a OPC command.
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
283

mercurial