简体   繁体   English

使用python库检索OPC UA Space地址

[英]Retrieve OPC UA Space address using python library

I am trying to obtain my address space from my opc ua server.我正在尝试从我的 opc ua 服务器获取我的地址空间。 I tried using opcua-asyncio, using the simple-client.py script:我尝试使用 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()

but every time I try to use it I get the same error:但每次我尝试使用它时,都会遇到相同的错误:

File ".\\client.py", line 39, in asyncua.ua.uaerrors._auto.BadAttributeIdInvalid: The attribute is not supported for the specified Node.(BadAttributeIdInvalid)文件“.\\client.py”,第 39 行,在 asyncua.ua.uaerrors._auto.BadAttributeIdInvalid 中:指定节点不支持该属性。(BadAttributeIdInvalid)

Does anyone knows how to solve it?有谁知道如何解决它? Thank you so much非常感谢

It looks like you're trying to read the Value Attribute of every Node you encounter.看起来您正在尝试读取您遇到的每个节点的值属性。 Only Nodes of NodeClass Variable or VariableType have a Value Attribute.只有 NodeClass Variable 或 VariableType 的节点才有 Value 属性。

So the response from the server is entirely appropriate.所以来自服务器的响应是完全合适的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM