Python 通过OPC UA 连接PLC

        OPCUA 是一个重要的自动化控制协议,现在大多数主流PLC 都支持OPCUA 服务器。Python 是一种非常普及的程序设计语言。使用python 语言能够快速地实现产品或者软件算法的原型设计。而且python擅长做数值计算,比如,编写一个PID 算法,或者OpenCV 图像识别程序,是python 最擅长的。这些算法通过opcua 协议与PLC 连接十分方便。

   Opcua 是一种基于服务器和客户端架构的协议。使用Python能实现服务器和客户端。使用的插件是python-opcua。

安装方法:

pip install opcua

代码

import sys
sys.path.insert(0, "..")
import time
from opcua import  Server

if __name__ == "__main__":

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

    # setup our own namespace, not really necessary but should as spec
    uri = "http://examples.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()

运行

使用uaExpert 访问,url 设置为 opc.tcp://localhost:48400/freeopcua/server/

Python 通过OPC UA 连接PLC_第1张图片

 上述代码来自:https://github.com/FreeOpcUa/python-opcua/blob/master/examples/server-minimal.py

客户端

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


from opcua import Client


if __name__ == "__main__":

    client = Client("opc.tcp://localhost:48400/freeopcua/server/")
    # client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
    try:
        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
        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())

    finally:
        client.disconnect()

结束语

使用python 做算法原型,通过opcua 连接PLC 。十分方便。

你可能感兴趣的:(python,OPCUA,PLC)