-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadbank.py
executable file
·384 lines (304 loc) · 11.9 KB
/
loadbank.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/python3
# TDi Loadbank Controller
# Copyright (C) 2014 Simon Howroyd
#
# 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 3 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/>.
#############################################################################
# Import Libraries
import telnetlib, time, os
# Define Class
class TdiLoadbank():
# Code to run when class is created
def __init__(self, HOST, PORT=23, password=''):
# Define network connection information
self.__HOST = HOST
self.__PORT = PORT # Default 23 if not specified
self.__password = password # Default blank if not specified
# Define Loadbank commands
self.__LOAD_COMMAND = "load"
self.__RANGE_COMMAND = "rng"
self.__MODE_COMMAND = "mode"
self.__VOLTAGE_COMMAND = "v"
self.__CURRENT_COMMAND = "i"
self.__POWER_COMMAND = "p"
self.__VOLTAGE_LIMIT_COMMAND = "vl"
self.__CURRENT_LIMIT_COMMAND = "il"
self.__POWER_LIMIT_COMMAND = "pl"
self.__VOLTAGE_MINIMUM_COMMAND = "uv"
self.__CONSTANT_VOLTAGE_COMMAND = "cv"
self.__CONSTANT_CURRENT_COMMAND = "ci"
self.__CONSTANT_POWER_COMMAND = "cp"
# Define internal variables
self.__mode = ""
self.__voltage = 0
self.__current = 0
self.__power = 0
self.__set_v = "0"
self.__set_i = "0"
self.__set_p = "0"
# Method to connect over the network
def connect(self):
# Ping the loadbank first to see if it is there
if os.system("ping -c 1 -w 2 " + self.__HOST + " > /dev/null 2>&1"):
print("Failed to detect a loadbank on network")
return 0
print("Loadbank found! Connecting...", end="")
# Connect using telnet
self._tn = self._connect(self.__HOST, self.__PORT, self.__password)
if self._tn:
print("connected!\n")
else:
print("Failed, check password?\n")
return 0
# Get safety limits
self.__set_v = self._get(self._tn, self.__CONSTANT_VOLTAGE_COMMAND).split()[0]
self.__set_i = self._get(self._tn, self.__CONSTANT_CURRENT_COMMAND).split()[0]
self.__set_p = self._get(self._tn, self.__CONSTANT_POWER_COMMAND).split()[0]
# Get current mode
self.mode = self._get(self._tn, self.__MODE_COMMAND)
# Everything working, return 1
return 1
# Method to connect over Telnet
@classmethod
def _connect(cls, HOST, PORT, password):
# Initiate connection
tn = telnetlib.Telnet(HOST, PORT)
# If we have a password...
if password:
# Wait for the loadbank to ask us for a password
tn.read_until(b"Password ? ")
# Write the password to the Loadbank
tn.write(password.encode('ascii') + b"\r\n")
# Clear the buffer
cls._flush(tn) # Flush read buffer (needed)
# Return the Telnet handle
return tn
# Method to close down the connection
def shutdown(self):
time.sleep(0.4)
self.load = False
self.zero()
self._tn.close()
return 1
# Method to zero the Loadbank
def zero(self):
time.sleep(0.4)
if "VOLTAGE" in self.mode:
self.voltage_constant = '0.0'
elif "CURRENT" in self.mode:
self.current_constant = '0.0'
elif "POWER" in self.mode:
self.power_constant = '0.0'
# Method to clear the buffer
@staticmethod
def _flush(tn):
tn.read_very_eager() # Flush read buffer
# Method to set a value
@classmethod
def _set(cls, tn, command, value):
# Build the command in the correct format
buf = (command + ' ' + value + '\r')
# Send the command over the network
cls._send(tn, buf)
# Method to get a string of text
@classmethod
def _get(cls, tn, command):
# Queries end with a '?', append if necessary
if not command.endswith('?'):
command += '?'
# Build the query in the correct format
buf = (command + '\r')
# Send the query over the network
return str(cls._send(tn, buf))
# Method to get a number **recursive**
@classmethod
def _get_float(cls, tn, command):
# Get the raw data string
data = cls._get(tn, command)
# Look for a valid reply
try:
return float(data.split()[0])
# No valid reply so run the method again
except ValueError:
return cls._get_float(tn, command) # **recursivity**
# Method to handle data 2way telnet datastream
@classmethod
def _send(cls, tn, inbuf):
# Flush the buffer
cls._flush(tn)
# Create some memory
outbuf = ""
# Send the query or command to the Loadbank
tn.write(inbuf.encode('ascii'))
# Was it a query? If so what is the expected reply?
if '?' in inbuf:
if 'v' in inbuf:
expected = b'volts'
elif 'i' in inbuf:
expected = b'amps'
elif 'p' in inbuf:
expected = b'watts'
elif 'rng' in inbuf:
expected = b'AMP'
else:
expected = b'\r'
# Check if the Loadbank acknowledged the query
while not outbuf or outbuf.isspace():
# Look for the expected reply or timeout
outbuf = tn.read_until(expected, 0.1) # Timeout = 0.1sec TODO
# Decode the reply
outbuf = outbuf.decode('ascii').strip('\r\n')
# If the reply is not what we expected...
if not outbuf or outbuf.isspace():
# Flush the buffer
cls._flush(tn)
# Send the query again
tn.write(inbuf.encode('ascii'))
# Return the query reply
return outbuf
# Was a command not a query, no reply expected.
else:
return inbuf
# Property - Is the load on or off?
@property
def load(self):
# Query the Loadbank
state = self._get(self._tn, self.__LOAD_COMMAND)
# Return the answer in boolean
if "on" in state:
return True
elif "off" in state:
return False
else:
return "UNKNOWN STATE"
# Property - Set the Loadbank on or off
@load.setter
def load(self, state):
if state:
self._set(self._tn, self.__LOAD_COMMAND, "on")
else:
self._set(self._tn, self.__LOAD_COMMAND, "off")
# Property - What is the Loadbank sensitivity range (see Loadbank manual)
@property
def range(self):
return self._get(self._tn, self.__RANGE_COMMAND)
# Property - Set a new range
@range.setter
def range(self, setting):
# Sanity check that the request is a number 0-9
if int(setting) in range(1,10):
self._set(self._tn, self.__RANGE_COMMAND, setting)
print('Set new rng ' + self.range)
else:
raise ValueError
# Property - What is the current Loadbank mode
@property
def mode(self):
if "VOLTAGE" in self.__mode:
return self.__mode + " " + str(self.voltage_constant)
elif "CURRENT" in self.__mode:
return self.__mode + " " + str(self.current_constant)
elif "POWER" in self.__mode:
return self.__mode + " " + str(self.power_constant)
else:
self.__setpoint = "error getting mode"
# Property - Set new Loadbank mode
@mode.setter
def mode(self, op_mode):
op_mode = op_mode.lower() # Change any capitals to lower case
if "vo" in op_mode or "cv" in op_mode:
self._set(self._tn, self.__MODE_COMMAND, self.__CONSTANT_VOLTAGE_COMMAND)
self.__mode = "VOLTAGE"
elif "cu" in op_mode or "ci" in op_mode:
self._set(self._tn, self.__MODE_COMMAND, self.__CONSTANT_CURRENT_COMMAND)
self.__mode = "CURRENT"
elif "po" in op_mode or "cp" in op_mode:
self._set(self._tn, self.__MODE_COMMAND, self.__CONSTANT_POWER_COMMAND)
self.__mode = "POWER"
# Update electrical data
def update(self):
self.__voltage = self._get_float(self._tn, self.__VOLTAGE_COMMAND)
self.__current = self._get_float(self._tn, self.__CURRENT_COMMAND)
self.__power = self._get_float(self._tn, self.__POWER_COMMAND)
# Property - What is the voltage?
@property
def voltage(self):
return self.__voltage
# Property - What is the voltage setpoint
@property
def voltage_constant(self):
return self.__set_v
# Propert - Set a new voltage setpoint
@voltage_constant.setter
def voltage_constant(self, volts):
self._set(self._tn, self.__CONSTANT_VOLTAGE_COMMAND, volts)
self.__set_v = volts
# Property - What is the maximum voltage limit?
@property
def voltage_limit(self):
return self._get_float(self._tn, self.__VOLTAGE_LIMIT_COMMAND)
# Property - Set a new maximum voltage limit
@voltage_limit.setter
def voltage_limit(self, volts):
self._set(self._tn, self.__VOLTAGE_LIMIT_COMMAND, volts)
# Property - What is the mimimum voltage limit?
@property
def voltage_minimum(self):
return self._get_float(self._tn, self.__VOLTAGE_MINIMUM_COMMAND)
# Property - Set a new minimum voltage limit
@voltage_minimum.setter
def voltage_minimum(self, volts):
self._set(self._tn, self.__VOLTAGE_MINIMUM_COMMAND, volts)
# Property - What is the current?
@property
def current(self):
return self.__current
# Property - What is the current setpoint?
@property
def current_constant(self):
return self.__set_i
# Property - Set a new current setpoint
@current_constant.setter
def current_constant(self, amps):
self._set(self._tn, self.__CONSTANT_CURRENT_COMMAND, amps)
self.__set_i = amps
# Property - What is the maximum current limit?
@property
def current_limit(self):
return self._get_float(self._tn, self.__CURRENT_LIMIT_COMMAND)
# Property - Set a new maximum current limit?
@current_limit.setter
def current_limit(self, amps):
self._set(self._tn, self.__CURRENT_LIMIT_COMMAND, amps)
# Property - What is the power?
@property
def power(self):
return self.__power
# Property - What is the power setpoint?
@property
def power_constant(self):
return self.__set_p
# Property - Set a new power setpoint?
@power_constant.setter
def power_constant(self, watts):
self._set(self._tn, self.__CONSTANT_POWER_COMMAND, watts)
self.__set_p = watts
# Property - What is the maximum power limit?
@property
def power_limit(self):
return self._get_float(self._tn, self.__POWER_LIMIT_COMMAND)
# Property - Set a new maximum power limit?
@power_limit.setter
def power_limit(self, watts):
self._set(self._tn, self.__POWER_LIMIT_COMMAND, watts)