-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmwcancmd.py
executable file
·372 lines (321 loc) · 11 KB
/
mwcancmd.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
#!/usr/bin/env python3
# Controlling the Mean Well NPB-abc0 and BIC-2200-CAN
# tested only with the 24V Version NPB-1200 and BIC-2200-CAN-24
# Requirement for using
# Needed external python modules
# pip3 install ifcfg
# What is missing:
# macGH 15.06.2023 Version 0.2.3
# - support for Meanwell NPB-x Charger
# - new config area
# macGH 16.06.2023 Version 0.2.4: fault and status queries
# macGH 18.06.2023 Version 0.2.5: Changed to mwcan.py class
# macGH 19.06.2023 Version 0.2.6: Fixed some parts
# macGH 16.02.2024 Version 0.2.7: Added firmware read
# macGH 26.03.2024 Version 0.2.8: Added systemconfig read write
# macGH 13.05.2024 Version 0.2.9: Added NPB config curve read
# macGH 24.09.2024 Version 0.3.0: Added BIC read Fanspeed
import os
import can
import sys
import signal
import atexit
import ifcfg
from mwcan import *
####################################################
# Config
# 0 = BIC-2200
# 1 = NPM-abc0
USEDMW = 0
# BIC-2200 --> "00" to "07"
# NPM-abc0 --> "00" to "03"
USEDID = "03"
# "" = default = "/dev/ttyACM0"
# if you have another device specify here
RS232DEV = ""
# Enter Loglevel 0,10,20,30,40,50
# CRITICAL 50
# ERROR 40
# WARNING 30
# INFO 20
# DEBUG 10
# NOTSET 0
LOGLEVEL = 20
logtofile = 0
logtoconsole = 1
def on_exit():
print("CLEAN UP ...")
candev.can_down()
def handle_exit(signum, frame):
sys.exit(0)
def mwcan_commands():
print("")
print(" " + sys.argv[0] + " - controlling the Meanwell CAN devices BIC-2200 Power Supply and NPB-abc0 Charger")
print("")
print(" Usage:")
print(" " + sys.argv[0] + " parameter and <value>")
print("")
print(" on -- output on")
print(" off -- output off")
print(" readonoff -- read current on/off status")
print("")
print(" cvread -- read charge voltage setting")
print(" cvset <value> -- set charge voltage")
print(" ccread -- read charge current setting")
print(" ccset <value> -- set charge current")
print("")
print(" dvread -- read discharge voltage setting")
print(" dvset <value> -- set discharge voltage")
print(" dcread -- read discharge current setting")
print(" dcset <value> -- set discharge current")
print("")
print(" vread -- read DC voltage")
print(" cread -- read DC current")
print(" acvread -- read AC voltage")
print("")
print(" charge -- set direction charge battery")
print(" discharge -- set direction discharge battery")
print("")
print(" tempread -- read power supply temperature")
print(" typeread -- read power supply type")
print(" firmwareread -- read power supply firmware")
print(" serialread -- read power supply serial number")
print(" statusread -- read status")
print(" fan1 -- read fanspeed1")
print(" fan2 -- read fanspeed2")
print(" faultread -- read fault status")
print(" readscaling -- read scaling factors")
print(" systemconfigread -- read system config")
print(" systemconfigset <value> -- write system config")
print(" NPB_chargemode <value> -- Set PSU = 0 or Chargermode = 1")
print(" NPB_readcurve -- read NPB curve config")
print("")
print(" <value> = amps oder volts * 100 --> 25,66V = 2566")
print("")
print(" Version 0.2.6 ")
#########################################
# Operation function
def readoperation():#0=off, 1=on
print ("Read operation mode")
v = candev.operation(0,0)
print(v)
return v
def operation(val):#0=off, 1=on
print ("turn output on=1 / off=0 ")
v = candev.operation(1,val)
print(v)
return v
def charge_voltage(rw,val=0x00): #0=read, 1=set
# print ("read/set charge voltage")
# Command Code 0x0020
# Read Charge Voltage
if (rw == 1) and (val == 0x00) : return -1
v = candev.v_out_set(rw,val)
print(v)
return v
def charge_current(rw,val=0x00): #0=read, 1=set
# print ("read/set charge current")
# Command Code 0x0030
# Read Charge Voltage
#if (rw == 1) and (val == 0x00) : return -1
v = candev.i_out_set(rw,val)
print(v)
return v
def discharge_voltage(rw,val=0x00): #0=read, 1=set
# print ("read/set discharge voltage")
# Command Code 0x0120
# Read Charge Voltage
if (rw == 1) and (val == 0x00) : return -1
v = candev.BIC_discharge_v(rw,val)
print(v)
return v
def discharge_current(rw,val=0x00): #0=read, 1=set
# print ("read/set charge current")
# Command Code 0x0130
# Read Charge Voltage
if (rw == 1) and (val == 0x00) : return -1
v = candev.BIC_discharge_i(rw,val)
print(v)
return v
def vread():
# print ("read dc voltage")
# Command Code 0x0060
# Read DC Voltage
v = candev.v_out_read()
print(v)
return v
def cread():
# print ("read dc current")
# Command Code 0x0061
# Read DC Current
v = candev.i_out_read()
print(v)
return v
def acvread():
# print ("read ac voltage")
# Command Code 0x0050
# Read AC Voltage
v = candev.v_in_read()
print(v)
return v
def BIC_chargemode(val): #0=charge, 1=discharge
# print ("set direction charge")
# Command Code 0x0100
# Set Direction Charge
v = candev.BIC_chargemode(1,val)
print(v)
return v
def NPB_readcurve():
# print ("Set PSU or Charger Mode to NPB Device")
# Command Code 0x00B4
v = candev.NPB_curve_config(0,0)
candev.decode_curve_config(v)
return v
def NPB_chargemode(rw, val=0xFF):
# print ("Set PSU or Charger Mode to NPB Device")
# Command Code 0x00B4
v = candev.NPB_curve_config_pos(1,CURVE_CONFIG_CUVE,val) #Bit 7 should be 0
print(v)
return v
def typeread():
# print ("read power supply type")
# Command Code 0x0082
# Command Code 0x0083
# Read Type of PSU
v = candev.type_read()
print(v)
return v
def firmwareread():
# print ("read power supply type")
# Command Code 0x0084
# Read firmware version of PSU
v = candev.firmware_read()
print(v)
candev.decode_firmware(v)
return v
def serialread():
# print ("read power supply type")
# Command Code 0x0087
# Command Code 0x0088
# Read serial of PSU
v = candev.serial_read()
print(v)
return v
def tempread():
# print ("read power supply temperature")
# Command Code 0x0062
# Read AC Voltage
v = candev.temp_read()
print(v)
return v
def readscaling():
# print ("Read System Scaling factor")
# Command Code 0x00C0
# Read System Status
v = candev.system_scaling_factor()
return v
def statusread():
# print ("Read System Status")
# Command Code 0x00C1
# Read System Status
v = candev.system_status()
candev.decode_system_status(v)
return v
def readfan1():
# print ("Read fan1")
# Command Code 0x0070
v = candev.BIC_fanspeed1()
print(v)
return v
def readfan2():
# print ("Read fan2")
# Command Code 0x0071
v = candev.BIC_fanspeed2()
print(v)
return v
def systemconfig(rw,val):
# print ("Read System config")
# Command Code 0x00C2
# Read System config
v = candev.system_config(rw,val)
candev.decode_system_config(v)
return v
def faultread():
# print ("Read System Fault Status")
# Command Code 0x0040
# Read System Fault Status
v = candev.fault_status_read()
candev.decode_fault_status(v)
return v
def command_line_argument():
if len (sys.argv) == 1:
print ("")
print ("Error: First command line argument missing.")
mwcan_commands()
error = 1
return
if sys.argv[1] in ['on']: operation(1)
elif sys.argv[1] in ['off']: operation(0)
elif sys.argv[1] in ['readonoff']: readoperation()
elif sys.argv[1] in ['cvread']: charge_voltage(0)
elif sys.argv[1] in ['cvset']: charge_voltage(1,int(sys.argv[2]))
elif sys.argv[1] in ['ccread']: charge_current(0)
elif sys.argv[1] in ['ccset']: charge_current(1,int(sys.argv[2]))
elif sys.argv[1] in ['dvread']: discharge_voltage(0)
elif sys.argv[1] in ['dvset']: discharge_voltage(1,int(sys.argv[2]))
elif sys.argv[1] in ['dcread']: discharge_current(0)
elif sys.argv[1] in ['dcset']: discharge_current(1,int(sys.argv[2]))
elif sys.argv[1] in ['vread']: vread()
elif sys.argv[1] in ['cread']: cread()
elif sys.argv[1] in ['acvread']: acvread()
elif sys.argv[1] in ['charge']: BIC_chargemode(0)
elif sys.argv[1] in ['discharge']: BIC_chargemode(1)
elif sys.argv[1] in ['tempread']: tempread()
elif sys.argv[1] in ['typeread']: typeread()
elif sys.argv[1] in ['serialread']: serialread()
elif sys.argv[1] in ['firmwareread']:firmwareread()
elif sys.argv[1] in ['statusread']: statusread()
elif sys.argv[1] in ['fan1']: readfan1()
elif sys.argv[1] in ['fan2']: readfan2()
elif sys.argv[1] in ['faultread']: faultread()
elif sys.argv[1] in ['readscaling']:readscaling()
elif sys.argv[1] in ['systemconfigread']:systemconfig(0,0)
elif sys.argv[1] in ['systemconfigset'] :systemconfig(1,int(sys.argv[2]))
elif sys.argv[1] in ['NPB_chargemode']: NPB_chargemode(int(sys.argv[2]))
elif sys.argv[1] in ['NPB_readcurve']: NPB_readcurve()
else:
print("")
print("Unknown first argument '" + sys.argv[1] + "'")
mwcan_commands()
error = 1
return
#### Main
atexit.register(on_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)
#CAN INIT
if USEDMW == 0xFF:
print("ERROR - YOU NEED TO CONFIGURE THE DEVICE USED IN THE BEGINNING OF THIS SCRIPT")
sys.exit(1)
if USEDID == "":
print("ERROR - YOU NEED TO CONFIGURE THE ID IN THE BEGINNING OF THIS SCRIPT")
sys.exit(1)
mylogs = logging.getLogger()
mylogs.setLevel(LOGLEVEL)
if logtofile == 1:
file = logging.FileHandler(self.logpath, mode='a')
file.setLevel(LOGLEVEL)
fileformat = logging.Formatter("%(asctime)s:%(module)s:%(levelname)s:%(message)s",datefmt="%H:%M:%S")
file.setFormatter(fileformat)
mylogs.addHandler(file)
if logtoconsole == 1:
stream = logging.StreamHandler()
stream.setLevel(LOGLEVEL)
streamformat = logging.Formatter("%(asctime)s:%(module)s:%(levelname)s:%(message)s",datefmt="%H:%M:%S")
stream.setFormatter(streamformat)
mylogs.addHandler(stream)
candev = mwcan(USEDMW,USEDID,RS232DEV,LOGLEVEL)
candev.can_up()
print("Found Device: " + candev.mwtype)
command_line_argument()
sys.exit(0)