pygpibtoolkit/gpibcontroller.py

Thu, 28 Feb 2008 18:35:23 +0100

author
David Douard <david.douard@logilab.fr>
date
Thu, 28 Feb 2008 18:35:23 +0100
changeset 61
6ecb0fd8c129
parent 59
d8bd4a931516
child 62
486a480d196b
permissions
-rw-r--r--

cleanup: removed useless commandregister (and fix some missing import)

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:
diff changeset
1 #
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:
diff changeset
2 """
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:
diff changeset
3 A general purpose GPIB controller based on prologix GPIB device
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:
diff changeset
4 """
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:
diff changeset
5 import sys
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:
diff changeset
6 import threading
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:
diff changeset
7 import time
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
8 from inspect import isclass
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
9
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
10 from serial.serialutil import SerialException
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:
diff changeset
11 from prologix import GPIB, GPIB_CONTROLLER, GPIB_DEVICE
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
12 from pygpibtoolkit.pygpib import AbstractCommand
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
13
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
14 class AbstractGPIBDeviceMetaclass(type):
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
15 def __new__(mcs, name, bases, classdict):
59
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
16 # Automatically add commands to Device classes.
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
17 # Commands which name starts with '_' are ignored.
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
18 # Only commands defined in the same module as the device
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
19 # are added to the device.
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
20 # Commands are descriptors (see pygpib.py)
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
21 new_cls = type.__new__(mcs, name, bases, classdict)
59
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
22 modname = new_cls.__module__
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
23 module = sys.modules[modname]
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
24 for pname, param in module.__dict__.items():
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
25 if not pname.startswith('_') and isclass(param) and \
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
26 issubclass(param, AbstractCommand) and \
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
27 param.__module__ == modname:
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
28 setattr(new_cls, pname, param())
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
29 return new_cls
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:
diff changeset
30
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:
diff changeset
31 class AbstractGPIBDevice(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
32 __metaclass__ = AbstractGPIBDeviceMetaclass
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:
diff changeset
33 _accepts = []
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
34
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:
diff changeset
35 @classmethod
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36 def accepts(cls, idn):
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:
diff changeset
37 return idn in cls._accepts
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:
diff changeset
38
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:
diff changeset
39 def __init__(self, idn, address, controller):
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:
diff changeset
40 self._idn = idn
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:
diff changeset
41 self._address = address
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:
diff changeset
42 self._controller = controller
59
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
43 self._use_cache = True
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
44
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
45 self._cache = {}
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
46 for pname, param in self.__class__.__dict__.items():
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
47 if isinstance(param, AbstractCommand) and not param._readonly:
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
48 self._cache[pname] = param._init_value
59
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
49 def use_cache(self, use=None):
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
50 if use is None:
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
51 return self._use_cache
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
52 self._use_cache = bool(use)
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
53
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
54 def _get(self, name):
59
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
55 if self._use_cache and name in self._cache and self._cache[name] is not None:
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
56 return self._cache[name]
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
57
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
58 param = getattr(self.__class__, name)
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
59 cmd = param.build_get_cmd()
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
60 value = self._controller.send_command(self._address, cmd)
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
61 value = param.convert_from(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
62 if name in self._cache:
59
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
63 self._cache[name] = value
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
64 return value
d8bd4a931516 Make it possible to disable the cache mecanism temporarly and make AbstractCommand derivated classes not considered as commands if their name starts with '_'.
David Douard <david.douard@logilab.fr>
parents: 57
diff changeset
65
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
66 def _set(self, name, 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
67 param = getattr(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
68 cmd = param.build_set_cmd(value)
7013c4bebc7b Some improvements & cleanups. Now the get/set mecanisme using descriptor works (but is not yet implemented for most commands of the HP356X devices).
David Douard <david.douard@logilab.fr>
parents: 54
diff changeset
69 res = self._controller.send_command(self._address, cmd)
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 if name in self._cache:
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 self._cache[name] = 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
72 return res
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
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
74 def manage_srq(self, statusbyte):
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:
diff changeset
75 pass
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
76
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:
diff changeset
77 def send_command(self, cmd):
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:
diff changeset
78 return self._controller.send_command(self._address, cmd).strip()
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:
diff changeset
79
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:
diff changeset
80 class GPIBDeviceRegister(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:
diff changeset
81 def __init__(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
82 self._registry = []
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 def register_manager(self, mgr):
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:
diff changeset
84 self._registry.append(mgr)
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:
diff changeset
85 def get_manager(self, idn):
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:
diff changeset
86 for mgr in self._registry:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
87 if mgr.accepts(idn):
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:
diff changeset
88 return mgr
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:
diff changeset
89 return 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:
diff changeset
90 def get_idn_cmds(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
91 return [mgr._idn for mgr in self._registry]
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
92 def __str__(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
93 msg = "<GPIBDeviceRegister: %s managers\n"%len(self._registry)
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
94 for mgr in self._registry:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
95 msg += " %s: %s\n"%(mgr.__name__, ", ".join(mgr._accepts))
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:
diff changeset
96 msg += ">"
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:
diff changeset
97 return msg
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:
diff changeset
98
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:
diff changeset
99 deviceRegister = GPIBDeviceRegister()
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:
diff changeset
100
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:
diff changeset
101 class GenericGPIBDevice(AbstractGPIBDevice):
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:
diff changeset
102 _idn = "*IDN?"
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:
diff changeset
103
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:
diff changeset
104 deviceRegister.register_manager(GenericGPIBDevice)
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:
diff changeset
105
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:
diff changeset
106 class GPIBController(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:
diff changeset
107 """
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:
diff changeset
108 The main GPIB Controller In Charge.
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:
diff changeset
109
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:
diff changeset
110 This is responsible for any communication with the registered
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:
diff changeset
111 devices.
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:
diff changeset
112
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:
diff changeset
113 It hold a thread reponsible for communication with devices,
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:
diff changeset
114 performing spolls regularly. It can react and call callbacks on
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:
diff changeset
115 events detected while spolling.
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:
diff changeset
116
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
117 It also keeps a command queue which devices/user program can fill
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:
diff changeset
118 with GPIB and device commands, which are executed ASAP (ie. as
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:
diff changeset
119 soon as the GPIB bus if free and the device states itself as
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:
diff changeset
120 ready.)
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:
diff changeset
121
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:
diff changeset
122 """
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:
diff changeset
123 def __init__(self, device="/dev/ttyUSB0", controller_address=21):
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:
diff changeset
124 self._address = controller_address
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
125 try:
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
126 self._cnx = GPIB(device)
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
127 except SerialException:
5c1a312830e7 An AbstractGPIBDevice and the Command should now behave quite OK ie. one can type d.MyCommand and it will return the value (either from the cache or after having asked it to the device), and use d.FRS = 10000 to send the command "FRS 10000 Hz" to the device transparently.
David Douard <david.douard@logilab.fr>
parents: 53
diff changeset
128 self._cnx = 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:
diff changeset
129 self._devices = {}
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:
diff changeset
130 self._setup_threading_system()
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:
diff changeset
131
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:
diff changeset
132 def __del__(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
133 print "deleting controller"
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:
diff changeset
134 self._stop.set()
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:
diff changeset
135
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:
diff changeset
136 def _setup_threading_system(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
137 self._n_cmds = 0
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:
diff changeset
138 self._n_cmds_lock = threading.RLock()
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:
diff changeset
139 self._cmd_queue = {}
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:
diff changeset
140 self._cmd_condition = threading.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:
diff changeset
141 self._results_queue = {}
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:
diff changeset
142 self._results_condition = threading.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:
diff changeset
143 self._loop_interrupter = threading.Event()
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:
diff changeset
144 self._stop = threading.Event()
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:
diff changeset
145 self._cnx_thread = threading.Thread(name="GPIBConnectionThread",
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:
diff changeset
146 target=self._cnx_loop)
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:
diff changeset
147 self._cnx_thread.start()
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:
diff changeset
148 self._loop_interrupter.set()
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:
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:
diff changeset
150 def _check_srq(self):
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
151 if self._cnx and self._cnx.check_srq():
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:
diff changeset
152 addrs = sorted(self._devices.keys())
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
153 polled = self._cnx.poll(addrs)
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:
diff changeset
154 for add in addrs:
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:
diff changeset
155 if polled[add] & 0x40:
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:
diff changeset
156 print "device %s (#%s) requested attention"%(self._devices[add]._idn, add)
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:
diff changeset
157 # TODO: check what to do...
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
158 self._devices[add].manage_srq(polled[add])
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:
diff changeset
159
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:
diff changeset
160 def _cnx_loop(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
161 while 1:
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:
diff changeset
162 #sys.stderr.write('.')
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:
diff changeset
163 if self._stop.isSet():
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:
diff changeset
164 print "_stop set, exiting"
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:
diff changeset
165 return
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
166 while not self._loop_interrupter.isSet():
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:
diff changeset
167 #sys.stderr.write('|')
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:
diff changeset
168 self._loop_interrupter.wait()
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:
diff changeset
169 self._loop_interrupter.clear()
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:
diff changeset
170 # first we check for SRQ and dispatch its management
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:
diff changeset
171 self._check_srq()
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:
diff changeset
172 # then we check if we have any pending commands.
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
173 self._run_one_cmd()
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:
diff changeset
174 self._loop_interrupter.set()
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:
diff changeset
175 time.sleep(0.1)
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:
diff changeset
176
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:
diff changeset
177 def _run_one_cmd(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
178 cond = self._cmd_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:
diff changeset
179 cond.acquire()
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:
diff changeset
180 if len(self._cmd_queue) == 0:
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:
diff changeset
181 cond.release()
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:
diff changeset
182 return
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
183 n_cmd = min(self._cmd_queue.keys())
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
184 addr, cmd, cb = self._cmd_queue.pop(n_cmd)
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:
diff changeset
185 cond.release()
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:
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:
diff changeset
187 # TODO: check device is RDY etc.
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:
diff changeset
188 resu = self._cnx.send_command(cmd, addr)
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:
diff changeset
189 # TODO: check any error etc.
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:
diff changeset
190 if isinstance(cb, threading._Event):
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:
diff changeset
191 cond = self._results_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:
diff changeset
192 cond.acquire()
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:
diff changeset
193 self._results_queue[n_cmd] = resu
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:
diff changeset
194 cond.notifyAll()
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:
diff changeset
195 cond.release()
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:
diff changeset
196 cb.set()
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:
diff changeset
197 else:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
198 if callable(cb):
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:
diff changeset
199 cb(resu)
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:
diff changeset
200
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:
diff changeset
201 def stop(self, *args):
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:
diff changeset
202 self._stop.set()
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:
diff changeset
203
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:
diff changeset
204 def send_command(self, addr, cmd, sync=True, cb=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:
diff changeset
205 if cb is not None and callable(cb):
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:
diff changeset
206 sync = False
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:
diff changeset
207 self._n_cmds_lock.acquire()
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:
diff changeset
208 self._n_cmds += 1
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:
diff changeset
209 n_cmd = self._n_cmds
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:
diff changeset
210 self._n_cmds_lock.release()
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:
diff changeset
211
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:
diff changeset
212 cond = self._cmd_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:
diff changeset
213 cond.acquire()
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:
diff changeset
214 if sync:
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:
diff changeset
215 cb = threading.Event()
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:
diff changeset
216 self._cmd_queue[n_cmd] = (addr, cmd, cb)
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:
diff changeset
217 cond.notify()
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:
diff changeset
218 cond.release()
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:
diff changeset
219 if sync:
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:
diff changeset
220 cb.wait()
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:
diff changeset
221 cond = self._results_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:
diff changeset
222 cond.acquire()
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:
diff changeset
223 while n_cmd not in self._results_queue:
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:
diff changeset
224 cond.wait()
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:
diff changeset
225 resu = self._results_queue.pop(n_cmd)
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:
diff changeset
226 cond.release()
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:
diff changeset
227 return resu
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:
diff changeset
228
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:
diff changeset
229 def detect_devices(self):
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
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:
diff changeset
231 Perform a Serial Poll on all addresses to detect alive devices
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:
diff changeset
232 connected on the GPIB bus.
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:
diff changeset
233 """
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:
diff changeset
234 while not self._loop_interrupter.isSet():
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:
diff changeset
235 self._loop_interrupter.wait()
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:
diff changeset
236 self._loop_interrupter.clear()
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:
diff changeset
237 try:
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:
diff changeset
238 spoll = self._cnx.poll()
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:
diff changeset
239 for address in spoll:
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:
diff changeset
240 if address not in self._devices:
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:
diff changeset
241 # found a new device
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:
diff changeset
242 for id_str in deviceRegister.get_idn_cmds():
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:
diff changeset
243 idn = self._cnx.send_command(id_str, address).strip()
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:
diff changeset
244 if idn:
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:
diff changeset
245 self.register_device(address, idn)
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:
diff changeset
246 break
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
247 else:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
248 # redo a spoll, it should have set the err bit
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:
diff changeset
249 poll = self._cnx.poll(address)
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:
diff changeset
250 if not poll & 0x20: # 0x20 == ERR bit
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:
diff changeset
251 print "Strange, device %d did not answer to a %s command without setting its ERR bit"%(address, id_str)
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:
diff changeset
252 else:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
253 print "Can't retrieve IDN of device at address ", address
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:
diff changeset
254 finally:
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:
diff changeset
255 self._loop_interrupter.set()
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:
diff changeset
256
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:
diff changeset
257 def register_device(self, address, idn):
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:
diff changeset
258 """
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:
diff changeset
259 Register a device manager for device at given GPIB
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:
diff changeset
260 address. The manager is retrived from device registry.
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
261 """
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:
diff changeset
262 devicemgr = deviceRegister.get_manager(idn)
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:
diff changeset
263 if devicemgr is not 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:
diff changeset
264 devicemgr = devicemgr(idn, address, self)
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
265 self._devices[address] = devicemgr
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:
diff changeset
266 return devicemgr
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:
diff changeset
267 else:
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
268 print "can't find a manager for", repr(idn)
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:
diff changeset
269 print deviceRegister._registry
8e32c806fcdd Major refactoring in progress. Build the toolkit around a GPIB controller which have a communication thread with the devices. Every device is an instance of a class that describes the device model and registers itself to the controller.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
270
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:
diff changeset
271 def idn(self, address):
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:
diff changeset
272 """
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:
diff changeset
273 Query identity of the addressed device.
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:
diff changeset
274 """
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:
diff changeset
275 return self.send_command(address, self._devices[address].__class__._idn).strip()
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:
diff changeset
276
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:
diff changeset
277 def status(self, address):
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:
diff changeset
278 """
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:
diff changeset
279 Query status byte for device at given address
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:
diff changeset
280 """
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:
diff changeset
281 while not self._loop_interrupter.isSet():
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:
diff changeset
282 self._loop_interrupter.wait()
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:
diff changeset
283 self._loop_interrupter.clear()
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:
diff changeset
284 try:
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:
diff changeset
285 return self._cnx.poll(address)
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:
diff changeset
286 finally:
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:
diff changeset
287 self._loop_interrupter.set()
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:
diff changeset
288
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:
diff changeset
289

mercurial