-
Notifications
You must be signed in to change notification settings - Fork 0
/
psu.go
192 lines (164 loc) · 3.72 KB
/
psu.go
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
package psu
import (
"fmt"
"strconv"
"strings"
"time"
"go.bug.st/serial"
)
type PSU struct {
port serial.Port
}
type IDF struct { // KIPRIM,<model, eg.DC310S>,<serial no.>,FV:Vx.x.x
Brand string
Model string
Serial string
Version string
}
const (
GetID = "*IDN?"
GetVoltage = "MEAS:VOLT?"
GetCurrent = "MEAS:CURR?"
GetPower = "MEAS:POW?"
GetSetpointVoltage = "VOLT?"
GetSetpointCurrent = "CURR?"
GetLimitVoltage = "VOLT:LIM?"
GetLimitCurrent = "CURR:LIM?"
SetVoltage = "VOLT %.3f"
SetCurrent = "CURR %.3f"
SetLimitVoltage = "VOLT:LIM %.3f"
SetLimitCurrent = "CURR:LIM %.3f"
GetOutput = "OUTP?"
SetOutput = "OUTP %s"
)
var setToGet = map[string]string{
SetOutput: GetOutput,
SetCurrent: GetSetpointCurrent,
SetVoltage: GetSetpointVoltage,
SetLimitVoltage: GetLimitVoltage,
SetLimitCurrent: GetLimitCurrent,
}
var outputStr = map[bool]string{
true: "ON",
false: "OFF",
}
func NewPSU(tty string) (*PSU, error) {
mode := &serial.Mode{
BaudRate: 115200,
DataBits: 8,
Parity: serial.NoParity,
StopBits: serial.OneStopBit,
}
port, err := serial.Open(tty, mode)
if err != nil {
return nil, err
}
port.SetReadTimeout(3 * time.Second)
return &PSU{
port: port,
}, nil
}
func (psu *PSU) Destroy() {
psu.port.Close()
psu = nil
}
func (psu *PSU) SetData(function string, value interface{}, ensure bool) error {
var err error
if function == SetOutput {
if vBool, ok := value.(bool); ok {
err = psu.sendCommand(function, outputStr[vBool])
} else {
return fmt.Errorf("invalid value %v for bool", value)
}
} else {
err = psu.sendCommand(function, value)
}
if ensure {
res, err := psu.GetData(setToGet[function])
if err != nil {
return err
}
if res != value {
fmt.Printf("%v != %v, retry\n", value, res)
psu.SetData(function, value, ensure) // retry
}
}
return err
}
func (psu *PSU) sendCommand(function string, value interface{}) error {
if psu == nil {
return fmt.Errorf("PSU not initialised")
}
psu.port.ResetInputBuffer() // Flush read buffer
var str string
if value != nil {
str = fmt.Sprintf("%s\n", fmt.Sprintf(function, value))
} else {
str = fmt.Sprintf("%s\n", function)
}
_, err := psu.port.Write([]byte(str))
if err != nil {
return fmt.Errorf("error write command [%s][%v]", function, value)
}
time.Sleep(500 * time.Millisecond) // Documentation says so...
return nil
}
func (psu *PSU) readReply() (string, error) {
res := ""
for {
buff := make([]byte, 32)
n, err := psu.port.Read(buff)
if err != nil {
return "", err
}
if n == 0 {
return "", fmt.Errorf("EOF")
}
res += string(buff[:n])
if buff[n-1] == '\n' { // Response end with /r/n
break
}
}
if len(res) < 2 {
return "", fmt.Errorf("invalid response size [%s]", res)
}
res = res[:len(res)-2] // Remove /r/n
if res == "ERR" {
return "", fmt.Errorf("ERR")
}
return res, nil
}
func (psu *PSU) GetData(function string) (interface{}, error) {
if psu == nil {
return nil, fmt.Errorf("psu not initialised")
}
err := psu.sendCommand(function, nil)
if err != nil {
return nil, err
}
res, err := psu.readReply()
if err != nil {
return nil, err
}
switch function {
case GetID:
spl := strings.Split(res, ",")
if len(spl) != 4 {
return nil, fmt.Errorf("invalid id")
}
if len(spl[3]) < 4 {
return nil, fmt.Errorf("invalid version")
}
return IDF{
Brand: spl[0],
Model: spl[1],
Serial: spl[2],
Version: spl[3][4:],
}, nil
case GetVoltage, GetCurrent, GetPower, GetSetpointCurrent, GetSetpointVoltage, GetLimitCurrent, GetLimitVoltage:
return strconv.ParseFloat(res, 64)
case GetOutput:
return res == "ON", nil
}
return nil, err
}