forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
method.go
52 lines (44 loc) · 1.21 KB
/
method.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
// 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/mvandergrift/opcua"
"github.com/mvandergrift/opcua/debug"
"github.com/mvandergrift/opcua/ua"
)
func main() {
var (
endpoint = flag.String("endpoint", "opc.tcp://localhost:4840", "OPC UA Endpoint URL")
)
flag.BoolVar(&debug.Enable, "debug", false, "enable debug logging")
flag.Parse()
log.SetFlags(0)
ctx := context.Background()
c, err := opcua.NewClient(*endpoint, opcua.SecurityMode(ua.MessageSecurityModeNone))
if err != nil {
log.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close(ctx)
in := int64(12)
req := &ua.CallMethodRequest{
ObjectID: ua.NewStringNodeID(2, "main"),
MethodID: ua.NewStringNodeID(2, "even"),
InputArguments: []*ua.Variant{ua.MustVariant(in)},
}
resp, err := c.Call(ctx, req)
if err != nil {
log.Fatal(err)
}
if got, want := resp.StatusCode, ua.StatusOK; got != want {
log.Fatalf("got status %v want %v", got, want)
}
out := resp.OutputArguments[0].Value()
log.Printf("%d is even: %v", in, out)
}