-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpib_server.py
234 lines (198 loc) · 7.73 KB
/
gpib_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Copyright (C) 2008 Matthew Neeley
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# CHANGELOG
#
# 2011 December 10 - Peter O'Malley & Jim Wenner
#
# Fixed bug where doesn't add devices if no SOCKETS connected.
#
# 2011 December 5 - Jim Wenner
#
# Added ability to read TCPIP (Ethernet) devices if configured to use
# sockets (i.e., fixed port address). To do this, added getSocketsList
# function and changed refresh_devices.
#
# 2011 December 3 - Jim Wenner
#
# Added ability to read TCPIP (Ethernet) devices. Must be configured
# using VXI-11 or LXI so that address ends in INSTR. Does not accept if
# configured to use sockets. To do this, changed refresh_devices.
#
# To be clear, the gpib system already supported ethernet devices just fine
# as long as they weren't using raw socket protocol. The changes that
# were made here and in the next few revisions are hacks to make socket
# connections work, and should be improved.
from labrad.server import LabradServer, setting
from twisted.internet.defer import inlineCallbacks
from twisted.internet.reactor import callLater
from labrad.errors import DeviceNotSelectedError
import labrad.units as units
import pyvisa as visa
"""
### BEGIN NODE INFO
[info]
name = GPIB Bus
version = 1.5.0-no-refresh
description = Gives access to GPIB devices via pyvisa.
instancename = %LABRADNODE% GPIB Bus
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 20
### END NODE INFO
"""
KNOWN_DEVICE_TYPES = ('GPIB', 'TCPIP', 'USB')
class GPIBBusServer(LabradServer):
"""Provides direct access to GPIB-enabled devices."""
name = '%LABRADNODE% GPIB Bus'
refreshInterval = 10
defaultTimeout = 1.0 * units.s
def initServer(self):
self.devices = {}
# start refreshing only after we have started serving
# this ensures that we are added to the list of available
# servers before we start sending messages
callLater(0.1, self.startRefreshing)
def startRefreshing(self):
"""Start periodically refreshing the list of devices.
The start call returns a deferred which we save for later.
When the refresh loop is shutdown, we will wait for this
deferred to fire to indicate that it has terminated.
"""
# self.refresher = LoopingCall(self.refreshDevices)
#self.refresherDone = self.refresher.start(self.refreshInterval, now=True)
self.refreshDevices()
@inlineCallbacks
def stopServer(self):
"""Kill the device refresh loop and wait for it to terminate."""
if hasattr(self, 'refresher'):
self.refresher.stop()
yield self.refresherDone
def refreshDevices(self):
"""Refresh the list of known devices on this bus.
Currently supported are GPIB devices and GPIB over USB.
"""
try:
rm = visa.ResourceManager()
addresses = [str(x) for x in rm.list_resources()]
additions = set(addresses) - set(self.devices.keys())
deletions = set(self.devices.keys()) - set(addresses)
for addr in additions:
try:
if not addr.startswith(KNOWN_DEVICE_TYPES):
continue
instr = rm.open_resource(addr) #rm.get_instrument(addr)
instr.write_termination = ''
instr.clear()
if addr.endswith('SOCKET'):
instr.write_termination = '\n'
self.devices[addr] = instr
self.sendDeviceMessage('GPIB Device Connect', addr)
except Exception as e:
print('Failed to add ' + addr + ':' + str(e))
for addr in deletions:
del self.devices[addr]
self.sendDeviceMessage('GPIB Device Disconnect', addr)
except Exception as e:
print('Problem while refreshing devices:', str(e))
def sendDeviceMessage(self, msg, addr):
print(msg + ': ' + addr)
self.client.manager.send_named_message(msg, (self.name, addr))
def initContext(self, c):
c['timeout'] = self.defaultTimeout
def getDevice(self, c):
if 'addr' not in c:
raise DeviceNotSelectedError("No GPIB address selected")
if c['addr'] not in self.devices:
raise Exception('Could not find device ' + c['addr'])
instr = self.devices[c['addr']]
return instr
@setting(0, addr='s', returns='s')
def address(self, c, addr=None):
"""Get or set the GPIB address for this context.
To get the addresses of available devices,
use the list_devices function.
"""
if addr is not None:
c['addr'] = addr
return c['addr']
@setting(2, time='v[s]', returns='v[s]')
def timeout(self, c, time=None):
"""Get or set the GPIB timeout."""
if time is not None:
c['timeout'] = time
return c['timeout']
@setting(3, data='s', returns='')
def write(self, c, data):
"""Write a string to the GPIB bus."""
self.getDevice(c).write(data)
@setting(8, data='y', returns='')
def write_raw(self, c, data):
"""Write a string to the GPIB bus."""
self.getDevice(c).write_raw(data)
@setting(4, n_bytes='w', returns='s')
def read(self, c, n_bytes=None):
"""Read from the GPIB bus.
Termination characters, if any, will be stripped.
This includes any bytes corresponding to termination in
binary data. If specified, reads only the given number
of bytes. Otherwise, reads until the device stops sending.
"""
instr = self.getDevice(c)
if n_bytes is None:
ans = instr.read_raw()
else:
ans = instr.read_raw(n_bytes)
return str(ans).strip()
@setting(5, data='s', returns='s')
def query(self, c, data):
"""Make a GPIB query, a write followed by a read.
This query is atomic. No other communication to the
device will occur while the query is in progress.
"""
instr = self.getDevice(c)
instr.write(data)
ans = instr.read_raw()
if isinstance(ans, bytes):
ans = ans.decode('utf-8') # convert from bytes without adding extra characters to string
return str(ans).strip()
@setting(7, n_bytes='w', returns='y')
def read_raw(self, c, n_bytes=None):
"""Read raw bytes from the GPIB bus.
Termination characters, if any, will not be stripped.
If n_bytes is specified, reads only that many bytes.
Otherwise, reads until the device stops sending.
"""
instr = self.getDevice(c)
if n_bytes is None:
ans = instr.read_raw()
else:
ans = instr.read_raw(n_bytes)
return bytes(ans)
@setting(20, returns='*s')
def list_devices(self, c):
"""Get a list of devices on this bus."""
return sorted(self.devices.keys())
@setting(21)
def refresh_devices(self, c):
""" manually refresh devices """
self.refreshDevices()
__server__ = GPIBBusServer()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)