-
Notifications
You must be signed in to change notification settings - Fork 0
/
psu.py
108 lines (82 loc) · 2.13 KB
/
psu.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
import sys
from serial import Serial
psu_path = "/dev/ttyACM0"
def _send(func, decode=True):
"""
Decorator for sending a command to the power supply.
"""
def wrapper(*args, **kwargs):
cmd = f"{func(*args, **kwargs)}\r".encode()
try:
with Serial(psu_path, baudrate=9600, timeout=0.1) as p:
p.write(cmd)
response = p.readline()
if response:
try:
return float(response.decode())
except ValueError:
binary_str = bin(
int.from_bytes(response, byteorder=sys.byteorder)
)
return binary_str
except Exception as e:
print(f"PSU send failed: {e}")
return wrapper
@_send
def _get_status():
"""
Returns 8bit message.
"""
return "STATUS?"
@_send
def get_current(channel: int, realtime: bool = False):
"""
Returns either the programmed setting or a realtime reading for channel.
"""
return f"I{('SET','OUT')[realtime]}{channel}?"
@_send
def set_current(channel: int, value: float):
"""
Sets current on channel to the value.
"""
return f"ISET{channel}:{value}"
@_send
def get_voltage(channel: int, realtime: bool = False):
"""
Returns either the programmed setting or a realtime reading for channel.
"""
return f"V{('SET','OUT')[realtime]}{channel}?"
@_send
def set_voltage(channel: int, value: float):
"""
Sets voltage on channel to the value.
"""
return f"VSET{channel}:{value}"
@_send
def set_mode(mode: int):
"""
Sets the power supply mode to: [0]NORMAL | [1]SERIAL | [2]PARALLEL
"""
return f"TRACK{mode}"
@_send
def set_ocp(state: bool):
"""
Sets the overcurrent protection state.
"""
return f"OCP{state}"
def get_output():
return int(_get_status()[7])
@_send
def set_output(state: bool):
"""
Sets the output state.
"""
return f"OUT{int(state)}"
@_send
def get_id():
"""
Returns the ID.
"""
return "*IDN?"
if __name__ == "__main__":
pass