-
Notifications
You must be signed in to change notification settings - Fork 0
/
takasago.py
executable file
·181 lines (146 loc) · 5.39 KB
/
takasago.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
#!/usr/bin/python
#-*- coding: latin-1 -*-
"""Module to interface to a Takagaso EX style power supply via serial interface.
For the ZX-S-400L power supply one needs to put it into EX compatibility mode.
This is done by putting function setting 61 to 1. Also note that the device
address is registered in function setting register 60.
"""
import serial
class TakasagoPowersupply (object):
"""Handle basic output set and query operations of Takasago power supplies"""
def __init__ (self, device = '/dev/ttyAMA0', address = 1, max_current = 1., model = "ZX", position = None):
"""Create and initialize the TakasagoPowersupply object.
:param device: path to serical device (default: /dev/ttyUSB0)
:param address: bus address of the powersupply (default: 1)
:param max_current: maximum allowable current in A (default: 1.0)
:param model: powersupply model, "ZX" or "KX" (default: "ZX")
:param position: parameter to determine the position of the powersupply
in a list (integer, default: None)
"""
self.address = address
self.max_current = max_current
self.device = device
self.model = model
self.position = position
self.serial = serial.Serial (
port = self.device,
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 0.5
)
if model == "ZX":
self.commands = {
"GetCurrent": "TK5",
"SetCurrent": "MC",
"GetVoltage": "TK4",
"SetVoltage": "MV",
"SetOutput": "OT"
}
if model == "KX":
self.commands = {
"GetCurrent": "TK7",
"SetCurrent": "OC",
"GetVoltage": "TK6",
"SetVoltage": "OV",
"SetOutput": "OT"
}
def _sendCommand (self, address, cmd):
"""Low level function to send a command to a specific device
:param address: power supply address
:param cmd: command string to be sent
"""
self.serial.flushInput ()
self.serial.flushOutput ()
self.serial.write ("A" + str(address) + "," + cmd + "\r")
self.serial.flush ()
def _readReply (self):
"""Read a reply string from the serial bus
:return: single line reply strong or None if timeout
"""
resp = ''
while True:
char = self.serial.read ()
if char == '\r':
break
if len (char):
resp += char
else:
# We had a timeout
#print "timeout when waiting for Takasago response"
return None
return resp
def setOutput (self, state):
"""Enables/Disable the output of the powersupply.
:param state: boolean parameter indicating the state (True = On, False = Off)
:return: True on success
"""
try:
if state:
self._sendCommand (self.address, self.commands["SetOutput"] + "1")
else:
self._sendCommand (self.address, self.commands["SetOutput"] + "0")
except:
return False
return True
def setCurrent (self, current):
"""Set the powersupply current, clipping to the max_current
:param current: set current in A
:return: True on success
"""
if (current > self.max_current):
current = self.max_current
if (current < 0):
current = 0
try:
self._sendCommand (self.address, self.commands["SetCurrent"] + str(current))
except:
return False
return True
def getCurrent (self):
"""Query the actual current of the powersupply
:return: current in A or None if not successful
"""
try:
self._sendCommand (self.address, self.commands["GetCurrent"])
reply = self._readReply ()
return float (reply[0:-1])
except:
return None
return None
def setVoltage (self, voltage):
"""Set the powersupply voltage
:param current: set voltage in V
:return: True on success
"""
if voltage < 0:
voltage = 0
try:
self._sendCommand (self.address, self.commands["SetVoltage"] + str(voltage))
except:
return False
return True
def getVoltage (self):
"""Query the actual voltage of the powersupply
:return: voltage in V or None if not successful
"""
try:
self._sendCommand (self.address, self.commands["GetVoltage"])
reply = self._readReply ()
return float (reply[0:-1])
except:
return None
return None
def close (self):
"""Close serial connection"""
self.serial.close ()
if __name__ == '__main__':
"""Just testing"""
psu = TakasagoPowersupply (address = 3, model = "KX")
#psu.setOutput (True)
#psu.setCurrent (0.3)
print psu.getVoltage ()
print psu.getCurrent ()
#psu.setOutput (False)
psu.close ()