pygpibtoolkit/pygpib.py

Mon, 30 Mar 2009 21:32:14 +0200

author
David Douard <david.douard@logilab.fr>
date
Mon, 30 Mar 2009 21:32:14 +0200
changeset 81
19e4da44795e
parent 79
b8eec4f9db52
child 86
96e30b092f70
permissions
-rw-r--r--

add more commands to HP_8904 device

66
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
1 # This program is free software; you can redistribute it and/or modify it under
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
2 # the terms of the GNU General Public License as published by the Free Software
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
3 # Foundation; either version 2 of the License, or (at your option) any later
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
4 # version.
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
5 #
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
6 # This program is distributed in the hope that it will be useful, but WITHOUT
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
8 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
9 #
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
10 # You should have received a copy of the GNU General Public License along with
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
11 # this program; if not, write to the Free Software Foundation, Inc.,
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
13 """ Copyright (c) 2007-2008 David Douard (Paris, FRANCE).
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
14 http://www.logilab.org/project/pygpibtoolkit -- mailto:david.douard@logilab.fr
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
15
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 65
diff changeset
16 gpib: create serial connection to GPIB-USB device (Prologix is the
2
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17 only supported device for now).
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 """
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
19 import re
2
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 import serial
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 from serial.serialutil import SerialException
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 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
23 from pygpibtoolkit.tools import AbstractRegister
65
10d218fbf86f fixes to make gpib_detect works
David Douard <david.douard@logilab.fr>
parents: 62
diff changeset
24 import operator
2
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 class ConnectionError(Exception):
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 pass
cd9efa64f6da added a gpib module (handle gpib connection)
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
29 class Condition(object):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
30 def __call__(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
31 return True
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
32 def __and__(self, cond):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
33 assert isinstance(cond, Condition)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
34 return _ComposedCondition(operator.__and__, self, cond)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
35 def __or__(self, cond):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
36 assert isinstance(cond, Condition)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
37 return _ComposedCondition(operator.__or__, self, cond)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
38
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
39 class _ComposedCondition(Condition):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
40 def __init__(self, op, *args):
65
10d218fbf86f fixes to make gpib_detect works
David Douard <david.douard@logilab.fr>
parents: 62
diff changeset
41 self._conditions = list(args[:])
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
42 self._reduc_op = op
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
43 for cond in args:
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
44 assert isinstance(cond, Condition)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
45 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
46 i = self._conditions.index(cond)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
47 self._conditions[i:i+1] = cond._conditions
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
48 def __call__(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
49 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
50
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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61
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
62 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
63 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
64 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
65 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
66 return self.constants[k]
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
67
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
68 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
69 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
70 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
71 return self.descriptions[k]
9
3b50c46fca56 several updates to the gpib module
David Douard <david.douard@logilab.fr>
parents: 7
diff changeset
72
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
73 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
74 _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
75 (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
76 ]
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
77
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 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
79 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
80 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
81 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
82 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
83
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 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
85 """
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
86 Base class for HPIB command description.
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
87
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
88 This is actually an attribute descriptor, which should have
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
89 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
90 """
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
91 _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
92 _init_value = None
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
93 _cached = True
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
94 _cmds = []
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
95
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
96 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
97 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
98 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
99 return instance._get(self.__class__.__name__)
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
100
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
101 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
102 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
103 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
104 return instance._set(self.__class__.__name__, value)
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
105
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
106 def get_value_from_device(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
107 cmd = self.build_get_cmd()
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
108 value = device.send_command(cmd)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
109 value = self.convert_from(value)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
110 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
111
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
112 def send_value_to_device(self, device, value):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
113 cmd, value = self.build_set_cmd(value)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
114 res = device.send_command(cmd)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
115 return res, self.convert_to(value)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
116
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
117 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
118 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
119
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
120 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
121 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
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 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
124 return None
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
125
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
126 def convert_to(self, *value):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
127 return None
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
128
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
129
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
130 class Command(AbstractCommand):
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
131 """pure command
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
132 """
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
133
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
134
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
135 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
136 _readonly = False
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
137
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
138 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
139 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
140
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
141 def build_set_cmd(self, *value):
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
142 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
143 cmd = "%s %s"%(self.__class__.__name__, value)
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
144 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
145
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
146 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
147 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
148 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
149 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
150
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 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
152 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
153 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
154 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
155
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
156 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
157 _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
158 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
159 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
160 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
161 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
162
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 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
164 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
165 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
166 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
167
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
168 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
169 _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
170 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
171 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
172 # 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
173 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
174 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
175
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
176 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
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184
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 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
186 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
187 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
188 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
189
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 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
191 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
192
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 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
194 _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
195 _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
196 _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
197 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
198 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
199 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
200 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
201
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
202 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
203 _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
204
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
205 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
206 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
207
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
208 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
209 """
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 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
211 """
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 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
213 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
214 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
215 if isinstance(value[0], basestring):
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
216 value = value[0].strip()
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
217 reunits = '(?P<unit>'+'|'.join(self._units)+')'
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
218 reexpr = r'(?P<value>[-]?[0-9]+([.][0-9]+)?) *' + reunits
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
219 m = re.match(reexpr, value)
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
220 try:
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
221 if m and m.group('unit'):
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
222 value = m.group('value')
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
223 unit = m.group('unit')
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
224 else:
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
225 unit = self._units[0]
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
226 freq = float(value)
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
227 except Exception, e:
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 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 raise ValueError, "Can't interpret %s as a %s"%(repr(value), self._name)
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
237 assert unit in self._units, "Unit is not correct (%s)"%repr(unit)
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 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
239 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
240
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
241 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
242 _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
243 _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
244
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 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
246 _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
247 _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
248
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 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
250 _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
251 _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
252
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
253 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
254 _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
255 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
256 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
257 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
258 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
259
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
260 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
261 _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
262 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
263 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
264 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
265 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
266 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
267
81
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
268 class SimpleMode(AbstractCommand):
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
269 """
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
270 A device mode which can only be wrote (there is no way for reading
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
271 the device mode), thus it must be kept in cache (and we must know
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
272 the init value mode (the subclass must have defined the
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
273 '_init_value' class attribute)).
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
274 """
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
275 def __init__(self):
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
276 self._cmds = []
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
277 for cmdname, cmd in self.__class__.__dict__.items():
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
278 if isinstance(cmd, ModeCommand):
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
279 self._cmds.append(cmdname)
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
280
81
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
281 def build_set_cmd(self, value):
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
282 assert value in self._cmds
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
283 return value, value
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
284
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
285
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
286 class WriteOnlyMode(SimpleMode):
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
287 def get_value_from_device(self, device):
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
288 raise ValueError("can't retrieve mode value from device")
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
289
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
290 def __set__(self, instance, value):
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
291 if instance is None:
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
292 return self
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
293 instance._cache[self.__class__.__name__] = value
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
294 return getattr(instance, value)
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
295
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
296
81
19e4da44795e add more commands to HP_8904 device
David Douard <david.douard@logilab.fr>
parents: 79
diff changeset
297 class Mode(SimpleMode):
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
298 _DATA_BLOCK = None
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
299 _key = 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
300
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
301 def get_mode(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
302 if self._key and self._DATA_BLOCK:
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
303 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
304 return mode
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
305 return None
79
b8eec4f9db52 many improvements:
David Douard <david.douard@logilab.fr>
parents: 66
diff changeset
306
62
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
307 def get_value_from_device(self, device):
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
308 value = self.get_mode(device)
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
309 return value
486a480d196b finish some previous unfinished (!) refactoring
David Douard <david.douard@logilab.fr>
parents: 61
diff changeset
310
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
311 # 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
312 # 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
313 # # 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
314 # 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
315 # 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
316 # 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
317 # # 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
318 # 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
319 # # 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
320 # 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
321 # # 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
322 # 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
323 # # 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
324 # 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
325 # # (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
326 # 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
327 # 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
328 # 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
329 # 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
330 # # 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
331 # 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
332 # # 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
333 # 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
334 # # 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
335 # # 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
336

mercurial