-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHM310P.py
executable file
·175 lines (151 loc) · 4.34 KB
/
HM310P.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
#!/usr/bin/python3
import minimalmodbus
import serial
import time
class HM310P():
rDepth = 100
def __init__(self):
self.supply = minimalmodbus.Instrument('/dev/dcpowersupply', 1, minimalmodbus.MODE_RTU)
self.supply.serial.baudrate = 9600
self.supply.serial.startbits = 1
self.supply.serial.stopbits = 1
self.supply.serial.parity = serial.PARITY_NONE
self.supply.serial.bytesize = 8
self.supply.timeout = 0.5
##########################
#### Power Management ####
##########################
def get_power(self):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.read_power()
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def read_power(self):
try:
power = self.supply.read_register(1,0)
return power
except:
return "Error"
def power_on(self):
self.set_power(1)
def power_off(self):
self.set_power(0)
def set_power(self, status):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.write_power(status)
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def write_power(self, status):
try:
self.supply.write_register(1, status, 0)
return True
except:
return "Error"
############################
#### Voltage Management ####
############################
def get_voltage(self):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.read_voltage()
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def read_voltage(self):
try:
voltage = self.supply.read_register(16,2)
return voltage
except:
return "Error"
def set_voltage(self, voltage):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.write_voltage(voltage)
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def write_voltage(self, voltage):
try:
self.supply.write_register(48, voltage, 2)
return True
except:
return "Error"
############################
#### Current Management ####
############################
def set_current(self, current):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.write_current(current)
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def write_current(self, current):
try:
self.supply.write_register(49, current, 2)
return True
except:
return "Error"
###############################
#### Protection Management ####
###############################
def set_overvoltageprotection(self, voltage):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.write_overvoltageprotection(voltage)
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def write_overvoltageprotection(self, voltage):
try:
self.supply.write_register(32, voltage, 2)
return True
except:
return "Error"
def set_overcurrentprotection(self, current):
r = 0
value = "Error"
while r <= self.rDepth:
value = self.write_overcurrentprotection(current * 10)
if not value == "Error":
return value
r += 1
time.sleep(0.001)
return False
def write_overcurrentprotection(self, current):
try:
self.supply.write_register(33, current, 2)
return True
except:
return "Error"
if __name__ == "__main__":
supply = HM310P()
supply.set_voltage(0)
supply.power_on()
time.sleep(5)
for i in range(0,101):
supply.set_voltage(i/100*5)
time.sleep(3)
supply.power_off()