-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyVisa_Rigol_DP832A_class.py
299 lines (256 loc) · 10.4 KB
/
PyVisa_Rigol_DP832A_class.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed 12. Jan CET 2022
@author: Bjoern Kasper (urmel79)
Wrapper class to communicate with the Rigol DP832A via LAN interface and SCPI commands using PyVisa and PyVISA-py
"""
import pyvisa
import time, sys
class PyVisa_Rigol_DP832A():
def __init__(self, tcp_ip):
self._ip = tcp_ip
self._delay = 0.01 #delay for writing the commands in seconds (10 ms)
# define voltage and current limits as constants
self.VOLTAGE_MIN = 0.0
self.VOLTAGE_MAX_1_2 = 32.0 # max 32 V
self.VOLTAGE_MAX_3 = 5.3 # max 5.3 V
self.CURRENT_MIN = 0.0
self.CURRENT_MAX = 3.2 # max 3.2 A
self.OVP_MIN = 0.001 # min 0.001 V
self.OVP_MAX_1_2 = 33.0 # max 33 V
self.OVP_MAX_3 = 5.5 # max 5.5 V
self.OCP_MIN = 0.001 # min 0.001 A
self.OCP_MAX = 3.3 # max 3.3 A
try:
if self._ip == []:
self.status = "No IP address provided"
elif self._ip != []:
self.rm = pyvisa.ResourceManager('@py')
self.psu_res = 'TCPIP0::%s::INSTR' %self._ip
self.psu = self.rm.open_resource(self.psu_res)
self.status = "Connected"
self.connected_with = 'LAN over %s' %self._ip
except pyvisa.VisaIOError:
self.status = "Disconnected"
self.connected_with = 'Nothing'
print("Pyvisa is not able to connect with the device")
#define a OPEN CONNECTION function
def openConnection(self, tcp_ip):
try:
if self.status == "Disconnected":
if tcp_ip == []:
self.status = "No IP address provided"
elif tcp_ip != []:
self.rm = pyvisa.ResourceManager('@py')
self.psu_res = 'TCPIP0::%s::INSTR' %tcp_ip
self.psu = self.rm.open_resource(self.psu_res)
self.status = "Connected"
self.connected_with = 'LAN over %s' %tcp_ip
except pyvisa.VisaIOError:
self.status = "Disconnected"
self.connected_with = "Nothing"
print("Pyvisa is not able to connect with the device")
#define a CLOSE CONNECTION function
def closeConnection(self):
try:
if self.status == "Connected":
self.rm.close()
self.status = "Disconnected"
self.connected_with = "Nothing"
except pyvisa.VisaIOError:
self.status = "Error"
print("Device is not connected")
#define a TOGGLE OUTPUT function
def toggleOutput(self, chan, state):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
if ((state == 'ON') or (state == 'OFF')):
self.cmd1 = ':OUTP CH%s,%s' %(chan, state)
self.psu.write(self.cmd1)
time.sleep(self._delay)
return chan, state
else:
print("Wrong state provided <'ON'|'OFF'>")
return -1
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a SET VOLTAGE function
def setVoltage(self, chan, voltage):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
# check voltage limits
if (voltage < self.VOLTAGE_MIN): voltage = self.VOLTAGE_MIN
if ((chan == 1) or (chan == 2)):
if (voltage > self.VOLTAGE_MAX_1_2): voltage = self.VOLTAGE_MAX_1_2
elif (chan == 3):
if (voltage > self.VOLTAGE_MAX_3): voltage = self.VOLTAGE_MAX_3
self.chan_curr = self.psu.query(':INST:NSEL?')
if (self.chan_curr != chan):
self.cmd1 = ':INST:NSEL %s' %chan
self.cmd2 = ':VOLT %s' %voltage
self.psu.write(self.cmd1)
time.sleep(self._delay)
self.psu.write(self.cmd2)
time.sleep(self._delay)
return chan, voltage
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a SET CURRENT function
def setCurrent(self, chan, current):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
# check current limits
if (current < self.CURRENT_MIN): current = self.CURRENT_MIN
if (current > self.CURRENT_MAX): current = self.CURRENT_MAX
self.chan_curr = self.psu.query(':INST:NSEL?')
if (self.chan_curr != chan):
self.cmd1 = ':INST:NSEL %s' %chan
self.cmd2 = ':CURR %s' %current
self.psu.write(self.cmd1)
time.sleep(self._delay)
self.psu.write(self.cmd2)
time.sleep(self._delay)
return chan, current
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a SET OVERVOLTAGE PROTECTION function
def setOVP(self, chan, ovp):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
# check ovp limits
if (ovp < self.OVP_MIN): ovp = self.OVP_MIN
if ((chan == 1) or (chan == 2)):
if (ovp > self.OVP_MAX_1_2): ovp = self.OVP_MAX_1_2
elif (chan == 3):
if (ovp > self.OVP_MAX_3): ovp = self.OVP_MAX_3
self.chan_curr = self.psu.query(':INST:NSEL?')
if (self.chan_curr != chan):
self.cmd1 = ':INST:NSEL %s' %chan
self.cmd2 = ':VOLT:PROT %s' %ovp
self.psu.write(self.cmd1)
time.sleep(self._delay)
self.psu.write(self.cmd2)
time.sleep(self._delay)
return chan, ovp
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a TOGGLE OVERVOLTAGE PROTECTION function
def toggleOVP(self, chan, state):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
if ((state == 'ON') or (state == 'OFF')):
self.chan_curr = self.psu.query(':INST:NSEL?')
if (self.chan_curr != chan):
self.cmd1 = ':INST:NSEL %s' %chan
self.cmd2 = ':VOLT:PROT:STAT %s' %state
self.psu.write(self.cmd1)
time.sleep(self._delay)
self.psu.write(self.cmd2)
time.sleep(self._delay)
return chan, state
else:
print("Wrong state provided <'ON'|'OFF'>")
return -1
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a SET OVERCURRENT PROTECTION function
def setOCP(self, chan, ocp):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
# check ocp limits
if (ocp < self.OCP_MIN): ocp = self.OCP_MIN
if (ocp > self.OCP_MAX): ocp = self.OCP_MAX
self.chan_curr = self.psu.query(':INST:NSEL?')
if (self.chan_curr != chan):
self.cmd1 = ':INST:NSEL %s' %chan
self.cmd2 = ':CURR:PROT %s' %ocp
self.psu.write(self.cmd1)
time.sleep(self._delay)
self.psu.write(self.cmd2)
time.sleep(self._delay)
return chan, ocp
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a TOGGLE OVERCURRENT PROTECTION function
def toggleOCP(self, chan, state):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
if ((state == 'ON') or (state == 'OFF')):
self.chan_curr = self.psu.query(':INST:NSEL?')
if (self.chan_curr != chan):
self.cmd1 = ':INST:NSEL %s' %chan
self.cmd2 = ':CURR:PROT:STAT %s' %state
self.psu.write(self.cmd1)
time.sleep(self._delay)
self.psu.write(self.cmd2)
time.sleep(self._delay)
return chan, state
else:
print("Wrong state provided <'ON'|'OFF'>")
return -1
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a MEASURE VOLTAGE function
def measVolt(self, chan):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
self.cmd1 = ':MEAS:VOLT? CH%s' %chan
self.V = self.psu.query(self.cmd1)
self.V = float(self.V)
time.sleep(self._delay)
return chan, self.V
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a MEASURE CURRENT function
def measCurrent(self, chan):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
self.cmd1 = ':MEAS:CURR? CH%s' %chan
self.C = self.psu.query(self.cmd1)
self.C = float(self.C)
time.sleep(self._delay)
return chan, self.C
else:
print("Wrong channel selected <1|2|3>")
return -1
#define a MEASURE POWER function
def measPower(self, chan):
if (self.status != "Connected"):
print("Device is not connected")
return -1
if ((chan == 1) or (chan == 2) or (chan == 3)):
self.cmd1 = ':MEAS:POWE? CH%s' %chan
self.P = self.psu.query(self.cmd1)
self.P = float(self.P)
time.sleep(self._delay)
return chan, self.P
else:
print("Wrong channel selected <1|2|3>")
return -1