forked from hootrhino/rulex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siemens_s1200_driver.go
130 lines (116 loc) · 2.63 KB
/
siemens_s1200_driver.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
123
124
125
126
127
128
129
130
package driver
import (
"encoding/json"
"github.com/i4de/rulex/common"
"github.com/i4de/rulex/glogger"
"github.com/i4de/rulex/typex"
"github.com/robinson/gos7"
)
/*
*
* 西门子s1200驱动
*
*/
type siemens_s1200_driver struct {
state typex.DriverState
s7client gos7.Client
device *typex.Device
RuleEngine typex.RuleX
dbs []common.S1200Block // PLC 的DB块
}
func NewS1200Driver(d *typex.Device,
e typex.RuleX,
s7client gos7.Client,
dbs []common.S1200Block) typex.XExternalDriver {
return &siemens_s1200_driver{
state: typex.DRIVER_STOP,
device: d,
RuleEngine: e,
s7client: s7client,
dbs: dbs,
}
}
func (s1200 *siemens_s1200_driver) Test() error {
_, err := s1200.s7client.GetCPUInfo()
if err != nil {
glogger.GLogger.Error(err)
return err
}
return nil
}
// 读取配置
func (s1200 *siemens_s1200_driver) Init(_ map[string]string) error {
return nil
}
func (s1200 *siemens_s1200_driver) Work() error {
return nil
}
func (s1200 *siemens_s1200_driver) State() typex.DriverState {
_, err := s1200.s7client.PLCGetStatus()
if err != nil {
glogger.GLogger.Error(err)
return typex.DRIVER_STOP
}
return typex.DRIVER_UP
}
// 字节格式:[dbNumber1, start1, size1, dbNumber2, start2, size2]
// 读: db --> dbNumber, start, size, buffer[]
func (s1200 *siemens_s1200_driver) Read(cmd []byte, data []byte) (int, error) {
values := []common.S1200BlockValue{}
for _, db := range s1200.dbs {
rData := []byte{}
if err := s1200.s7client.AGReadDB(db.Address, db.Start, db.Size, rData); err != nil {
return 0, err
}
values = append(values, common.S1200BlockValue{
Tag: db.Tag,
Address: db.Address,
Start: db.Start,
Size: db.Size,
Value: rData,
})
}
bytes, _ := json.Marshal(values)
copy(data, bytes)
return len(bytes), nil
}
// db.Address:int, db.Start:int, db.Size:int, rData[]
// [
//
// {
// "tag":"V",
// "address":1,
// "start":1,
// "size":1,
// "value":"AAECAwQ="
// }
//
// ]
func (s1200 *siemens_s1200_driver) Write(cmd []byte, data []byte) (int, error) {
blocks := []common.S1200BlockValue{}
if err := json.Unmarshal(data, &blocks); err != nil {
return 0, err
}
//
for _, block := range blocks {
if err := s1200.s7client.AGWriteDB(
block.Address,
block.Start,
block.Size,
block.Value,
); err != nil {
return 0, err
}
}
return 0, nil
}
func (s1200 *siemens_s1200_driver) DriverDetail() typex.DriverDetail {
return typex.DriverDetail{
Name: "SIEMENS_s1200",
Type: "TCP",
Description: "SIEMENS s1200 系列 PLC 驱动",
}
}
func (s1200 *siemens_s1200_driver) Stop() error {
return nil
}