使用python库检索OPC UA空间地址

问题描述 投票:0回答:1

我正在尝试从我的opc ua服务器获取地址空间。我尝试使用opcua-asyncio,使用simple-client.py脚本:

import asyncio
import sys
sys.path.insert(0, "..")
import logging
from asyncua import Client, Node, ua

logging.basicConfig(level=logging.INFO)
_logger = logging.getLogger('asyncua')


async def main():
    url = 'opc.tcp://serveraddress/Server'
    # url = 'opc.tcp://commsvr.com:port/UA/CAS_UA_Server'
    async with Client(url=url) as client:
        # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
        root = client.get_root_node()
        _logger.info('Objects node is: %r', root)

        # Node objects have methods to read and write node attributes as well as browse or populate address space
        _logger.info('Children of root are: %r', await root.get_children())

        uri = "http://opcfoundation.org/UA/"
        idx = await client.get_namespace_index(uri)
        print(idx)
        # get a specific node knowing its node id
        var = client.get_node(ua.NodeId(85, 0))
        #var = client.get_node('i=85')
        #var = await root.get_child("[0: Objects , 0:Types , 0:Views]")
        print("My variable", var, await var.read_value())
        # print(var)
        # var.get_data_value() # get value of node as a DataValue object
        # var.read_value() # get value of node as a python builtin
        # var.write_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
        # var.write_value(3.9) # set node value using implicit data type

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.set_debug(True)
    loop.run_until_complete(main())
    loop.close()

但是每次我尝试使用它时,都会出现相同的错误:

文件“。\ client.py”,第39行,在asyncua.ua.uaerrors._auto.BadAttributeIdInvalid:该属性是指定的节点不支持。(BadAttributeIdInvalid)

有人知道如何解决吗?非常感谢

python python-3.x iot opc-ua
1个回答
0
投票

似乎您正在尝试读取遇到的每个节点的值属性。只有NodeClass变量或VariableType的节点才具有值属性。

因此来自服务器的响应是完全适当的。

© www.soinside.com 2019 - 2024. All rights reserved.