-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcip.go
122 lines (102 loc) · 2.91 KB
/
cip.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
package go_cip
import (
"bytes"
"errors"
"fmt"
eip "github.com/loki-os/go-ethernet-ip"
"github.com/loki-os/go-ethernet-ip/typedef"
)
type Controller struct {
device *eip.Device
timeTicks typedef.Usint
timeoutTicks typedef.Usint
slot uint8
controller *eip.Device
}
func NewControllerFromClient(device *eip.Device, slot uint8) *Controller {
_client := &Controller{
device: device,
slot: slot,
timeTicks: 3,
timeoutTicks: 250,
}
return _client
}
func NewControllerFromIP(addr string, slot uint8, config *eip.Config) (*Controller, error) {
device, err := eip.NewDevice(addr)
if err != nil {
return nil, err
}
err2 := device.Connect(config)
if err2 != nil {
return nil, err2
}
_device := &Controller{
device: device,
slot: slot,
timeTicks: 3,
timeoutTicks: 250,
}
return _device, nil
}
func (c *Controller) SetTimeout(timeTicks typedef.Usint, timeoutTicks typedef.Usint) {
c.timeTicks = timeTicks
c.timeoutTicks = timeoutTicks
}
func (c *Controller) UCMM(mrr *eip.MessageRouterRequest) (*eip.SendDataSpecificData, error) {
ucs := UnConnectedSend{
TimeTick: c.timeTicks,
TimeOutTicks: c.timeoutTicks,
MessageRequest: mrr,
RouterPath: PortBuild([]byte{c.slot}, 1, true),
}
_mrr := &eip.MessageRouterRequest{}
_mrr.New(0x52, Paths(
LogicalBuild(LogicalTypeClassID, 06, true),
LogicalBuild(LogicalTypeInstanceID, 01, true),
), ucs.Encode())
cpf := &eip.CommonPacketFormat{}
cpf.New([]eip.CommonPacketFormatItem{
eip.CommonPacketFormatItem{
TypeID: eip.ItemIDUCMM,
Data: nil,
},
eip.CommonPacketFormatItem{
TypeID: eip.ItemIDUnconnectedMessage,
Data: _mrr.Encode(),
},
})
return c.device.SendRRData(cpf, 10)
}
func (c *Controller) GetAttributeAll() error {
paths := Paths(
LogicalBuild(LogicalTypeClassID, 01, true),
LogicalBuild(LogicalTypeInstanceID, 01, true),
)
mrreq := &eip.MessageRouterRequest{}
mrreq.New(0x01, paths, nil)
res, err := c.UCMM(mrreq)
if err != nil {
return err
}
mrres := &eip.MessageRouterResponse{}
mrres.Decode(res.Packet.Items[1].Data)
if mrres.GeneralStatus != 0 {
return errors.New(fmt.Sprintf("target error => Service Code: %#x | Status: %#x | Addtional: %s", mrres.ReplyService, mrres.GeneralStatus, mrres.AdditionalStatus))
}
dataReader := bytes.NewReader(mrres.ResponseData)
c.controller = &eip.Device{}
eip.ReadByte(dataReader, &c.controller.VendorID)
eip.ReadByte(dataReader, &c.controller.DeviceType)
eip.ReadByte(dataReader, &c.controller.ProductCode)
eip.ReadByte(dataReader, &c.controller.Major)
eip.ReadByte(dataReader, &c.controller.Minor)
eip.ReadByte(dataReader, &c.controller.Status)
eip.ReadByte(dataReader, &c.controller.SerialNumber)
nameLength := uint8(0)
eip.ReadByte(dataReader, &nameLength)
productName := make([]byte, nameLength)
eip.ReadByte(dataReader, &productName)
c.controller.ProductName = string(productName)
return nil
}