-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclientInput.go
More file actions
52 lines (47 loc) · 1.22 KB
/
clientInput.go
File metadata and controls
52 lines (47 loc) · 1.22 KB
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
package modbus
import (
"fmt"
"strings"
"time"
)
// X04xReadInputs server response to a Read Multiple Inputs request
type X04xReadInputs struct {
Address int
Values []int
}
func (s X04xReadInputs) String() string {
cnt := len(s.Values)
txt := make([]string, cnt)
for i, v := range s.Values {
txt[i] = fmt.Sprintf(" 0x%04x: 0x%04x % 6d\n", s.Address+i, v, v)
}
return fmt.Sprintf("X04xReadInputs %05d -> %05d (count %v)\n", s.Address, s.Address+cnt-1, cnt) + strings.Join(txt, "")
}
func (c client) ReadInputs(from int, count int, tout time.Duration) (*X04xReadInputs, error) {
p := dataBuilder{}
p.word(from)
p.word(count)
tx := pdu{0x04, p.payload()}
ret := &X04xReadInputs{}
decode := func(r *dataReader) error {
l, err := r.byte()
if err != nil {
return err
}
if len(r.data) != l+1 {
return fmt.Errorf("Expect Read Inputs response to have correct internal length, %v not %v", len(r.data)-1, l)
}
if l != count*2 {
return fmt.Errorf("Expect Read Inputs response to have correct count of values, %v not %v", count, l/2)
}
v, err := r.words(count)
ret.Address = from
ret.Values = v
return nil
}
err := <-c.query(tout, tx, decode)
if err != nil {
return nil, err
}
return ret, nil
}