-
Notifications
You must be signed in to change notification settings - Fork 11
/
kel103.py
148 lines (116 loc) · 4.44 KB
/
kel103.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
import socket
import time
class koradUdpComm(object):
def __init__(self, localAddress, deviceAddress, port):
self.clientAddress = (localAddress, port)
self.deviceAddress = (deviceAddress, port)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def connect(self):
self.sock.bind(self.clientAddress)
self.sock.settimeout(1.0)
def close(self):
self.sock.close()
def udpSendRecv(self, message):
# build the message
messageb = bytearray()
messageb.extend(map(ord, message))
messageb.append(0x0a)
startTime = time.time()
while 1:
sent = self.sock.sendto(messageb , self.deviceAddress)
self.sock.settimeout(1.0)
data, server = self.sock.recvfrom(1024)
if len(data) > 0:
return data.decode('utf-8')
if time.time() - startTime > 3:
print ("UDP timeout")
return " "
def udpSend(self, message):
# build the message
messageb = bytearray()
messageb.extend(map(ord, message))
messageb.append(0x0a)
sent = self.sock.sendto(messageb , self.deviceAddress)
class kel103(object):
def __init__(self, localAddress, deviceAddress, port):
self.device = koradUdpComm(localAddress, deviceAddress, port)
self.device.connect()
def deviceInfo(self):
return self.device.udpSendRecv('*IDN?')
def checkDevice(self):
if 'KEL103' in self.deviceInfo():
return True
else:
return False
def measureVolt(self):
s = self.device.udpSendRecv(':MEAS:VOLT?')
return float(s.strip('V\n'))
def measureSetVolt(self):
s = self.device.udpSendRecv(':VOLT?')
return float(s.strip('V\n'))
def setVolt(self, voltage):
s = self.device.udpSend(':VOLT ' + str(voltage) + 'V')
if self.measureSetVolt() != voltage:
raise ValueError('Voltage set incorectly on the device')
def measurePower(self):
s = self.device.udpSendRecv(':MEAS:POW?')
return float(s.strip('W\n'))
def measureSetPower(self):
s = self.device.udpSendRecv(':POW?')
return float(s.strip('W\n'))
def setPower(self, power):
s = self.device.udpSend(':POW ' + str(power) + 'W')
if self.measureSetPower() != power:
raise ValueError('Power set incorectly on the device')
def measureCurrent(self):
s = self.device.udpSendRecv(':MEAS:CURR?')
return float(s.strip('A\n'))
def measureSetCurrent(self):
s = self.device.udpSendRecv(':CURR?')
return float(s.strip('A\n'))
def setCurrent(self, current):
s = self.device.udpSend(':CURR ' + str(current) + 'A')
if self.measureSetCurrent() != current:
raise ValueError('Current set incorectly on the device')
def checkOutput(self):
s = self.device.udpSendRecv(':INP?')
if 'OFF' in s:
return False
if 'ON' in s:
return True
def setOutput(self, state):
if state == True:
self.device.udpSend(':INP 1')
if self.checkOutput() != state:
raise ValueError('Caution: Output not set')
if state == False:
self.device.udpSend(':INP 0')
if self.checkOutput() != state:
raise ValueError('Caution: Output not set')
def setConstantCurrent(self):
self.device.udpSend(':FUNC CC')
def setConstantPower(self):
self.device.udpSend(':FUNC CW')
def setConstantResistance(self):
self.device.udpSend(':FUNC CR')
def setDynamicModeCV(self, voltage1, voltage2, freq, dutycycle):
cmd = ':DYN 1,'
cmd += '%.3fV,' % voltage1
cmd += '%.3fV,' % voltage2
cmd += '%.3fHZ,' % freq
cmd += '%.3f%%' % dutycycle
s = self.device.udpSend(cmd)
def setDynamicModeCC(self, slope1, slope2, current1, current2, freq, dutycycle):
cmd = ':DYN 2,'
cmd += '%.3fA/uS,' % slope1
cmd += '%.3fA/uS,' % slope2
cmd += '%.3fA,' % current1
cmd += '%.3fA,' % current2
cmd += '%.3fHZ,' % freq
cmd += '%.3f%%' % dutycycle
s = self.device.udpSend(cmd)
def getDynamicMode(self):
s = self.device.udpSendRecv(':DYN?')
return s.strip('\n')
def endComm(self):
self.device.close()