-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
92 lines (89 loc) · 2 KB
/
main.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
package main
import (
"log"
"time"
"github.com/huskar-t/opcda"
"github.com/huskar-t/opcda/com"
)
func main() {
com.Initialize()
defer com.Uninitialize()
host := "localhost"
progID := "Matrikon.OPC.Simulation.1"
tags := []string{
"Bucket Brigade.ArrayOfReal8",
"Bucket Brigade.ArrayOfString",
"Bucket Brigade.Boolean",
"Bucket Brigade.Int1",
"Bucket Brigade.Int2",
"Bucket Brigade.Int4",
"Bucket Brigade.Real4",
"Bucket Brigade.Real8",
"Bucket Brigade.String",
"Bucket Brigade.Time",
"Bucket Brigade.UInt1",
"Bucket Brigade.UInt2",
"Bucket Brigade.UInt4",
}
value := []interface{}{
[]float64{1.2, 2.3, 3.4},
[]string{"hello", "world"},
true,
int8(1),
int16(2),
int32(3),
float32(5.5777),
float64(6.6777777777),
"hello",
time.Now(),
uint8(7),
uint16(8),
uint32(9),
}
server, err := opcda.Connect(progID, host)
if err != nil {
log.Fatalf("connect to opc server failed: %s\n", err)
}
defer server.Disconnect()
groups := server.GetOPCGroups()
group, err := groups.Add("group1")
if err != nil {
log.Fatalf("add group failed: %s\n", err)
}
items := group.OPCItems()
itemList, errs, err := items.AddItems(tags)
if err != nil {
log.Fatalf("add items failed: %s\n", err)
}
for i, err := range errs {
if err != nil {
log.Fatalf("add item %s failed: %s\n", tags[i], err)
}
}
// Wait for the OPC server to be ready
time.Sleep(time.Second)
// group sync write
log.Println("group sync write")
serverHandles := make([]uint32, len(itemList))
for i, item := range itemList {
serverHandles[i] = item.GetServerHandle()
}
errs, err = group.SyncWrite(serverHandles, value)
if err != nil {
log.Fatalf("sync write failed: %s\n", err)
}
for i, err := range errs {
if err != nil {
log.Fatalf("sync write item %s failed: %s\n", tags[i], err)
}
}
// item write
log.Println("item write")
for i, item := range itemList {
err := item.Write(value[i])
if err != nil {
log.Fatalf("write item %s failed: %s\n", tags[i], err)
}
}
log.Println("finish")
}