forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
63 lines (53 loc) · 1.32 KB
/
write.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
// Copyright 2018-2020 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package main
import (
"context"
"flag"
"log"
"github.com/imatic-tech/opcua"
"github.com/imatic-tech/opcua/debug"
"github.com/imatic-tech/opcua/ua"
)
func main() {
var (
endpoint = flag.String("endpoint", "opc.tcp://localhost:4840", "OPC UA Endpoint URL")
nodeID = flag.String("node", "", "NodeID to read")
value = flag.String("value", "", "value")
)
flag.BoolVar(&debug.Enable, "debug", false, "enable debug logging")
flag.Parse()
log.SetFlags(0)
ctx := context.Background()
c := opcua.NewClient(*endpoint)
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseWithContext(ctx)
id, err := ua.ParseNodeID(*nodeID)
if err != nil {
log.Fatalf("invalid node id: %v", err)
}
v, err := ua.NewVariant(*value)
if err != nil {
log.Fatalf("invalid value: %v", err)
}
req := &ua.WriteRequest{
NodesToWrite: []*ua.WriteValue{
{
NodeID: id,
AttributeID: ua.AttributeIDValue,
Value: &ua.DataValue{
EncodingMask: ua.DataValueValue,
Value: v,
},
},
},
}
resp, err := c.WriteWithContext(ctx, req)
if err != nil {
log.Fatalf("Read failed: %s", err)
}
log.Printf("%v", resp.Results[0])
}