-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellmanClass.py
274 lines (240 loc) · 8.12 KB
/
spellmanClass.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import socket
class Spellman:
STX = '\x02' # Start of Text character
ETX = '\x03' # End of Text character
SUCCESS = '\x24' # Character $
V_MAX = 50000 # 50,000 V max spellman
V_COEF = V_MAX / 4095
I_MAX = 0.6 # 0.6 mA max spellman
I_COEF = I_MAX / 4095
def __init__(self, host='192.168.17.1', port=50001):
self.server_host = host
self.server_port = port
self.name = 'Spellman SL30'
self._vset = None
self._iset = None
self._vmon = None
self._imon = None
self._stat = None
def send_recv(self, message, tOp=0, tOut=1):
""" Send a message and wait for the response. """
if isinstance(message, str):
message = message.encode('ascii')
try:
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
so.settimeout(tOut)
so.connect((self.server_host, self.server_port))
so.sendall(message)
so.shutdown(socket.SHUT_WR)
so.settimeout(tOut + tOp)
resp = b''
while True:
chunk = so.recv(1024)
if not chunk:
break
resp += chunk
if b'\x03' in resp:
break
finally:
so.close()
return resp.decode('ascii')
def build_message(self, cmd, arg=None):
""" Returns a formatted Spellman message str. """
if arg is None:
return f"{self.STX}{cmd},,{self.ETX}"
else:
return f"{self.STX}{cmd},{arg},{self.ETX}"
# Properties
@property
def vset(self):
if self._vset is None:
self._vset = self.get_vset()
return self._vset
@vset.setter
def vset(self, voltage_V):
self.set_vset(voltage_V)
self._vset = voltage_V
@property
def iset(self):
if self._iset is None:
self._iset = self.get_iset()
return self._iset
@iset.setter
def iset(self, current_mA):
self.set_iset(current_mA)
self._iset = current_mA
@property
def vmon(self):
return self.get_vmon()
@property
def imon(self):
return self.get_imon()
@property
def stat(self):
return self.get_status()
@property
def on(self):
return self.get_status()['HV']
@property
def remote(self):
return self.get_status()['REMOTE']
# Methods for interacting with the hardware
def get_vset(self):
"""Request voltage setpoint."""
ans = self.request_DAC(0)
if ans == '':
return -1
return int(ans[1]) * self.V_COEF
def set_vset(self, voltage_V):
"""Set voltage setpoint."""
voltage = int(voltage_V / self.V_COEF)
return self.set_DAC(0, voltage)
def get_iset(self):
"""Request current setpoint."""
ans = self.request_DAC(1)
if ans == '':
return -1
return int(ans[1]) * self.I_COEF
def set_iset(self, current_mA):
"""Set current setpoint."""
current = int(current_mA / self.I_COEF)
return self.set_DAC(1, current)
def get_vmon(self):
"""Request voltage monitor."""
ans = self.analog()
if ans == '':
return -1
return int(ans[3]) * self.V_COEF
def get_imon(self):
"""Request current monitor."""
ans = self.analog()
if ans == '':
return -1
return int(ans[4]) * self.I_COEF
def get_status(self):
"""Get system and status information."""
stat = self.status_parsed()
stat.update(self.system_parsed())
return stat
# Methods for specific commands
def set_DAC(self, i, n):
"""Set digital analog converter."""
cmd = 10 + i
command_message = self.build_message(cmd, n)
try:
ans = self.send_recv(command_message).strip(self.STX + self.ETX + ',').split(',')
return ans[0], ans[1]
except Exception:
return ''
def request_DAC(self, i):
"""Request digital analog converter."""
cmd = 14 + i
command_message = self.build_message(cmd)
try:
ans = self.send_recv(command_message).strip(self.STX + self.ETX + ',').split(',')
return ans[0], ans[1]
except Exception:
return ''
def analog(self):
"""Request analog inputs."""
cmd = 20
command_message = self.build_message(cmd)
try:
ans = self.send_recv(command_message).strip(self.STX + self.ETX + ',').split(',')
return ans[0], ans[1], ans[2], ans[3], ans[4], ans[5], ans[6], ans[7]
except Exception:
return ''
def system(self):
"""Request system status."""
cmd = 22
command_message = self.build_message(cmd)
try:
ans = self.send_recv(command_message).strip(self.STX + self.ETX + ',').split(',')
return ans[0], ans[1], ans[2], ans[3]
except Exception:
return ''
def status(self):
"""Request digital input status."""
cmd = 76
command_message = self.build_message(cmd)
try:
ans = self.send_recv(command_message).strip(self.STX + self.ETX + ',').split(',')
return ans[0], ans[1], ans[2], ans[3], ans[4], ans[5], ans[6], ans[7], ans[8]
except Exception:
return ''
def system_parsed(self):
ans = self.system()
status = {}
try:
status['HV'] = bool(int(ans[1]))
status['ILK'] = bool(int(ans[2]))
status['FAULT'] = bool(int(ans[3]))
except IndexError:
status['HV'] = '??'
status['ILK'] = '??'
status['FAULT'] = '??'
return status
def status_parsed(self):
ans = self.status()
stat = {}
try:
stat['SYSFAULT'] = bool(int(ans[1])) # Fault
stat['SYSILK'] = bool(int(ans[2])) # Interlock
stat['REMOTE'] = bool(int(ans[3])) # Remote
stat['SYSHV'] = bool(int(ans[4])) # High voltage
stat['HC'] = bool(int(ans[5])) # High current
stat['REG'] = bool(int(ans[6])) # Regulation error
stat['ARC'] = bool(int(ans[7])) # Arc
stat['OT'] = bool(int(ans[8])) # Over temperature
except IndexError:
stat['REMOTE'] = '??'
stat['ARC'] = '??'
return stat
def turn_remote_on(self):
'''Turn on remote mode
return a tupla with command code and error code ($=>ok)'''
cmd = 85
arg = 1
command_message = self.build_message(cmd,arg)
try:
ans = self.send_recv(command_message, self.server_host).strip(STX+ETX+',').split(',')
return ans[0],ans[1]
except Exception:
return ''
def turn_remote_off(self):
'''Turn off remote mode
return a tupla with command code and error code ($=>ok)'''
cmd = 85
arg = 0
command_message = self.build_message(cmd,arg)
try:
ans = self.send_recv(command_message, self.server_host).strip(STX+ETX+',').split(',')
return ans[0],ans[1]
except Exception:
return ''
def turn_hv_on(self):
'''Turn on high voltage
return a tupla with command code and error code ($=>ok)'''
cmd = 99
arg = 1
command_message = self.build_message(cmd,arg)
try:
ans = self.send_recv(command_message, self.server_host).strip(STX+ETX+',').split(',')
return ans[0],ans[1]
except Exception:
return ''
def turn_hv_off(self):
'''Turn off high voltage
return a tupla with command code and error code ($=>ok)'''
cmd = 99
arg = 0
command_message = self.build_message(cmd,arg)
try:
ans = self.send_recv(command_message, self.server_host).strip(STX+ETX+',').split(',')
return ans[0],ans[1]
except Exception:
return ''
def turn_on(self):
return self.turn_hv_on()
def turn_off(self):
return self.turn_hv_off()