OPC UA学习心得 — 4 OPC编程实现,基于Python

OPC UA编程实现 — 基于Python

FreeOpcUa,是使用Python开发基于OPC统一架构的优选第三方库,项目链接:https://github.com/FreeOpcUa/python-opcua

创建一个OPC服务器的步骤非常简单:

from opcua import Server

server = Server()    # 实例化一个UA服务器
server.set_endpoint("opc.tcp://0.0.0.0:48400/freeopcua/server/")    # 设定服务器URI
server.start()    # 启动UA服务器

测试所创建的UA服务器,建议可以使用UaExpert,它可以实现UA客户端的功能,下载链接:https://www.unified-automation.com

  1. 软件打开后,右键“Servers”,添加新连接

添加连接

  1. 在“Custom Discovery”下双击,并填入上面代码中设定的服务器URI
    在这里插入图片描述
    上步添加完URI后,会扫描到Python中创建的UA服务器,选择“Anonymous”,建立连接
    在这里插入图片描述
  2. 连接成功后在主页面能够看到OPC UA规范定义的标准地址空间结构
    在这里插入图片描述
    对于如何通过FreeOpcUa,创建自己的地址空间,项目源代码Examples文件夹下的server-example.py文件进行了举例。这里要介绍的是如何通过XML文件来编辑地址空间。

在XML文件中创建节点和分配引用,是非常非常棒的一种方法。

编写XML文件,首先添加命名空间,主要包括节点类、数据类型以及W3C标准。

<?xml version="1.0" encoding="UTF-8" ?>
<UANodeSet xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd"
           xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</UANodeSet>

1. 创建对象节点

<UAObject NodeId="" BrowseName="" ParentNodeId="">
    <Description></Description>
    <DisplayName></DisplayName>
    <References>
        <Reference ReferenceType="" IsForward=""></Reference>
    </References>
</UAObject>
  1. 填写正确的NodeId,用于明确标识节点
  2. 填写BrowseName,作为浏览地址空间时的非本地化名称
  3. 填写ParentNodeId,在实例化时非常关键,与ModelParent相关,如果不对实例声明进行明确指定,会导致无法实例化
  4. 分配引用,需要注意每种引用可以使用的次数(在节点类学习心得中进行了总结中)

2. 创建变量节点

<UAVariable NodeId="" BrowseName="" DataType="" ParentNodeId="" AccessLevel="" UserAccessLevel="">
    <Description></Description>
    <DisplayName></DisplayName>
    <References>
        <Reference ReferenceType="" IsForward=""></Reference>
    </References>
    <Value></Value>
</UAVariable>
  1. AccessLevel,设定访问方式,可读可写或其他
  2. Value,定义节点Value属性

一个简单的例子

from opcua import Server

server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:48400/freeopcua/server/")    # 设定服务器URI
server.import_xml("custom_nodes.xml")    # 导入XML文件
server.start()

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<UANodeSet xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd"
			xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd"
			xmlns:xsd="http://www.w3.org/2001/XMLSchema"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <UAObject NodeId="i=30001" BrowseName="MyXMLFolder">
        <Description>A custom folder.</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=61</Reference>
            <Reference ReferenceType="Organizes" IsForward="false">i=85</Reference>
        </References>
    </UAObject>

    <UAObject NodeId="i=30002" BrowseName="MyXMLObject">
        <Description>A custom object node.</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=58</Reference>
            <Reference ReferenceType="Organizes" IsForward="false">i=30001</Reference>
        </References>
    </UAObject>

    <UAVariable NodeId="i=30004" BrowseName="MyXMLVariable" DataType="String">
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=63</Reference>
            <Reference ReferenceType="Organizes" IsForward="false">i=30002</Reference>
        </References>
        <Value><uax:String>StringValue</uax:String></Value>
    </UAVariable>

    <UAVariable NodeId="i=30005" BrowseName="MyXMLProperty" DataType="UInt32">
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=68</Reference>
            <Reference ReferenceType="HasProperty" IsForward="false">i=30002</Reference>
        </References>
        <Value><uax:UInt32>76</uax:UInt32></Value>
    </UAVariable>
</UANodeSet>
  1. 在上例中,首先添加了一个类型为文件夹的对象节点MyXMLFolder,通过HasTypeDefinition设定其类型为FolderType,用于组织地址空间中的节点,并使用Organizes引用将它组织到Root下的Objects节点下
  2. 然后在文件夹节点下添加了一个名为MyXMLObject的对象节点,通过HasTypeDefinition引用设定其类型为BaseObjectType,并使用Organizes引用将它组织到MyXMLFolder节点下
  3. 在MyXMLObject对象节点下创建一个变量MyXMLVariable,数据类型设定为字符串型,节点类型为BaseVariableType,设定Value属性来赋予初始值“StringValue”
  4. 在MyXMLObject对象节点下添加一个特性MyXMLProperty,需要使用HasProperty引用,节点类型设定为PropertyType,数据类型为32位的UInt,并赋予初始值76

在这里插入图片描述
一个复杂的例子
创建一个自定义的对象类型节点,然后通过实例化该类型节点得到一个复杂的对象节点。
复杂对象类型节点实例化

from opcua import Server

server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:48400/freeopcua/server/")    # 设定服务器URI
uri = 'http://examples.freeopcua.github.io'
idx = server.register_namespace(uri)    # 注册地址空间

server.import_xml("test.xml")    # 导入自定义的节点类型

my_sensor_type = server.get_root_node().get_child([
    "0:Types", "0:ObjectTypes", "0:BaseObjectType", "0:TemperatureSensorType"]).nodeid
my_sensor = server.nodes.objects.add_object(idx, "TemperatureSensor", my_sensor_type)

server.start()

在XML文件中创建自定义的类型定义节点MyTemperatureSensorType
类型定义节点
编程实例化该节点,得到地址空间结构如图:
地址空间结构
XML文件代码:

<?xml version="1.0" encoding="UTF-8" ?>
<UANodeSet xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd"
           xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <UAObjectType NodeId="i=30000" BrowseName="TemperatureSensorType">
        <DisplayName>MyTemperatureSensorType</DisplayName>
        <Description>温度传感器类型</Description>
        <References>
            <Reference ReferenceType="HasSubtype" IsForward="false">i=58</Reference>    <!-- BaseObjectType -->
        </References>
    </UAObjectType>

    <UAObject NodeId="i=30001" BrowseName="Configuration">
        <Description>配置</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=58</Reference>    <!-- BaseObjectType -->
            <Reference ReferenceType="HasModellingRule">i=78</Reference>    <!-- ModellingRule_Mandatory -->
            <Reference ReferenceType="HasComponent" IsForward="false">i=30000</Reference>
        </References>
    </UAObject>

    <UAVariable NodeId="i=30002" BrowseName="EngineeringUnit" ParentNodeId="i=30001" DataType="String">
        <DisplayName>EngineeringUnit</DisplayName>
        <Description>温度工程单位</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=63</Reference>    <!-- BaseDataVariableType -->
            <Reference ReferenceType="HasModellingRule">i=78</Reference>    <!-- ModellingRule_Mandatory -->
            <Reference ReferenceType="Organizes" IsForward="false">i=30001</Reference>
        </References>
        <Value><uax:String>℃</uax:String></Value>
    </UAVariable>

    <UAObject NodeId="i=30003" BrowseName="Measurement">
        <Description>温度测试</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=58</Reference>    <!-- BaseObjectType -->
            <Reference ReferenceType="HasModellingRule">i=78</Reference>    <!-- ModellingRule_Mandatory -->
            <Reference ReferenceType="HasComponent" IsForward="false">i=30000</Reference>
        </References>
    </UAObject>

    <UAVariable NodeId="i=30004" BrowseName="Temperature" ParentNodeId="i=30003" DataType="Float">
        <DisplayName>Temperature</DisplayName>
        <Description>温度</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=63</Reference>    <!-- BaseDataVariableType -->
            <Reference ReferenceType="HasModellingRule">i=78</Reference>    <!-- ModellingRule_Mandatory -->
            <Reference ReferenceType="Organizes" IsForward="false">i=30003</Reference>
        </References>
        <Value><uax:Float>0.0</uax:Float></Value>
    </UAVariable>

    <UAVariable NodeId="i=30005" BrowseName="EngineeringUnit" ParentNodeId="i=30004" DataType="String">
        <DisplayName>EngineeringUnit</DisplayName>
        <Description>温度工程单位</Description>
        <References>
            <Reference ReferenceType="HasTypeDefinition">i=68</Reference>    <!-- PropertyType -->
            <Reference ReferenceType="HasModellingRule">i=78</Reference>    <!-- ModellingRule_Mandatory -->
            <Reference ReferenceType="HasProperty" IsForward="false">i=30004</Reference>
        </References>
        <Value><uax:String>℃</uax:String></Value>
    </UAVariable>
</UANodeSet>
如有错误,欢迎指正 hbin6358@163.com
  • 25
    点赞
  • 95
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值