-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvectron_475.py
184 lines (158 loc) · 5.17 KB
/
convectron_475.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
# Copyright []
#
# 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 = Convectron 475
version = 1.0
description =
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 20
### END NODE INFO
"""
import platform
global serial_server_name
serial_server_name = platform.node() + '_serial_server'
from labrad.server import setting
from labrad.devices import DeviceServer,DeviceWrapper
from twisted.internet.defer import inlineCallbacks, returnValue
from labrad.types import Value
TIMEOUT = Value(2,'s')
BAUD = 19200
class Convectron475Wrapper(DeviceWrapper):
@inlineCallbacks
def connect(self, server, port):
"""Connect to a device."""
print('connecting to "%s" on port "%s"...' % (server.name, port), end=' ')
self.server = server
self.ctx = server.context()
self.port = port
p = self.packet()
p.open(port)
print('opened on port "%s"' %self.port)
p.baudrate(BAUD)
p.readbuffer() # clear out the read buffer
p.timeout(TIMEOUT)
print(" CONNECTED ")
yield p.send()
def packet(self):
"""Create a packet in our private context."""
return self.server.packet(context=self.ctx)
def shutdown(self):
"""Disconnect from the serial port when we shut down."""
return self.packet().close().send()
@inlineCallbacks
def write(self, code):
"""Write a data value to the temperature controller."""
yield self.packet().write(code).send()
@inlineCallbacks
def read(self):
'''
Read out whatever is in the buffer
'''
p=self.packet()
p.readbuffer()
ans=yield p.send()
returnValue(ans.readbuffer.rstrip())
@inlineCallbacks
def query(self, code):
""" Write, then read. """
p = self.packet()
p.write(code+"\r")
p.readbuffer()
ans = yield p.send()
returnValue(ans.readbuffer.rstrip())
class Convectron475Server(DeviceServer):
name = 'Convectron 475'
deviceName = 'Convectron 475'
deviceWrapper = Convectron475Wrapper
@inlineCallbacks
def initServer(self):
print('loading config info...', end=' ')
self.reg = self.client.registry()
yield self.loadConfigInfo()
print('done.')
print(self.serialLinks)
yield DeviceServer.initServer(self)
@inlineCallbacks
def loadConfigInfo(self):
"""Load configuration information from the registry."""
reg = self.reg
yield reg.cd(['', 'Servers', '475 Convectron', 'Links'], True)
dirs, keys = yield reg.dir()
p = reg.packet()
print(" created packet")
print("printing all the keys",keys)
for k in keys:
print("k=",k)
p.get(k, key=k)
ans = yield p.send()
print("ans=",ans)
self.serialLinks = dict((k, ans[k]) for k in keys)
@inlineCallbacks
def findDevices(self):
"""Find available devices from list stored in the registry."""
devs = []
for name, (serServer, port) in list(self.serialLinks.items()):
if serServer not in self.client.servers:
continue
server = self.client[serServer]
print(server)
print(port)
ports = yield server.list_serial_ports()
print(ports)
if port not in ports:
continue
devName = '%s - %s' % (serServer, port)
devs += [(devName, (server, port))]
returnValue(devs)
@setting(100)
def connect(self, c, server, port):
dev=self.selectedDevice(c)
yield dev.connect(server,port)
@setting(101, returns='s')
def read_pressure(self,c):
"""Read Convectron Gauge pressure response."""
dev=self.selectedDevice(c)
ans = yield dev.query("RD")
returnValue(ans)
@setting(102, returns='s')
def id(self, c):
"""Get the serial number of the 475 Controller."""
dev = self.selectedDevice(c)
ans = yield dev.query("SN")
returnValue(ans)
@setting(9002)
def read(self, c):
dev=self.selectedDevice(c)
ret=yield dev.read()
returnValue(ret)
@setting(9003)
def write(self, c, phrase):
dev=self.selectedDevice(c)
yield dev.write(phrase)
@setting(9004)
def query(self,c,phrase):
dev=self.selectedDevice(c)
ret = yield dev.query(phrase)
returnValue(ret)
__server__ = Convectron475Server()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)