Mon, 04 Jun 2018 22:27:41 +0200
[plotter] use OSError instead of more specific SerialException
79 | 1 | # This program is free software; you can redistribute it and/or modify it under |
2 | # the terms of the GNU General Public License as published by the Free Software | |
3 | # Foundation; either version 2 of the License, or (at your option) any later | |
4 | # version. | |
5 | # | |
6 | # This program is distributed in the hope that it will be useful, but WITHOUT | |
7 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
8 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details | |
9 | # | |
10 | # You should have received a copy of the GNU General Public License along with | |
11 | # this program; if not, write to the Free Software Foundation, Inc., | |
12 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
13 | """ Copyright (c) 2007-2008 David Douard (Paris, FRANCE). | |
91
f2a8f688dbc0
project moved to bitbucket
David Douard <david.douard@logilab.fr>
parents:
79
diff
changeset
|
14 | https://bitbucket.org/dddouard/pygpibtoolkit -- mailto:david.douard@sdfa3.org |
79 | 15 | |
16 | Module defining a mockup GPIB communication object. | |
17 | ||
18 | """ | |
19 | ||
20 | class BaseMockup(object): | |
21 | _idnstr = "BaseMockup" | |
22 | _idncmd = "*IDN?" | |
23 | ||
24 | _cmds = {} | |
25 | ||
26 | def __init__(self): | |
27 | #self._current_cmd = None | |
28 | self._current_data = None | |
29 | ||
30 | def cmd(self, cmd, *args): | |
31 | if cmd == self._idncmd: | |
32 | return self._idnstr | |
33 | try: | |
34 | if ' ' in cmd: | |
35 | cmd, cmdargs = cmd.split(None, 1) | |
36 | args = args + (cmdargs,) | |
37 | _cmd = getattr(self, cmd) | |
38 | self._current_data = _cmd(*args) | |
39 | except AttributeError: | |
40 | if cmd in self._cmds: | |
41 | self._current_data = iter(self._cmds[cmd]) | |
42 | else: | |
43 | raise ValueError('Unknown command %s'%repr(cmd)) | |
44 | else: | |
45 | if isinstance(self._current_data, basestring): | |
46 | ret = self._current_data | |
47 | self._current_data = None | |
48 | return ret | |
49 | elif self._current_data is None: | |
50 | return None | |
51 | else: | |
52 | try: | |
53 | return self._current_data.next() | |
54 | except StopIteration: | |
55 | self._current_data = None | |
56 | return None | |
57 | return None | |
58 | ||
59 | def read(self): | |
60 | try: | |
61 | return self._current_data.next() | |
62 | except StopIteration: | |
63 | self._current_data = None | |
64 | return None | |
65 | except: | |
66 | return None | |
67 | ||
68 | GPIB_CONTROLLER = 1 | |
69 | GPIB_DEVICE = 0 | |
70 | ||
71 | class GPIB(object): | |
72 | _retries = 15 | |
73 | def __init__(self, address=0, mode=1, **kw): | |
74 | """ | |
75 | Create a new mockup GPIB controller. | |
76 | """ | |
77 | self.set_mode(mode) | |
78 | self.set_address(address) | |
79 | self._devices = {} | |
80 | ||
81 | def adddevice(self, add, mockup): | |
82 | self._devices[add] = mockup | |
83 | ||
84 | def set_address(self, address, check=True): | |
85 | """ | |
86 | Set the address of the GPIB device: | |
87 | ||
88 | - if the device is the Controller In Charge, this is the | |
89 | address of the device commands are sent to, | |
90 | ||
91 | - if the device is in GPIB_DEVICE mode, this is its address. | |
92 | """ | |
93 | self._address = address | |
94 | ||
95 | def set_mode(self, mode): | |
96 | """ | |
97 | Set GPIB device mode to 'mode': | |
98 | ||
99 | - GPIB_CONTROLLER: set the device as the Controller In Charge | |
100 | on the GPIB bus | |
101 | ||
102 | - GPIB_DEVICE: set the device as a standard GPIB device on the | |
103 | bus. | |
104 | """ | |
105 | self._mode = mode | |
106 | ||
107 | def set_controller(self): | |
108 | """ | |
109 | Set GPIB device the Controller In Charge on the GPIB bus. | |
110 | """ | |
111 | self.set_mode(1) | |
112 | ||
113 | def set_device(self): | |
114 | """ | |
115 | Set the GPIB device as a simple device on the GPIB bus. | |
116 | """ | |
117 | self.set_mode(0) | |
118 | ||
119 | def send_command(self, cmd, address=None): | |
120 | """ | |
121 | Send the specified GPIB command on the bus (must be the CIC), | |
122 | and read the answer. | |
123 | Eventually, set the addressed device first. | |
124 | """ | |
125 | assert self._mode == 1 | |
126 | if address is not None: | |
127 | self.set_address(address) | |
128 | ||
129 | if self._address in self._devices: | |
130 | return self._devices[self._address].cmd(cmd) | |
131 | return None | |
132 | ||
133 | def read_eoi(self, address=None): | |
134 | """ | |
135 | Read the HPIB buffer from device, till EOI is performed, or timeout. | |
136 | """ | |
137 | if address is not None: | |
138 | self.set_address(address) | |
139 | if self._address in self._devices: | |
140 | return "".join(self._devices[self._address].read()) | |
141 | ret = "" | |
142 | return ret | |
143 | ||
144 | def check_srq(self): | |
145 | """ | |
146 | Check the SRQ line | |
147 | """ | |
148 | assert self._mode == 1, "must be the Controller In Charge" | |
149 | return None | |
150 | ||
151 | def poll(self, addresses=None): | |
152 | """ | |
153 | Poll every address, and return a dictionnary | |
154 | {add: status, ...} | |
155 | """ | |
156 | assert self._mode == 1, "must be the Controller In Charge" | |
157 | ret = {} | |
158 | if addresses is None: | |
159 | for add, mock in self._devices.items(): | |
160 | ret[add] = mock._idnstr | |
161 | return ret | |
162 | return {} | |
163 | ||
164 | def reset(self): | |
165 | """ | |
166 | Perform a reset of the USB device | |
167 | ||
168 | """ | |
169 | pass | |
170 |