python - 了解 python 中的免费 OPC/UA 代码

标签 python opc

我正在使用 python 开发OPCUA。我正在使用freeopc 。我已经使用了他们的 server_minimal 和 client_minimal 示例,并且运行良好。我在理解代码时遇到一些问题。据我所知,OPCUA 堆栈的地址空间就像所有节点的集合。这些节点进一步包含对象,并且这些对象具有变量,我们可以从中读取写入数据。如果我错了,请纠正我。

---------------------------------
         Address space
---------------------------------
   |                    |
   |                    |
   V                    V
  Node1               Node2
   |
   Object1
    |
    Var1, Var2

所以在服务器端我想知道什么是命名空间

# setup our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)

命名空间的用途是什么? uri 里面要放什么?

在客户端,我想知道:

连接到服务器后,我们正在做:

    # 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()
    print("Objects node is: ", root)

get_root_node() 是什么意思。是不是就像我们正在连接到定义了所有节点的服务器的地址空间?

    # Node objects have methods to read and write node attributes as well as browse or populate address space
    print("Children of root are: ", root.get_children())

root.get_children()--这是否意味着获取节点的对象?

    # Now getting a variable node using its browse path
    myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
    obj = root.get_child(["0:Objects", "2:MyObject"])

root.get_child是什么意思?

客户端输出:

 ('Objects node is: ', Node(TwoByteNodeId(i=84)))
 ('Children of root are: ', [Node(NumericNodeId(i=85)), Node(NumericNodeId(i=86)), Node(NumericNodeId(i=87))])

以上代码取自server_minimal.py client_minimal.py

谁能解释一下这些。我尝试阅读他们的文档,但那里没有提到这一点。

谢谢。

最佳答案

我也在与 freeopcua 合作,对于一些问题我想我有答案

root = client.get_root_node()

将为您提供服务器根节点,因此基本上是图表中的“地址空间”。

root.get_children()

将返回作为根的直接子节点的所有节点的列表,在树的示例中也是如此。 [节点1,节点2]。但是添加根节点,这是 0:Objects, 0:Types, 0:Views

要查看服务器的树,您最好使用 opcua-client,这是一个可以让您查看树的 GUI。

为此,请启动您的服务器,然后在终端中输入;

 $ opcua-client

(在 Linux 上时)

您可以添加限制来获取 child ,例如:

        objects = root.get_children(refs=ua.ObjectIds.HasChild, nodeclassmask=ua.NodeClass.Object)

这只会返回其他对象,而不是节点的方法或属性。

您得到的输出是因为 Node 没有真正的“ToString()”,i 是节点的 id(也可以通过 GUI 客户端看到)。

Node.getChild(NodeId)

如果您确定添加了一个值,则将返回一个节点对象,您可以通过在返回时调用 .get_value() 来获取它的值。 NodeId 是您想要的子项的规范。所以说你想要 var1 这将是

# First the code needed to add the node
node1 = root.add_object(2, "Node1") # root is the root node which can be obtained by either client.get_root_node or server.get_root_node
object1 = node1.add_object(3, "Object1")
object1.add_variable(4, "Var1", 42)
object1.add_variable(4, "Var2", 13)
# Now the code to ask the server for the node
var1_node = root.getChild(["2:Node1", "3:Object1", "4:Var1"])

# And to get its value
var1_node.get_value()

这里重要的是,要获取子项,您需要知道自己在哪里(您可以从任何 Node 对象获取子项,而不仅仅是根),然后使用“idx:Name”的组合,这是您在以下情况下添加的内容:您首先将值添加到服务器。

希望这会有所帮助(没有测试代码,因此可能需要一些调整才能实际运行)

关于python - 了解 python 中的免费 OPC/UA 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326769/

相关文章:

python - 如何从 xml tspan 标签中检索信息

c++ - 如何实现协议(protocol)适配器

java - OPC DA/DCOM示例java源代码,可以从服务器接收标签

PLC 未连接到 KEP 服务器 (OPC)

Python 多处理提前终止

python - “staticmethod”对象不可调用

python - 电报机器人返回空

具有奇怪输出错误的 Python 线程

iot - 如何使用python通过https连接opcua服务器,即opc.https

c# - 西门子 OPC UA 和 .NET C# 客户端无法连接到服务器?