-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver_iochannel.go
106 lines (95 loc) · 3.08 KB
/
server_iochannel.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
package gologix
import (
"bytes"
"errors"
"fmt"
"sync"
)
// this type satisfies the TagChannelProvider 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 and sent to all destination channels.
//
// get a new destintaion channel by calling GetOutputData. You can then receive from this to get data as it comes in.
//
// update the input data with the SetInputData(Tin) function
//
// it does not handle class 3 tag reads or writes.
type IOChannelProvider[Tin, Tout any] struct {
inMutex sync.Mutex
in Tin
outChannels []chan Tout
}
// this gets called with the IO setup forward open as the items
func (p *IOChannelProvider[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 *IOChannelProvider[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)
var out Tout
_, err = Unpack(b, &out)
if err != nil {
return fmt.Errorf("problem unpacking data into output struct %w", err)
}
for i := range p.outChannels {
select {
case p.outChannels[i] <- out:
default:
return errors.New("problem sending IOWrite data. channel full?")
}
}
return nil
}
func (p *IOChannelProvider[Tin, Tout]) TagRead(tag string, qty int16) (any, error) {
return 0, errors.New("not implemented")
}
func (p *IOChannelProvider[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 *IOChannelProvider[Tin, Tout]) GetOutputDataChannel() <-chan Tout {
newout := make(chan Tout)
p.outChannels = append(p.outChannels, newout)
return newout
}
// update the input data thread safely
// this input data is what the PLC receives
func (p *IOChannelProvider[Tin, Tout]) SetInputData(newin Tin) {
p.inMutex.Lock()
defer p.inMutex.Unlock()
p.in = newin
}