-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import logging | ||
import os | ||
import sys | ||
import argparse | ||
from opcua import ua, Client | ||
|
||
|
||
def add_common_args(parser, nodeid_required=True): | ||
parser.add_argument("-u", | ||
"--url", | ||
help="URL of OPC UA server (for example: opc.tcp://example.org:4840)", | ||
default='opc.tcp://localhost:4841', | ||
metavar="URL") | ||
parser.add_argument("-n", | ||
"--nodeid", | ||
help="Fully-qualified node ID (for example: i=85). Default: root node", | ||
default='i=84', | ||
required=nodeid_required, | ||
#required=True, | ||
metavar="NODE") | ||
parser.add_argument("-p", | ||
"--path", | ||
help="Comma separated browse path to the node starting at nodeid (for example: 3:Mybject,3:MyVariable)", | ||
default='', | ||
metavar="BROWSEPATH") | ||
parser.add_argument("-i", | ||
"--namespace", | ||
help="Default namespace", | ||
type=int, | ||
default=0, | ||
metavar="NAMESPACE") | ||
|
||
parser.add_argument("-v", | ||
"--verbose", | ||
dest="loglevel", | ||
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], | ||
default='WARNING', | ||
help="Set log level") | ||
|
||
|
||
def get_node(client, args): | ||
node = client.get_node(args.nodeid) | ||
if args.path: | ||
node = node.get_child(args.path.split(",")) | ||
|
||
|
||
def uaread(): | ||
parser = argparse.ArgumentParser(description="Read attribute of a node") | ||
add_common_args(parser) | ||
parser.add_argument("-a", | ||
"--attribute", | ||
dest="attribute", | ||
type=int, | ||
#default="VALUE", | ||
#choices=['VALUE', 'NODEID', 'BROWSENAME', 'ERROR', 'CRITICAL'], | ||
default=ua.AttributeIds.Value, | ||
help="Set attribute to read") | ||
parser.add_argument("-t", | ||
"--datatype", | ||
dest="datatype", | ||
default="python", | ||
choices=['python', 'variant', 'datavalue'], | ||
help="Data type to return") | ||
|
||
args = parser.parse_args() | ||
logging.basicConfig(format="%(levelname)s: %(message)s", level=getattr(logging, args.loglevel)) | ||
|
||
client = Client(args.url) | ||
client.connect() | ||
try: | ||
node = client.get_node(args.nodeid) | ||
if args.path: | ||
node = node.get_child(args.path.split(",")) | ||
attr = node.get_attribute(args.attribute) | ||
if args.datatype == "python": | ||
print(attr.Value.Value) | ||
elif args.datatype == "variant": | ||
print(attr.Value) | ||
else: | ||
print(attr) | ||
finally: | ||
client.disconnect() | ||
sys.exit(0) | ||
print(args) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import os | ||
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) | ||
|
||
from opcua.tools import uaread | ||
|
||
|
||
if __name__ == "__main__": | ||
uaread() |