forked from tymmothy/pyPSP603
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psp.py
253 lines (210 loc) · 8.81 KB
/
psp.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
"""This module provides an API for controlling GW-Instek PSP
power supplies over a serial port.
Copyright (c)
2011-2012, Timothy Twillman
2017, Pavel Revak
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY TIMOTHY TWILLMAN ''AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL TIMOTHY TWILLMAN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are
those of the authors and should not be interpreted as representing official
policies, either expressed or implied, of Timothy Twillman.
"""
import time
class Psp():
""" GW Instek PSP Power Supply Interface Class.
This class allows control of PSP power supplies via serial interface.
To use, instantiate this class with a (pySerial) serial port object.
Example:
>>> import serial
>>> import psp
>>> serial_port = serial.Serial('/dev/ttyS0', 2400)
>>> psu = psp.Psp(serial_port)
>>> psu.voltage = 4.2
>>> psu.relay = True
>>> print(psu.current)
would set the output voltage, close the output relay, then print the
output current.
Note:
This module is not derived from the Serial class specifically to allow
alternate methods of communication (e.g. the RFC2217Serial class).
"""
def __init__(self, serial_port):
""" Create a PSP object, binding it to the given in serial port.
Args:
serial_port (Serial / RFC2217Serial): Serial port instance.
"""
self._serial = serial_port
def send_command(self, cmd, wait=None):
""" Send a command to the PSU.
Args:
cmd (str): The command to send.
wait (float): Number of seconds to sleep after sending command.
"""
if isinstance(cmd, str):
cmd = map(ord, cmd)
self._serial.write(cmd)
self._serial.write([0x0d])
if wait:
time.sleep(wait)
def receive(self):
""" Receive a response (terminated by new line) from the PSU.
Note:
This function will block until a complete line is recieved.
Presently there is no way to specify a timeout.
"""
response = []
start_time = time.time()
while not response or response[-1] != 0x0a:
# Read in a char from the port.
response += list(self._serial.read())
return ''.join(map(chr, response))
@property
def voltage(self):
"""Output voltage, in volts. Read/write float value."""
self.send_command('V')
s = self.receive()
return float(s[1:])
@voltage.setter
def voltage(self, value):
self.send_command("SV %05.2F" % (value), 0.2)
@property
def voltage_limit(self):
"""Voltage limit, in volts. Read/write float value."""
self.send_command('U')
s = self.receive()
return float(s[1:])
@voltage_limit.setter
def voltage_limit(self, value):
self.send_command("SU %02d" % (value), 0.6)
@property
def current(self):
"""Output current, in amps. Read-only float value."""
# Note: Output current is read-only -- no setter.
self.send_command('A')
s = self.receive()
return float(s[1:])
@property
def current_limit(self):
"""Current limit, in amps. Read/write float value."""
self.send_command('I')
s = self.receive()
return float(s[1:])
@current_limit.setter
def current_limit(self, value):
self.send_command("SI %04.2F" % (value), 0.6)
@property
def power(self):
"""Output power, in watts. Read-only float value."""
self.send_command('W')
s = self.receive()
return float(s[1:])
@property
def power_limit(self):
"""Power limit, in watts. Read/write float value. """
self.send_command("P")
s = self.receive()
return float(s[1:])
@power_limit.setter
def power_limit(self, value):
self.send_command("SP %03d" % (value))
@property
def status(self):
"""Basic PSU status. Read-only dict value.
The dict has the following keys:
relay (bool) Relay status; output power is "ON" if True.
overheated (bool) PSU is overheating if True.
fine_control (bool) PSU is in "fine wheel control" mode if True.
wheel_locked (bool) PSU wheel is locked out if True.
remote (bool) PSU is in "remote" mode if True.
locked (bool) PSU is in "locked" mode if True.
"""
self.send_command('F')
s = self.receive()
return { 'relay' : True if s[1] == '1' else False,
'overheated' : True if s[2] == '1' else False,
'fine_control' : True if s[3] == '1' else False,
'wheel_locked' : True if s[4] == '1' else False,
'remote' : True if s[5] == '1' else False,
'locked' : True if s[6] == '1' else False }
@property
def full_status(self):
"""Full PSU status. Read-only dict value.
The dict has the following keys:
voltage (float) Present output voltage, in volts.
current (float) Present output current, in amps.
power (float) Present output power, in watts.
voltage_limit (float) Voltage limit, in volts.
current_limit (float) Current limit, in amps.
power_limit (float) Power limit, in watts.
relay (bool) Relay status; output power is "ON" if True.
overheated (bool) PSU is overheating if True.
fine_control (bool) PSU is in "fine wheel control" mode if True.
wheel_locked (bool) PSU wheel is locked out if True.
remote (bool) PSU is in "remote" mode if True.
locked (bool) PSU is in "locked" mode if True.
"""
self.send_command('L')
s = self.receive()
return {
'voltage' : float(s[1:6]),
'current' : float(s[7:12]),
'power' : float(s[13:18]),
'voltage_limit' : float(s[19:21]),
'current_limit' : float(s[22:26]),
'power_limit' : float(s[27:30]),
'relay' : True if s[31] == '1' else False,
'overheated' : True if s[32] == '1' else False,
'fine_control' : True if s[33] == '1' else False,
'wheel_locked' : True if s[34] == '1' else False,
'remote' : True if s[35] == '1' else False,
'locked' : True if s[36] == '1' else False }
@property
def overheated(self):
"""True if power supply is overheating. Read-only bool value."""
return self.status.get('overheated')
@property
def fine_control(self):
"""True if wheel is in fine control mode. Read-only bool value."""
return self.status.get('fine_control')
@property
def wheel_locked(self):
"""True if wheel is locked. Read-only bool value."""
return self.status.get('wheel_locked')
@property
def remote(self):
"""True if PSU is in remote mode. Read-only bool value. """
return self.status.get('remote')
@property
def locked(self):
"""True if PSU is locked. Read-only boolean value."""
return self.status.get('locked')
@property
def relay(self):
"""When true, relay is closed. Read/write bool value."""
return self.status.get('relay')
@relay.setter
def relay(self, value):
if value == True:
self.send_command('KOE', 0.5)
else:
self.send_command('KOD', 0.5)
# Note: Invert relay ("KO") easily emulated.
def save_param(self):
""" Save the current PSU parameter settings to internal EEPROM. """
self.send_command('EEP', 1.0);