-
Notifications
You must be signed in to change notification settings - Fork 2
/
dpm86
408 lines (316 loc) · 14.2 KB
/
dpm86
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#! /usr/bin/python3
# Program: dpm86
# Purpose: Control the DPM86xx series Power Supply from the Linux command line.
#
# Should work with:
#
# - DPM8605 (see f. e. https://joy-it.net/en/products/JT-DPM8605)
# - DPM8608 (search on ebay)
# - DPM8616 (search on ebay)
# - DPM8624 (see f. e. https://joy-it.net/en/products/JT-DPM8624)
#
# ! Be aware !
# - The power supply has to be set to "simple protocol" (this is the default configuration). Check the manual.
# - Check the variable 'tty' and choose the correct ttyUSB-device.
# - Check the variable 'addr' and choose the correct ID of your power supply ('01' by default).
# Unless you changed the setup of your power supply this should match. If in doubt: Check the manual and your power supply.
# - Be sure that there is no potential difference between the power supply and the controlling device (correctly spoken:
# between the power supply and the USB-RS485 adapter). Otherwise the signal transmission is damn error-prone, which becomes
# noticeable e.g. by strange characters in the transmission (or no transmission at all). Connect both to the same ground.
# - WARNING: The Raspberry 4 seems to be buggy. Try to avoid it. Use an older version instead.
# - This script comes along with no warranty at all. You have been warned.
# - Have fun.
#
# History:
#221026-AHo: Rewritten in Python
#221105-AHo: Moved to object oriented.
#221124-AHo: Bugfixes, some new features. Still sometimes really ugly code, but it works. :-)
#240101-AHo: Added comments about variable "tty" (DPM86 communication)
import sys
import serial, time
import os, stat
from os.path import exists
from os import access, R_OK, W_OK
#----- variables
ver="240101" # version of this script
# DPM settings
addr="01" # Adress of the power supply. Default: 01. Check manual of the power suppy for more information.
# DPM86 communication
# ===================
# The DPM86 power supply unit is controlled via the "RS485" protocol over a physical connection
# between the computer and the power supply unit (cable with two wires, e.g. bell wire).
#
# On the RPi, the corresponding pins on the GPIO strip can be used or a corresponding interface can be
# added with a USB-to-RS485 adapter (these adapters cost a few €). Either way - at the end of the day,
# the device via which the power supply unit can be accessed must be entered here.
#
# -> /dev/ttyUSBx - if you use a USB-to-RS485 adapter
# -> /dev/ttyAMA0 - if you use the PL011-UART from the Raspberry (untested!)
# -> /dev/ttyS0 - the mini UART from the Raspberry (untested!)
#
# Good sources (in German):
# - https://www.raspberry-pi-geek.de/ausgaben/rpg/2020/02/serielle-kommunikation-ueber-rs-485-mit-dem-raspberry-pi/
# - https://www.raspberry-pi-geek.de/ausgaben/rpg/2019/12/serielle-kommunikation-ueber-rs-232-rs-485-teil-1/
#
tty="/dev/ttyUSB0" # The device via this script is talkinf to the power supply.
read="r"
write="w"
ok=0
error=1
# parameter for dpm functions (just for a better reading) -- SHOULD BE MOVED TO THE CLASS DEFINITION
# function "f_output"
p_off="0" # parameter "off"
p_on="1" # parameter "on"
# function "f_const"
p_voltage="0" # parameter "voltage"
p_current="1" # parameter "current"
#----- class definition
class dpm86(serial.Serial):
# supported dpm functions -- see the document "dpm86xx-series-power-supply_simple-communication-protocol.odt/pdf" in this repository
F_MAX_VOLTAGE="00" # R/-: maximum output voltage
F_MAX_CURRENT="01" # R/-: maximum output current
F_VOLTAGE_SETTING="10" # R/W: output voltage target
F_CURRENT_SETTING="11" # R/W: output current target
F_OUTPUT="12" # R/W: output on/off
F_VOLTAGE="30" # R/-: output voltage
F_CURRENT="31" # R/-: output current
F_CONST="32" # R/W: constant current or constant voltage status
F_TEMPERATURE="33" # R/-: temperature
VOLTAGE_MIN=0 # 0 Volt
VOLTAGE_MAX=60 # 60 Volt
CURRENT_MIN=0 # 0 Ampere
CURRENT_MAX=9 # 9 Ampere --> should be set automatically, depending on the type (maybe next release)
def __init__(self, port=None, baudrate=9600, timeout=None, inter_byte_timeout=None):
super().__init__(
port = port,
baudrate = baudrate,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = timeout,
inter_byte_timeout = inter_byte_timeout
)
# dpm86: lowlevel - read a value
def vread(self, opcode):
opcode = str(opcode)
# sending command
self.cmd = ":" + addr + read + opcode + "=0" + ",,\n"
self.bcmd = self.cmd.encode()
written = self.write(self.bcmd)
if written < len(self.bcmd): return("ERROR -- Writing '" + self.cmd + "' to dpm86 failed: only " + written + " bytes written.")
# reading response
bresponse = self.readline()
response = bresponse.decode(errors='replace')
if response == "": return ("ERROR -- Communication with dpm86 failed. No response read.")
# return corrected response as word
response = response[7:-3]
if response.isdigit(): response = int(response)
return response
# dpm86: lowlevel - read a value
def vwrite(self, opcode, value):
opcode = str(opcode)
value = str(value)
# sending command
self.cmd =":" + addr + write + opcode + "=" + value + ",,\n"
self.bcmd = self.cmd.encode()
written = self.write(self.bcmd)
if written < len(self.bcmd): return("ERROR -- Writing '" + self.cmd + "' to dpm86 failed: only " + written + " bytes written.")
# reading response
bresponse = self.readline()
response = bresponse.decode(errors='replace')
# check and return value
response = response[:-2]
if response == ":" + addr + "ok": return("ok")
else: return("ERROR: " + response)
# dpm86: output
def output(self, state=None):
if state == None: return(self.vread(self.F_OUTPUT))
elif state in [p_on, p_off]: return(self.vwrite(self.F_OUTPUT, state))
else: return("ERROR: Invalid parameter.")
# dpm86: const
def const(self, state=None):
if state == None: return(self.vread(self.F_CONST))
elif state in [p_voltage, p_current]: return(self.vwrite(self.F_CONST, state))
else: return("ERROR: Invalid parameter.")
# dpm86: temperature
def temperature(self):
return(self.vread(self.F_TEMPERATURE))
def volt(self, v):
if type(v) is not int: return("ERROR reading values.")
return(float(v) / 100)
def ampere(self, a):
if type(a) is not int: return("ERROR reading values.")
return(float(a) / 1000)
# dpm86: read delivered current
def current(self):
return(self.ampere(self.vread(self.F_CURRENT)))
# dpm86: read delivered voltage
def voltage(self):
return(self.volt(self.vread(self.F_VOLTAGE)))
# dpm86: read delivered power
def power(self):
v = self.volt(self.vread(self.F_VOLTAGE))
c = self.ampere(self.vread(self.F_CURRENT))
if type(v) is str or type(c) is str: return("ERROR reading values.")
return(float(v) * float(c))
# dpm86: read and set current and voltage setting, read the power setting
def setting(self, name, value=None):
if name in ['voltage', 'volt', 'v']:
if value == None: return(self.volt(self.vread(self.F_VOLTAGE_SETTING)))
if value == "max": return(self.volt(self.vread(self.F_MAX_VOLTAGE)))
try:
value = float(value)
except:
return("ERROR: Value for voltage is not an integer or a float.")
if value < self.VOLTAGE_MIN or value > self.VOLTAGE_MAX: return("ERROR: Value for voltage out of range.")
return(self.vwrite(self.F_VOLTAGE_SETTING, int(value * 100)))
elif name in ['current', 'c', 'ampere', 'amp', 'a']:
if value == None: return(self.ampere(self.vread(self.F_CURRENT_SETTING)))
if value == "max": return(self.ampere(self.vread(self.F_MAX_CURRENT)))
try:
value = float(value)
except:
return("ERROR: Value for current is not an integer or a float.")
if value < self.CURRENT_MIN or value > self.CURRENT_MAX: return("ERROR: Value for current is out of range.")
return(self.vwrite(self.F_CURRENT_SETTING, int(value * 1000)))
elif name in ['power', 'p', 'watt', 'w']:
if value == None:
v = self.volt(self.vread(self.F_VOLTAGE_SETTING))
c = self.ampere(self.vread(self.F_CURRENT_SETTING))
if type(v) is str or type(c) is str: return("ERROR reading values.")
return(float(v) * float(c))
return("ERROR: Power expects no additional parameter")
else:
return("ERROR: Parameter 'name' unknown value.")
#----- Code starts here :-)
# print a short help
def help():
print("")
print(" " + sys.argv[0] + ", version " + ver + " -- tool for controlling DPM86xx series power supply")
print("")
print(" Usage:")
print(" " + sys.argv[0] + " action parameter")
print("")
print(" Details:")
print(" output/o -- read the actual output state (on/off)")
print(" output/o 1/on -- turn the output on")
print(" output/o 0/off -- turn the output off")
print("")
print(" voltage/volt/v -- read the actual delivered voltage")
print(" voltage/volt/v <0.0 .. max> -- set the voltage target")
print(" voltage/volt/v target/setting -- read the voltage target")
print(" voltage/volt/v max -- read the maximum output voltage")
print("")
print(" current/c/ampere/amp/a -- read the actual deliviered current")
print(" current/c/ampere/amp/a <0.0 .. max> -- set the current target")
print(" current/c/ampere/amp/a target/setting -- read the current target")
print(" current/c/ampere/amp/a max -- read the maximum output current")
print("")
print(" power/p/watt/w -- read the actual delivered power")
print(" power/p/watt/w target/setting -- read the power target")
print("")
print(" const/C -- read the actual const setting (voltage/const)")
print(" const/C voltage/v -- set constant voltage delivery")
print(" const/C current/c/ampere/amp/a -- set constant current delivery")
print("")
print(" temp/t -- read the temperature")
print("")
print(" READ/R <function> -- read value from function (DANGEROUS!)")
print(" WRITE/W <function> <value> -- write <value> to <function> (DANGEROUS!)")
print("")
# ----- command line argument "current/c/ampere/amp/a"
# dpm86 current/v ...
def cmd_current():
if len(sys.argv) == 2: print(dpm.current()) # current/c/ampere/amp/a -- read the actual deliviered current
elif sys.argv[2] in ['target', 'setting']: print(dpm.setting("c")) # current/c/ampere/amp/a target -- current: read the target
else: print(dpm.setting("c", sys.argv[2])) # current/c/ampere/amp/a <value> -- set the target
# ----- command line argument "voltage/volt/v"
# dpm86 voltage/v ...
def cmd_voltage():
if len(sys.argv) == 2: print(dpm.voltage()) # voltage/v -- voltage: read the actual delivered voltage
elif sys.argv[2] in ['target', 'setting']: print(dpm.setting("v")) # voltage/v target -- voltage: read the target
else: print(dpm.setting("v", sys.argv[2])) # voltage/v <value> -- voltage: set the target
# ----- raw communication
# dpm read/r <function>
def cmd_read():
print(dpm.vread(sys.argv[2]))
# dpm write/w <function> <value>
def cmd_write():
print(dpm.vwrite(sys.argv[2], sys.argv[3]))
# ----- command line argument "temp/t"
# dpm86 temp/t
def cmd_temp():
print(dpm.temperature())
# ----- command line argument "output/o"
# dpm output/o
def cmd_output():
if len(sys.argv) == 2: print(dpm.output()) # output/o -- read the actual output state (on/off)
elif sys.argv[2] in ['1', 'on']: print(dpm.output(p_on)) # output/o 1/on -- turn the output on"
elif sys.argv[2] in ['0', 'off']: print(dpm.output(p_off)) # output/o 0/off -- turn the output off
else:
print("")
print("Argument 'output': unknown parameter '" + sys.argv[2] + "'")
print("")
sys.exit(error)
# ----- command line argument "power/p"
# dpm power/p ...
def cmd_power():
if len(sys.argv) == 2: print(dpm.power()) # power/p -- read the actual delivered power
elif sys.argv[2] in ['target', 'setting']: print(dpm.setting("power")) # power/p target -- read the power target"
else:
print("")
print("Argument 'power': unknown parameter '" + sys.argv[2] + "'")
print("")
sys.exit(error)
# ----- command line argument "const/C"
# dpm const/C ...
def cmd_const():
if len(sys.argv) == 2: print(dpm.const()) # const/C -- read the actual const setting (const voltage/const current)
elif sys.argv[2] in ['voltage', 'volt', 'v']: print(dpm.const(p_voltage)) # const/C voltage/volt/v -- set constant voltage delivery
elif sys.argv[2] in ['current', 'c', 'ampere', 'amp', 'a']: print(dpm.const(p_current)) # const/C current/c/ampere/amp/a -- set constant current delivery
else:
print("")
print("Argument 'const': unknown parameter '" + sys.argv[2] + "'")
print("")
sys.exit(error)
# ----- first command line argument
# analyse first parameter
def command_line_argument():
if len(sys.argv) == 1: help()
elif sys.argv[1] in ['output', 'o']: cmd_output()
elif sys.argv[1] in ['const', 'C']: cmd_const()
elif sys.argv[1] in ['temp', 't']: cmd_temp()
elif sys.argv[1] in ['voltage', 'volt', 'v']: cmd_voltage()
elif sys.argv[1] in ['current', 'c', 'ampere', 'amp', 'a']: cmd_current()
elif sys.argv[1] in ['READ', 'R']: cmd_read()
elif sys.argv[1] in ['WRITE', 'W']: cmd_write()
elif sys.argv[1] in ['power', 'p', 'w', 'watt']: cmd_power()
else:
print("Unknown first command line argument '" + sys.argv[1] + "'")
help()
sys.exit(error)
# initialise
def init():
# check device tty
if not exists(tty):
print("ERROR -- Device '" + tty + "'does not exist.")
sys.exit(error)
if not stat.S_ISCHR(os.stat(tty).st_mode):
print("ERROR -- Device '" + tty + "' is not a character device.")
sys.exit(error)
if not access(tty, R_OK):
print("ERROR -- Device '" + tty + "' is not readable.")
sys.exit(error)
if not access(tty, W_OK):
print("ERROR -- Device '" + tty + "' is not writable.")
### Main Code
init()
dpm = dpm86(
port = tty,
baudrate = 9600,
timeout=0.5,
inter_byte_timeout=0.1
)
command_line_argument()
dpm.close()
# this is the last line :)