-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrnd320.go
238 lines (209 loc) · 4.53 KB
/
rnd320.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
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
package rnd
import (
"errors"
"fmt"
"os"
"strconv"
"syscall"
"time"
"github.com/rumpelsepp/opennetzteil"
)
type RND320 struct {
opennetzteil.NetzteilBase
path string
file *os.File
}
const (
ChannelModeCC = 1 << iota
ChannelModeVC
)
type Status struct {
ChannelMode string
Output bool
}
func NewRND320(path, name string) (*RND320, error) {
file, err := os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
return nil, err
}
return &RND320{
NetzteilBase: opennetzteil.NetzteilBase{Name: name},
file: file,
path: path,
}, nil
}
func (nt *RND320) reopenHandeIfNeeded(err error) error {
// This happens when the power supply itself is
// powercycled. In this case the handle must be renewed.
if errors.Is(err, syscall.EIO) {
file, err := os.OpenFile(nt.path, os.O_RDWR, 0644)
if err != nil {
// The filedescriptor could not be refreshed.
// This is a fatal error.
return err
}
nt.file = file
}
return nil
}
func (nt *RND320) request(cmd string, timeout time.Duration) ([]byte, error) {
var (
err error
resp []byte
)
for i := 0; i < 3; i++ {
resp, err = nt.RequestWithTimeout(nt.file, []byte(cmd), timeout)
if err != nil {
if err := nt.reopenHandeIfNeeded(err); err != nil {
return nil, err
}
// This powersupply is crap, retry 3 times.
time.Sleep(500 * time.Millisecond)
continue
}
break
}
return resp, err
}
func (nt *RND320) command(cmd string) error {
var err error
for i := 0; i < 3; i++ {
err = nt.SendCommand(nt.file, []byte(cmd))
if err != nil {
if err := nt.reopenHandeIfNeeded(err); err != nil {
return err
}
// This powersupply is crap, retry 3 times.
time.Sleep(500 * time.Millisecond)
continue
}
break
}
return err
}
func (nt *RND320) Probe() error {
cmd := "*IDN?"
resp, err := nt.request(cmd, 1000*time.Millisecond)
if err != nil {
return err
}
nt.NetzteilBase.Ident = string(resp)
return nil
}
func (nt *RND320) Status() (interface{}, error) {
cmd := "STATUS?"
resp, err := nt.request(cmd, 100*time.Millisecond)
if err != nil {
return nil, err
}
if len(resp) != 1 {
fmt.Println(resp)
return nil, fmt.Errorf("invalid data from device received")
}
var (
mode string
output bool
)
if resp[0]&0x01 == 1 {
mode = "CV"
} else {
mode = "CC"
}
if resp[0]&0x40 == 1 {
output = true
} else {
output = false
}
status := Status{
ChannelMode: mode,
Output: output,
}
return status, nil
}
func (nt *RND320) GetMaster() (bool, error) {
status, err := nt.Status()
if err != nil {
return false, err
}
s := status.(Status)
return s.Output, nil
}
func (nt *RND320) SetMaster(enabled bool) error {
var cmd string
if enabled {
cmd = "OUT1"
} else {
cmd = "OUT0"
}
if err := nt.command(cmd); err != nil {
return err
}
return nil
}
func (nt *RND320) SetBeep(enabled bool) error {
return opennetzteil.ErrNotImplemented
}
func (nt *RND320) GetChannels() (int, error) {
return 1, nil
}
func (nt *RND320) GetCurrent(channel int) (float64, error) {
cmd := fmt.Sprintf("IOUT%d?", channel)
resp, err := nt.request(cmd, 100*time.Millisecond)
if err != nil {
return 0, err
}
current, err := strconv.ParseFloat(string(resp), 32)
if err != nil {
return 0, err
}
return current, nil
}
func (nt *RND320) SetCurrent(channel int, current float64) error {
cmd := fmt.Sprintf("ISET%d:%.2f", channel, current)
err := nt.command(cmd)
if err != nil {
return err
}
return nil
}
func (nt *RND320) GetVoltage(channel int) (float64, error) {
cmd := fmt.Sprintf("VOUT%d?", channel)
resp, err := nt.request(cmd, 100*time.Millisecond)
if err != nil {
return 0, err
}
voltage, err := strconv.ParseFloat(string(resp), 32)
if err != nil {
return 0, err
}
return voltage, nil
}
func (nt *RND320) SetVoltage(channel int, voltage float64) error {
cmd := fmt.Sprintf("VSET%d:%.2f", channel, voltage)
err := nt.command(cmd)
if err != nil {
return err
}
return nil
}
func (nt *RND320) GetOut(channel int) (bool, error) {
return nt.GetMaster()
}
func (nt *RND320) SetOut(channel int, enabled bool) error {
if channel > 1 {
return fmt.Errorf("channel not avail")
}
return nt.SetMaster(enabled)
}
func (nt *RND320) GetOCP(channel int) (bool, error) {
return false, opennetzteil.ErrNotImplemented
}
func (nt *RND320) SetOCP(channel int, enabled bool) error {
return opennetzteil.ErrNotImplemented
}
func (nt *RND320) GetOVP(channel int) (bool, error) {
return false, opennetzteil.ErrNotImplemented
}
func (nt *RND320) SetOVP(channel int, enabled bool) error {
return opennetzteil.ErrNotImplemented
}