pygpibtoolkit/pygpib.py

Tue, 19 Aug 2008 23:01:19 +0200

author
David Douard <david.douard@logilab.fr>
date
Tue, 19 Aug 2008 23:01:19 +0200
changeset 65
10d218fbf86f
parent 62
486a480d196b
child 66
2a97995628a3
permissions
-rw-r--r--

fixes to make gpib_detect works

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
65
10d218fbf86f fixes to make gpib_detect works
David Douard <david.douard@logilab.fr>
parents: 62
diff changeset
9 import operator
2
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
10
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 class ConnectionError(Exception):
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
12 pass
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
13
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
14 class Condition(object):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
15 def __call__(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
16 return True
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
17 def __and__(self, cond):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
18 assert isinstance(cond, Condition)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
19 return _ComposedCondition(operator.__and__, self, cond)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
20 def __or__(self, cond):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
21 assert isinstance(cond, Condition)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
22 return _ComposedCondition(operator.__or__, self, cond)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
23
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
24 class _ComposedCondition(Condition):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
25 def __init__(self, op, *args):
65
10d218fbf86f fixes to make gpib_detect works
David Douard <david.douard@logilab.fr>
parents: 62
diff changeset
26 self._conditions = list(args[:])
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
27 self._reduc_op = op
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
28 for cond in args:
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
29 assert isinstance(cond, Condition)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
30 if isinstance(cond, _ComposedCondition) and cond._reduc_op == op:
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
31 i = self._conditions.index(cond)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
32 self._conditions[i:i+1] = cond._conditions
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
33 def __call__(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
34 return reduce(self._reduc_op, [f(device) for f in self._conditions])
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
35
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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46
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
47 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
48 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
49 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
50 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
51 return self.constants[k]
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
52
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
53 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
54 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
55 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
56 return self.descriptions[k]
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
57
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
58 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
59 _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
60 (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
61 ]
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
62
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 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
64 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
65 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
66 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
67 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
68
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 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
70 """
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
71 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
72
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
73 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
74 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
75 """
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
76 _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
77 _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
78
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
79 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
80 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
81 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
82 return instance._get(self.__class__.__name__)
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
83
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
84 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
85 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
86 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
87 return instance._set(self.__class__.__name__, value)
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
88
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
89 def get_value_from_device(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
90 cmd = self.build_get_cmd()
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
91 value = device.send_command(cmd)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
92 value = self.convert_from(value)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
93 return 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
94
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
95 def send_value_to_device(self, device, value):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
96 cmd, value = self.build_set_cmd(value)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
97 res = device.send_command(cmd)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
98 return res, self.convert_to(value)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
99
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
100 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
101 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
102
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
103 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
104 raise ValueError, "Can't set value for command '%s'"%self.__class__.__name__
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
105
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
106 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
107 return None
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
108
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
109 def convert_to(self, *value):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
110 return None
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
111
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
112
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
113 #XXX TODO: remove this
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
114 class Command(AbstractCommand):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
115 pass
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
116
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
117 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
118 _readonly = False
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
119
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
120 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
121 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
122
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
123 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
124 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
125 cmd = "%s %s"%(self.__class__.__name__, value)
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
126 return cmd, 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
127
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
128 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
129 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
130 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
131 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
132
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
133 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
134 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
135 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
136 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
137
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
138 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
139 _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
140 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
141 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
142 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
143 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
144
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 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
146 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
147 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
148 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
149
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
150 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
151 _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
152 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
153 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
154 # 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
155 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
156 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
157
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
158 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
159 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
160 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
161 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
162 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
163 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
164 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
165 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
166
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 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
168 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
169 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
170 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
171
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 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
173 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
174
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 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
176 _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
177 _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
178 _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
179 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
180 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
181 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
182 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
183
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
184 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
185 _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
186
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
187 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
188 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
189
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
190 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
191 """
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 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
193 """
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
194 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
195 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
196 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
197 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
198 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
199 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
200 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
201 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
202 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
203 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
204 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
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218
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
219 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
220 _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
221 _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
222
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 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
224 _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
225 _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
226
98a94ec5f24e make convert_from prototype accepts several arguments (to build command with several arguments)
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
227 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
228 _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
229 _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
230
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
231 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
232 _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
233 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
234 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
235 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
236 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
237
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
238 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
239 _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
240 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
241 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
242 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
243 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
244 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
245
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 class Mode(AbstractValue):
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
247 _DATA_BLOCK = None
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
248 _key = None
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
249
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
250 def __init__(self):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
251 self._cmds = []
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
252 for cmdname, cmd in self.__dict__.items():
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
253 if isinstance(cmd, ModeCommand):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
254 self._cmds.append(cmdname)
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
255
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
256 def get_mode(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
257 if self._key and self._DATA_BLOCK:
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
258 mode = getattr(getattr(device, self._DATA_BLOCK), self._key)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
259 return mode
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
260 return None
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
261
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
262 def get_value_from_device(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
263 value = self.get_mode(device)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
264 return value
61
6ecb0fd8c129 cleanup: removed useless commandregister (and fix some missing import)
David Douard <david.douard@logilab.fr>
parents: 58
diff changeset
265
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
266 def __get__(self, instance, owner):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
267 if instance is None:
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
268 return self
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
269 return instance._get(self.__class__.__name__)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
270
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
271 def build_set_command(self, value):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
272 assert value in self._cmds
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
273 return value
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
274
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
275 # 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
276 # 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
277 # # 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
278 # 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
279 # 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
280 # 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
281 # # 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
282 # 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
283 # # 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
284 # 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
285 # # 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
286 # 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
287 # # 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
288 # 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
289 # # (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
290 # 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
291 # 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
292 # 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
293 # 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
294 # # 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
295 # 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
296 # # 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
297 # 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
298 # # 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
299 # # 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
300

mercurial