python OPCUA服务器和客户端开发

  1. 环境准备
    安装开源的OPCUA包,安装采用pip安装。
pip install opcua
  1. 服务器端程序开发
    主要需要修改的内容为发布的网络地址:
    127.0.0.1表示发布为本机地址。

import sys
sys.path.insert(0, "..")

import time

from opcua import ua, Server

if __name__ == "__main__":

    # setup our server
    server = Server()
    server.set_endpoint("opc.tcp://127.0.0.1:4840/freeopcua/server/")

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

    # get Objects node, this is where we should put our nodes
    objects = server.get_objects_node()

    # populating our address space
    myobj = objects.add_object(idx, "MyObject")
    myvar = myobj.add_variable(idx, "MyVariable", 6.7)
    myvar.set_writable()    # Set MyVariable to be writable by clients

    # starting!
    server.start()
    
    try:
        count = 0
        while True:
            time.sleep(1)
            count += 0.1
            myvar.set_value(count)
    finally:
        #close connection, remove subcsriptions, etc
        server.stop()
  1. 客户端程序开发
import sys,time
sys.path.insert(0, "..")
from opcua import Client

if __name__ == "__main__":

 

    client = Client("opc.tcp://localhost:4840/freeopcua/server/")
    #client = Client("opc.tcp://127.0.0.1:4840/freeopcua/server/")
    #client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
    try:
        res = client.connect()

        # 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)

        # 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())

        # get a specific node knowing its node id
        #var = client.get_node(ua.NodeId(1002, 2))
        #var = client.get_node("ns=3;i=2002")
        #print(var)
        #var.get_data_value() # get value of node as a DataValue object
        #var.get_value() # get value of node as a python builtin
        #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
        #var.set_value(3.9) # set node value using implicit data type

        # Now getting a variable node using its browse path
        while(1):
            myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
            obj = root.get_child(["0:Objects", "2:MyObject"])
            print("myvar is: ", myvar)
            print("myobj is: ", obj)
            
            # Stacked myvar access
            print("myvar is: ", root.get_children()[0].get_children()[1].get_variables()[0].get_value())
            time.sleep(2)

    finally:
        client.disconnect()

client监听数据
引用信息来源:

https://blog.csdn.net/weixin_29482793/article/details/80537862

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>