forked from danomagnum/gologix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_io.go
102 lines (90 loc) · 2.9 KB
/
server_io.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
package gologix
import (
"bytes"
"errors"
"fmt"
"sync"
)
// this type satisfies the TagProvider interface to provide class 1 IO support
// It has to be defined with an input and output struct that consist of only GoLogixTypes
// It will then serialize the input data and send it to the PLC at the requested rate.
// When the PLC sends an IO output message, that gets deserialized into the output structure.
//
// If you are going to access In or Out, be sure to lock the appropriate Mutex first to prevent data race.
// Remember that they are pointers here so the locks also need to apply to the original data that was pointed at.
//
// it does not handle class 3 tag reads or writes.
type IOProvider[Tin, Tout any] struct {
InMutex sync.Mutex
OutMutex sync.Mutex
In *Tin
Out *Tout
}
// this gets called with the IO setup forward open as the items
func (p *IOProvider[Tin, Tout]) IORead() ([]byte, error) {
p.InMutex.Lock()
defer p.InMutex.Unlock()
b := bytes.Buffer{}
_, err := Pack(&b, *(p.In))
if err != nil {
return nil, err
}
dat := b.Bytes()
return dat, nil
}
func (p *IOProvider[Tin, Tout]) IOWrite(items []CIPItem) error {
if len(items) != 2 {
return fmt.Errorf("expeted 2 items but got %v", len(items))
}
if items[1].Header.ID != cipItem_ConnectedData {
return fmt.Errorf("expected item 2 to be a connected data item but got %v", items[1].Header.ID)
}
var seq_counter uint32
// according to wireshark only the least significant 4 bits are used.
// 00.. ROO?
// ..0. COO?
// ...1 // Run/Idle (1 = run)
var header uint16
err := items[1].DeSerialize(&seq_counter)
if err != nil {
return fmt.Errorf("problem getting sequence counter. %w", err)
}
err = items[1].DeSerialize(&header)
if err != nil {
return fmt.Errorf("problem getting header. %w", err)
}
payload := make([]byte, items[1].Header.Length-6)
err = items[1].DeSerialize(&payload)
if err != nil {
return fmt.Errorf("problem getting raw data. %w", err)
}
b := bytes.NewBuffer(payload)
p.OutMutex.Lock()
defer p.OutMutex.Unlock()
_, err = Unpack(b, p.Out)
if err != nil {
return fmt.Errorf("problem unpacking data into output struct %w", err)
}
return nil
}
func (p *IOProvider[Tin, Tout]) TagRead(tag string, qty int16) (any, error) {
return 0, errors.New("not implemented")
}
func (p *IOProvider[Tin, Tout]) TagWrite(tag string, value any) error {
return errors.New("not implemented")
}
// returns the most udpated copy of the output data
// this output data is what the PLC is writing to us
func (p *IOProvider[Tin, Tout]) GetOutputData() Tout {
p.OutMutex.Lock()
defer p.OutMutex.Unlock()
t := *p.Out
return t
}
// update the input data thread safely
// this input data is what the PLC receives
func (p *IOProvider[Tin, Tout]) SetInputData(newin Tin) {
p.InMutex.Lock()
defer p.InMutex.Unlock()
p.In = &newin
}