-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmks_server_devices.py
232 lines (198 loc) · 7.35 KB
/
mks_server_devices.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
# 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/>.
# CHANGE LOG
#
# 24 March 2012 - Daniel Sank
#
# Reworked server
"""
### BEGIN NODE INFO
[info]
name = MKS Gauge Server Devices
version = 2.0
description =
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 20
### END NODE INFO
"""
from labrad import types as T, util
from labrad.server import LabradServer, setting
from twisted.python import log
from twisted.internet import defer
from twisted.internet.defer import inlineCallbacks, returnValue
from datetime import datetime
CHANNELS = ['ch1', 'ch2']
class MKSGaugeWrapper(DeviceWrapper):
pass
class MKSServer(DeviceServer):
name = 'MKS Gauge Server Devices'
@inlineCallbacks
def initServer(self):
pass
@inlineCallbacks
def findDevices(self):
cxn = self.client
reg = cxn.registry
#Find out what gauge groups exist
p = reg.packet()
p.cd(REGISTRY_PATH)
p.dir(key='dir')
resp = yield p.send()
gaugeGroupNames = resp['dir'][1]
#Read the gaugeGroups from the registry
p = reg.packet()
for gaugeGroupName in gaugeGroupNames:
p.get(gaugeGroupName, key = gaugeGroupName)
resp = yield p.send()
gaugeGroups = [resp[group] for group in gaugeGroupNames]
self.gaugeGroups = [(server, ID, [dict(port=port, ch1=ch1, ch2=ch2) for port,ch1,ch2 in gauges]) for server,ID,gauges in gaugeGroups]
print 'Gauge groups loaded: '
print self.gaugeGroups
def serverConnected(self, ID, name):
"""Try to connect to gauges when a server connects."""
self.findGauges()
def serverDisconnected(self, ID, name):
"""Drop gauges from the list when a server disconnects."""
for S in self.gaugeGroups:
if S['ID'] == ID:
print "'%s' disconnected." % S['server']
S['ID'] = None
removals = [G for G in self.gauges if G['server'].ID == ID]
for G in removals:
self.gauges.remove(G)
@inlineCallbacks
def loadRegistryInfo(self):
""" Each registry key should be in the form ('SerialServerName',ID,[(<COM PORT NAME>,<CH1 Name>,<CH2 Name>),...])
ID should be an LabRAD w, -1 for no ID
"""
reg = self.client.registry
#Find out what gauge groups exist
p = reg.packet()
p.cd(['','Servers','MKS Gauges'])
p.dir(key='dir')
resp = yield p.send()
gaugeGroupNames = resp['dir'][1]
#Read the gaugeGroups from the registry
p = reg.packet()
for gaugeGroupName in gaugeGroupNames:
p.get(gaugeGroupName, key = gaugeGroupName)
resp = yield p.send()
gaugeGroups = [resp[group] for group in gaugeGroupNames]
self.gaugeGroups = [(server, ID, [dict(port=port, ch1=ch1, ch2=ch2) for port,ch1,ch2 in gauges]) for server,ID,gauges in gaugeGroups]
print 'Gauge groups loaded: '
print self.gaugeGroups
@inlineCallbacks
def findGauges(self):
"""Look for gauges and servers."""
cxn = self.client
yield cxn.refresh()
yield self.loadRegistryInfo()
for serverName, ID, gauges in self.gaugeGroups:
print 'serverName: ',serverName
print 'ID: ',ID
if ID != -1:
continue
if serverName in cxn.servers:
server = cxn[serverName]
ports = yield server.list_serial_ports()
for gauge in gauges:
if gauge['port'] in ports:
yield self.connectToGauge(server, gauge)
log.msg('Server ready')
# for S in self.gaugeServers:
# if S['ID'] is not None:
# continue
# if S['server'] in cxn.servers:
# log.msg('Connecting to %s...' % S['server'])
# ser = cxn[S['server']]
# ports = yield ser.list_serial_ports()
# for G in S['gauges']:
# if G['port'] in ports:
# yield self.connectToGauge(ser, G)
# S['ID'] = ser.ID
# log.msg('Server ready')
@inlineCallbacks
def connectToGauge(self, ser, G):
"""Connect to a single gauge."""
port = G['port']
ctx = G['context'] = ser.context()
log.msg(' Connecting to %s...' % port)
try:
res = yield ser.open(port, context=ctx)
ready = G['ready'] = res==port
except:
ready = False
if ready:
# set up baudrate
p = ser.packet(context=ctx)\
.baudrate(9600L)\
.timeout()
yield p.send()
res = yield ser.read(context=ctx)
while res:
res = yield ser.read(context=ctx)
yield ser.timeout(T.Value(2, 's'), context=ctx)
# check units
p = ser.packet()\
.write_line('u')\
.read_line(key='units')
res = yield p.send(context=ctx)
if 'units' in res.settings:
G['units'] = res['units']
else:
G['ready'] = False
# create a packet to read the pressure
p = ser.packet(context=ctx)\
.write_line('p')\
.read_line(key='pressure')
G['packet'] = p
G['server'] = ser
if ready:
self.gauges.append(G)
log.msg(' OK')
else:
log.msg(' ERROR')
@setting(2, 'Get Gauge List', returns=['*s: Gauge names'])
def list_gauges(self, c):
"""Request a list of available gauges."""
gauges = [G[ch] for G in self.gauges
for ch in CHANNELS if G[ch]]
return gauges
@setting(1, 'Get Readings', returns=['*v[Torr]: Readings'])
def get_readings(self, c):
"""Request current readings."""
deferreds = [G['packet'].send() for G in self.gauges]
res = yield defer.DeferredList(deferreds, fireOnOneErrback=True)
readings = []
strs = []
for rslt, G in zip(res, self.gauges):
s = rslt[1].pressure
strs.append(s)
r = rslt[1].pressure.split()
for ch, rdg in zip(CHANNELS, r):
if G[ch]:
try:
readings += [T.Value(float(rdg), 'Torr')]
except:
readings += [T.Value(0, 'Torr')]
returnValue(readings)
__server__ = MKSServer()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)