-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdg535.py
141 lines (120 loc) · 4.29 KB
/
dg535.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
# Copyright (C) 2007 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/>.
"""
### BEGIN NODE INFO
[info]
name = DG535
version = 2.0
description = Pulse sequencer box.
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 5
### END NODE INFO
"""
from labrad import types as T
from labrad.server import setting
from labrad.gpib import GPIBManagedServer, GPIBDeviceWrapper
from twisted.internet.defer import inlineCallbacks, returnValue
CHANNELS = 'T T0 A B AB C D CD ALL'.split()
ALL = CHANNELS.index('ALL')
MODES = 'TTL NIM ECL VAR'.split()
def findString(key, ls):
if key is None:
key = 0
elif isinstance(key, str):
key = ls.index(key)
elif isinstance(key, (int, long)):
if key < 0 or key >= len(ls):
raise Exception('Out of range.')
return key
def makeChannelCommand(cmd, channel, params, all_channels=[2,3,4,5,6,7]):
if params:
params = ',' + params
if channel == ALL:
channel = all_channels
else:
channel = [channel]
cmds = ['%s %d%s' % (cmd, c, params) for c in channel]
return ';'.join(cmds)
class DG535Server(GPIBManagedServer):
name = 'DG535'
deviceName = 'SRS DG535'
deviceIdentFunc = 'identify_device'
def initContext(self, c):
c['channel'] = 2
c['anchor'] = 1
@setting(1000, server='s', address='s', idn='s')
def identify_device(self, c, server, address, idn=None):
try:
yield self.client.refresh()
p = self.client[server].packet()
p.address(address)
p.timeout(1)
p.write('ES')
p.read()
resp = yield p.send()
returnValue(self.deviceName)
except Exception:
pass
@setting(11, 'Select Channel', chan=['s', 'w'], returns=['w'])
def select_channel(self, c, chan=2):
ch = c['channel'] = findString(chan, CHANNELS)
return ch
@setting(12, 'Select Delay Anchor', chan=['s', 'w'], returns=['w'])
def select_delay_anchor(self, c, chan=1):
ch = c['anchor'] = findString(data, CHANNELS)
return ch
def doCommand(self, c, cmd, params):
dev = self.selectedDevice(c)
chan = c['channel']
cmd = makeChannelCommand(cmd, chan, params)
return dev.write(cmd)
@setting(20, 'Set Channel Delay', delay=['v[s]'], returns=['b'])
def set_channel_delay(self, c, delay):
params = '%d,%g' % (c['anchor'], delay)
yield self.doCommand(c, 'DT', params)
returnValue(True)
@setting(30, 'Set High Impedance', data=['b'], returns=['b'])
def set_high_impedance(self, c, data):
params = str(int(data))
yield self.doCommand(c, 'TZ', params)
returnValue(True)
@setting(31, 'Set Output Mode', mode=['s', 'w'], returns=['b'])
def set_output_mode(self, c, mode=0):
params = str(findString(data, MODES))
yield self.doCommand(c, 'OM', params)
returnValue(True)
@setting(32, 'Set Output Amplitude', amp=['v[V]'], returns=['b'])
def set_output_amplitude(self, c, amp):
params = str(float(amp))
yield self.doCommand(c, 'OA', params)
returnValue(True)
@setting(33, 'Set Output Offset', off=['v[V]'], returns=['b'])
def set_output_offset(self, c, off):
params = str(float(off))
yield self.doCommand(c, 'OO', params)
returnValue(True)
@setting(34, 'Set Output Inversion', inv=['b'], returns=['b'])
def set_output_inversion(self, c, inv):
params = str(int(not inv))
yield self.doCommand(c, 'OP', params)
returnValue(True)
__server__ = DG535Server()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)