-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserverCoils.go
81 lines (65 loc) · 1.68 KB
/
serverCoils.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
package modbus
func (s *server) x01ReadCoils(mb Modbus, request *dataReader, response *dataBuilder) error {
addr, _ := request.word()
count, _ := request.word()
atomic := s.StartAtomic()
defer atomic.Complete()
coils, err := s.ReadCoils(atomic, addr, count)
if err != nil {
return err
}
// pack discretes in to bytes
response.bits(coils...)
return nil
}
func (s *server) xCoilsCommonWrite(atomic Atomic, addr int, values []bool) ([]bool, error) {
current, err := s.ReadCoils(atomic, addr, 1)
if err != nil {
return nil, err
}
replacement, err := s.updateCoils(s, atomic, addr, values, current)
if err != nil {
return nil, err
}
// Update the cache with the replacement values
err = s.WriteCoils(atomic, addr, replacement)
if err != nil {
return nil, err
}
return replacement, nil
}
// x05WriteSingleCoil(address uint16, value bool) (PDU, error)
func (s *server) x05WriteSingleCoil(mb Modbus, request *dataReader, response *dataBuilder) error {
addr, _ := request.word()
value, _ := request.word()
atomic := s.StartAtomic()
defer atomic.Complete()
repl, err := s.xCoilsCommonWrite(atomic, addr, []bool{value != 0})
if err != nil {
return err
}
ret := 0x0000
if repl[0] {
ret = 0xff00
}
response.word(addr)
response.word(ret)
return nil
}
func (s *server) x0fWriteCoils(mb Modbus, request *dataReader, response *dataBuilder) error {
addr, _ := request.word()
count, _ := request.word()
coils, err := request.bits(count)
if err != nil {
return err
}
atomic := s.StartAtomic()
defer atomic.Complete()
repl, err := s.xCoilsCommonWrite(atomic, addr, coils)
if err != nil {
return err
}
response.word(addr)
response.word(len(repl))
return nil
}