-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkunkin-kp184.py
executable file
·320 lines (278 loc) · 13 KB
/
kunkin-kp184.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
#!/usr/bin/python3
# preferred when using usb-serial adapter. Works around annoyance that device hooks
# and com-port names are rearranged between reboots and plugging-unplugging. Addresses
# devices by physical location in usb ports, including usb hubs. Works in both linux
# and windows(10).
# Get list of Your serial devices:
# $ python -m serial.tools.list_ports -v
# "LOCATION" field is physical usb port.
MY_DEVICE = "1-2"
# Internal serial port in windows:
# MY_DEVICE = "COM1"
# Internal serial port in linux:
# MY_DEVICE = "/dev/ttyS0"
import serial.tools.list_ports
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder
from pymodbus.constants import Endian
import math
import sys
def main():
mode_seen = False
requested_mode = None
dcload = Kunkin_KP184()
if len(sys.argv) < 2:
print_help_and_exit()
for param in sys.argv[1:]:
if mode_seen == True:
mode_value = float(param.replace(',', '.'))
if requested_mode == "CV":
dcload.set_CV_setting(mode_value)
print("Configured Constant Voltage is {} V".format(dcload.get_CV_setting()))
elif requested_mode == "CC":
dcload.set_CC_setting(mode_value)
print("Configured Constant Current is {} A".format(dcload.get_CC_setting()))
elif requested_mode == "CW":
dcload.set_CW_setting(mode_value)
print("Configured Constant Power is {} W".format(dcload.get_CW_setting()))
elif requested_mode == "CR":
dcload.set_CR_setting(mode_value)
print("Configured Constant Resistance is {} Ω".format(dcload.get_CR_setting()))
mode_seen = False
requested_mode = None
elif param == "status":
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
print("Measured Voltage is {} V".format(dcload.get_U_measure()))
if loadstatus == "ON":
print("Measured Current is {} A".format(dcload.get_I_measure()))
print("Measured Power is {} W".format(dcload.get_P_measure()))
print("Measured Resistance is {} Ω".format(dcload.get_R_measure()))
workmode = dcload.get_load_mode()
print("Work mode is {}".format(workmode))
if workmode == "CV":
print("Configured Constant Voltage is {} V".format(dcload.get_CV_setting()))
elif workmode == "CC":
print("Configured Constant Current is {} A".format(dcload.get_CC_setting()))
elif workmode == "CW":
print("Configured Constant Power is {} W".format(dcload.get_CW_setting()))
elif workmode == "CC":
print("Configured Constant Resistance is {} Ω".format(dcload.get_CR_setting()))
elif param == "on":
dcload.set_load_onoff("ON")
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
elif param == "off":
dcload.set_load_onoff("OFF")
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
elif param == "cv":
mode_seen = True
requested_mode = "CV"
load_mode = dcload.get_load_mode()
if load_mode != "CV":
loadstatus = dcload.get_load_onoff()
if loadstatus != "OFF":
print("Work Mode change: switching load off")
dcload.set_load_onoff("OFF")
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
dcload.set_load_mode("CV")
workmode = dcload.get_load_mode()
print("Work mode is {}".format(workmode))
elif param == "cc":
mode_seen = True
requested_mode = "CC"
load_mode = dcload.get_load_mode()
if load_mode != "CC":
loadstatus = dcload.get_load_onoff()
if loadstatus != "OFF":
print("Work Mode change: switching load off")
dcload.set_load_onoff("OFF")
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
dcload.set_load_mode("CC")
workmode = dcload.get_load_mode()
print("Work mode is {}".format(workmode))
elif param in ["cw", "cp"]:
mode_seen = True
requested_mode = "CW"
load_mode = dcload.get_load_mode()
if load_mode != "CW":
loadstatus = dcload.get_load_onoff()
if loadstatus != "OFF":
print("Work Mode change: switching load off")
dcload.set_load_onoff("OFF")
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
dcload.set_load_mode("CW")
workmode = dcload.get_load_mode()
print("Work mode is {}".format(workmode))
elif param == "cr":
mode_seen = True
requested_mode = "CR"
load_mode = dcload.get_load_mode()
if load_mode != "CR":
loadstatus = dcload.get_load_onoff()
if loadstatus != "OFF":
print("Work Mode change: switching load off")
dcload.set_load_onoff("OFF")
loadstatus = dcload.get_load_onoff()
print("Load is {}".format(loadstatus))
dcload.set_load_mode("CR")
workmode = dcload.get_load_mode()
print("Work mode is {}".format(workmode))
else:
print_help_and_exit()
def print_help_and_exit():
print(
"""Usage: kunkin-kp184.py status
Usage: kunkin-kp184.py on
Usage: kunkin-kp184.py off
Usage: kunkin-kp184.py cc 0.1
Usage: kunkin-kp184.py cv 0.2
Usage: kunkin-kp184.py cr 200
Usage: kunkin-kp184.py cv 0.3
Usage: kunkin-kp184.py cc 0.5 on status"""
, file=sys.stderr)
sys.exit(1)
class Kunkin_KP184:
client = None
UNIT = 0x1
LOAD_ONOFF = ["OFF", "ON"]
LOAD_MODE = ["CV", "CC", "CR", "CW"]
def __init__(self):
if MY_DEVICE.startswith("/dev") or MY_DEVICE.upper().startswith("COM"):
dev_hook = MY_DEVICE
else:
for cport in serial.tools.list_ports.comports():
if cport.location == MY_DEVICE:
dev_hook = cport.device
break
else:
raise ValueError("Serial port or adapter was not found at {}".format(MY_DEVICE))
self.client = ModbusClient(method="rtu", port=dev_hook, timeout=0.2, baudrate=9600, stopbits=1, bytesize=8, parity="N")
def __del__(self):
if self is not None:
if self.client is not None:
self.client.close()
def get_U_measure(self):
rr = self.client.read_holding_registers(0x0122, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
voltage = decoder.decode_32bit_uint() / 1000
return voltage
def get_I_measure(self):
rr = self.client.read_holding_registers(0x0126, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
amperage = decoder.decode_32bit_uint() / 1000
return amperage
def get_P_measure(self):
return round(self.get_U_measure() * self.get_I_measure(), 2)
def get_R_measure(self):
try:
return round(self.get_U_measure() / self.get_I_measure(), 2)
except ZeroDivisionError:
return math.inf
def get_load_mode(self):
rr = self.client.read_holding_registers(0x0110, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
res = decoder.decode_32bit_uint()
return self.LOAD_MODE[res & 0xFF]
def set_load_mode(self, load_mode):
mode = self.LOAD_MODE.index(load_mode)
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
builder.add_32bit_uint(mode)
payload = builder.build()
tosend = bytearray(b'\x00\x01\x04')
for c in payload:
tosend.extend(c)
rq = self.client.write_register(0x0110, tosend, skip_encode=True, unit=self.UNIT)
def get_load_onoff(self):
rr = self.client.read_holding_registers(0x010E, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
res = decoder.decode_32bit_uint()
return self.LOAD_ONOFF[res & 0xFF]
def set_load_onoff(self, status):
status = str(status)
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
if status in ["0", "off", "OFF"]:
builder.add_32bit_uint(0)
elif status in ["1", "on", "ON"]:
builder.add_32bit_uint(1)
else:
raise ValueError("set_load_onoff(0 | off | OFF | 1 | on | ON)")
payload = builder.build()
tosend = bytearray(b'\x00\x01\x04')
for c in payload:
tosend.extend(c)
rq = self.client.write_register(0x010E, tosend, skip_encode=True, unit=self.UNIT)
def get_CV_setting(self):
rr = self.client.read_holding_registers(0x0112, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
voltage = decoder.decode_32bit_uint() / 1000
return voltage
def set_CV_setting(self, voltage):
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
builder.add_32bit_uint(int(round(voltage * 1000, 0)))
payload = builder.build()
tosend = bytearray(b'\x00\x01\x04')
for c in payload:
tosend.extend(c)
rq = self.client.write_register(0x0112, tosend, skip_encode=True, unit=self.UNIT)
def get_CC_setting(self):
rr = self.client.read_holding_registers(0x0116, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
amperage = decoder.decode_32bit_uint() / 1000
return amperage
def set_CC_setting(self, amperage):
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
builder.add_32bit_uint(int(round(amperage * 1000, 0)))
payload = builder.build()
tosend = bytearray(b'\x00\x01\x04')
for c in payload:
tosend.extend(c)
rq = self.client.write_register(0x0116, tosend, skip_encode=True, unit=self.UNIT)
def get_CR_setting(self):
rr = self.client.read_holding_registers(0x011A, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
resistance = decoder.decode_32bit_uint() / 10
return resistance
def set_CR_setting(self, resistance):
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
builder.add_32bit_uint(int(round(resistance * 10, 0)))
payload = builder.build()
tosend = bytearray(b'\x00\x01\x04')
for c in payload:
tosend.extend(c)
rq = self.client.write_register(0x011A, tosend, skip_encode=True, unit=self.UNIT)
def get_CW_setting(self):
rr = self.client.read_holding_registers(0x011E, count=4, unit=self.UNIT)
if rr.isError():
raise ValueError(rr)
decoder = BinaryPayloadDecoder.fromRegisters(rr.registers, wordorder=Endian.Big, byteorder=Endian.Big)
power = decoder.decode_32bit_uint() / 100
return power
def set_CW_setting(self, power):
builder = BinaryPayloadBuilder(byteorder=Endian.Big, wordorder=Endian.Big)
builder.add_32bit_uint(int(round(power * 100, 0)))
payload = builder.build()
tosend = bytearray(b'\x00\x01\x04')
for c in payload:
tosend.extend(c)
rq = self.client.write_register(0x011E, tosend, skip_encode=True, unit=self.UNIT)
if __name__ == "__main__":
main()