forked from akira-okumura/PyGPD3303S
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpd3303s.py
340 lines (274 loc) · 9.08 KB
/
gpd3303s.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
"""
This is an interface module for DC Power Supply GPD-3303S manufactured by Good
Will Instrument Co., Ltd.
"""
import exceptions
import serial
import sys
class MySerial(serial.Serial):
"""
Wrapper for Serial
"""
try:
import io
except ImportError:
# serial.Serial inherits serial.FileLike
pass
else:
def readline(self, eol='\r'):
"""
Overrides io.RawIOBase.readline which cannot handle with '\r' delimiters
"""
leneol = len(eol)
ret = ''
while True:
c = self.read(1)
if c:
ret += c
if ret[-leneol:] == eol:
break
else:
break
return ret
class GPD3303S(object):
def __init__(self):
self.__baudRate = 9600 # 9600 bps
self.__parityBit = 'N' # None
self.__dataBit = 8
self.__stopBit = 1
self.__dataFlowControl = None
self.eol = '\r'
def open(self, port, readTimeOut = 1, writeTimeOut = 1):
self.serial = MySerial(port = port,
baudrate = self.__baudRate,
bytesize = self.__dataBit,
parity = self.__parityBit,
stopbits = self.__stopBit,
timeout = readTimeOut,
writeTimeout = writeTimeOut,
dsrdtr = self.__dataFlowControl)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
# Check if the delimiter is properly set
# By default, \r is the delimiter, but newer GPD3303S uses \r\n instead.
self.serial.setTimeout(0.1)
ret = self.serial.read(1)
self.serial.setTimeout(readTimeOut)
if ret == '\n':
self.setDelimiter('\r\n')
def close(self):
self.serial.close()
def setDelimiter(self, eol = '\r\n'):
"""
Must call this method for new-firmware (2.0 or above?) instruments.
Because the delimiter setting has been changed.
"""
self.eol = eol
def isValidChannel(self, channel):
"""
Check if the given channel number is valid or not. Only channel 1 and 2
are allowed.
"""
if not (channel == 1 or channel == 2):
raise exceptions.RuntimeError('Invalid channel number: %d was given.' % channel)
return True
def isValidMemory(self, memory):
"""
Check if the given memory number is valid or not. Only memory 1 to 2
are allowed.
"""
if not (memory <= 0 or 4 < memory):
raise exceptions.RuntimeError('Invalid memory number: %d was given.' % memory)
return True
def isValidFloat(self, value):
"""
Check if the given float number is valid or not. Three or less
significant figures are allowed.
"""
if value < 0:
raise exceptions.RuntimeError('Invalid float value: %f was given.' % value)
str = "%f" % value
position = str.find(".")
maxDigits = 5
if 0 <= position and position <= maxDigits : # found
str = str[0:maxDigits + 1]
else: # not found
str = str[0:maxDigits]
if float(str) != value:
sys.stderr.write('Invalid float value: %f was given.' % value)
return False
return True
def setCurrent(self, channel, current):
"""
ISET<X>:<NR2>
"""
self.isValidChannel(channel)
self.serial.write('ISET%d:%.3f\n' % (channel, current))
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def getCurrent(self, channel):
"""
ISET<X>?
"""
self.isValidChannel(channel)
self.serial.write('ISET%d?\n' % channel)
ret = self.serial.readline(eol=self.eol)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
return float(ret[:-len(self.eol)].replace('A', ''))
def setVoltage(self, channel, voltage):
"""
VSET<X>:<NR2>
"""
self.isValidChannel(channel)
self.serial.write('VSET%d:%.3f\n' % (channel, voltage))
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def getVoltage(self, channel):
"""
VSET<X>?
"""
self.isValidChannel(channel)
self.serial.write('VSET%d?\n' % channel)
ret = self.serial.readline(eol=self.eol)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
return float(ret[:-len(self.eol)].replace('V', ''))
def getCurrentOutput(self, channel):
"""
IOUT<X>?
"""
self.isValidChannel(channel)
self.serial.write('IOUT%d?\n' % channel)
ret = self.serial.readline(eol=self.eol)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
return float(ret[:-len(self.eol)].replace('A', ''))
def getVoltageOutput(self, channel):
"""
VOUT<X>?
"""
self.isValidChannel(channel)
self.serial.write('VOUT%d?\n' % channel)
ret = self.serial.readline(eol=self.eol)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
return float(ret[:-len(self.eol)].replace('V', ''))
def selectIndependentMode(self):
"""
TRACK<NR1>
"""
self.serial.write('TRACK0\n')
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def selectTrackingSeriesMode(self):
"""
TRACK<NR1>
"""
self.serial.write('TRACK1\n')
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def selectTrackingParallelMode(self):
"""
TRACK<NR1>
"""
self.serial.write('TRACK2\n')
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def enableBeep(self, enable = True):
"""
BEEP<Boolean>
"""
self.serial.write('BEEP%d\n' % int(enable))
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def enableOutput(self, enable = True):
"""
OUT<Boolean>
"""
self.serial.write('OUT%d\n' % int(enable))
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def printStatus(self):
"""
STATUS?
"""
self.serial.write('STATUS?\n')
for i in range(3):
ret = self.serial.readline(eol=self.eol)
print ret[:-len(self.eol)]
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def getIdentification(self):
"""
*IDN?
"""
self.serial.write('*IDN?\n')
ret = self.serial.readline(eol=self.eol)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
return ret[:-len(self.eol)]
def recallSetting(self, memory):
"""
RCL<NR1>
"""
self.isValidMemory(memory)
self.serial.write('RCL%d\n' % memory)
ret = self.serial.readline(eol=self.eol)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
return ret[:-len(self.eol)]
def saveSetting(self, memory):
"""
SAV<NR1>
"""
self.isValidMemory(memory)
self.serial.write('SAV%d\n' % memory)
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def printHelp(self):
"""
HELP?
"""
self.serial.write('HELP?\n')
for i in range(19):
ret = self.serial.readline(eol=self.eol)
print ret[:-len(self.eol)]
err = self.getError()
if err != 'No Error.':
raise exceptions.RuntimeError(err)
def getError(self):
"""
ERR?
"""
self.serial.write('ERR?\n')
ret = self.serial.readline(eol=self.eol)
if ret != '':
return ret[:-len(self.eol)]
else:
raise exceptions.RuntimeError('Cannot read error message')
class GPD4303S(GPD3303S):
def isValidChannel(self, channel):
"""
Check if the given channel number is valid or not. Only channels 1 to 4
are allowed.
"""
if not (1 <= channel <= 4):
raise exceptions.RuntimeError('Invalid channel number: %d was given.' % channel)
return True