我是靠谱客的博主 忧伤钢笔,这篇文章主要介绍opcua-python学习踩坑总结前言OPCUA通信协议介绍OPCUA的两种工作方式OPCUA-python是什么?服务器常用函数客户端常用函数 示例程序:温度实时检测,每隔五秒传到客户端,现在分享给大家,希望可以做个参考。

前言

之所以一直没写博文,大概是一直比较忙。

接了一些额外的活,做了几个视频放到了其他平台上,也通过做工程训练的车弥补了自己51和stm32,esp,还有定时器,i2c总线复用等等的知识,虽然工训是延期了,不过假期还是要继续做车,相关的博文也是要补上的

最近其实也系统的学了一下机器学习和深度学习的知识,写出了一篇应用在嵌入式设备上的深度学习目标检测论文,之后也会总结相关的知识,并尽量通俗易懂的解释出来。

那就先从opcua开始吧~~~

OPCUA通信协议介绍

OPC UA(OPC 统一架构)是一种用于工业控制系统的通信协议。它是一种开放的、平台无关的协议,可用于连接各种工业设备和软件应用程序,使它们能够互相通信并共享数据。

OPC UA 的主要优点在于,它能够提供一种统一的方式来连接不同厂商的设备和应用程序。这意味着,使用 OPC UA 的软件应用程序可以与使用其他协议的设备和应用程序进行通信,而无需考虑底层的通信协议。 

其实这类似于物联网中的mqtt协议,只不过mqtt较为轻量级,opcua则是用于工业相关的控制系统

OPCUA的两种工作方式

  1. 客户端/服务器模式:在这种模式下,OPC UA 客户端向 OPC UA 服务器请求数据,服务器接收请求并向客户端发送数据。

  2. 发布/订阅模式:在这种模式下,OPC UA 服务器将数据“发布”到网络上,而客户端可以“订阅”特定的数据。当服务器更新数据时,客户端会收到更新的数据。

这两种模式可以根据应用场景的需要进行选择。例如,客户端/服务器模式适用于客户端需要向服务器请求单个数据项的情况,而发布/订阅模式则适用于客户端需要定期接收大量数据的情况。

个人觉得客户端/服务器模式类似于TCP通讯协议,是点对点的,对实时性要求比较高,而发布订阅模式类似于UDP通讯协议,对实时性要求并没有那么高,但接收数据较多

其中发布订阅模式对小的嵌入式设备(stm32系列)比较友好,可以融合物联网的mqtt协议,真正将opcua纳入网络,而不只是自己的工业网络

OPCUA-python是什么?

顾名思义,那就是利用python实现这种通信协议的库呗,这个库目前已经停止更新,开发者们开发了一个新的库,名为opcua-asyncio,可通过如下链接访问:

https://github.com/FreeOpcUa/opcua-asyncio

不过做一些比较基本的功能,只用OPCUA-python这个库还是够用的

服务器常用函数

复制代码
1
2
endpoint = "opc.tcp://{}:{}".format(url, port) myserver.set_endpoint(endpoint)

设置endpoint(其实endpoint就像一个服务器链接)

复制代码
1
myserver = opcua.Server()

开启opcua服务器

复制代码
1
objects = myserver.get_objects_node()

设置服务器节点(使客户端得以访问)

复制代码
1
addspace = myserver.register_namespace(name)

设置服务器命名空间

复制代码
1
root = myserver.get_root_node()

获取服务器根节点

复制代码
1
objects.add_method(1, "Conveyor", Start_Conveyor_prog)

为节点(objects) 添加方法Start_Conveyor_prog

客户端常用函数 

复制代码
1
myclient = Client(endpoint)

在客户端引用endpoint

复制代码
1
myclient.get_node("ns=2;i=3")

在客户端里使用ns=2;i=3这个节点

复制代码
1
Start_Lathe_Prog1 = method[4]

调用服务器里设置的方法

复制代码
1
Workpiece = objects_node.call_method(Start_Conveyor_prog,[WorkpieceID.get_value()])

 call_method函数,第一个参数是节点名称,第二个参数是值,是这个节点里的值,属于数组,或者传递字典也是可以的

复制代码
1
return_value_kuka_prog1 = objects_node.call_method(Start_Kuka_Prog1)

返回某个方法

示例程序:温度实时检测,每隔五秒传到客户端

服务器程序:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import opcua from time import sleep from random import randint from datetime import datetime # Assign endpoint URL url = "localhost" port = 5001 endpoint = "opc.tcp://{}:{}".format(url, port) # create instance/object of type opcua.Server myserver = opcua.Server() # Assign endpoint url on the OPC UA server address space myserver.set_endpoint(endpoint) # Create a name for OPC UA namespace name = "Temperature Sensor" # Register the name on OPC UA namespace addspace = myserver.register_namespace(name) # Get reference to the Objects node of the OPC UA server objects = myserver.get_objects_node() # Create objects on the object node param = objects.add_object(addspace, "parameters") # Create variables Sensor_name = param.add_variable(addspace, "Sensor Name", "Temperature_Sensor_SF12") Temperature = param.add_variable(addspace, "Temperature Value", 'NA') # Set variable as writable Sensor_name.set_writable() Temperature.set_writable() ######################################################################################### # Present the data structure of the OPC UA server root = myserver.get_root_node() print("Root Node Id: %s" % root) print("Children of root are: %s" % root.get_children()) print() my_objects = myserver.get_objects_node() print("Defined object Node Id: %s" % my_objects) print("Children of the defined object are: %s" % my_objects.get_children()) print() sensor_name_node = my_objects.get_children()[1].get_children()[0] print("Sensor name node Id: %s" % sensor_name_node) print("Sensor name node browse name: %s" % sensor_name_node.get_browse_name()) print("Sensor name default value: %s" % sensor_name_node.get_value()) print() temperature_node = my_objects.get_children()[1].get_children()[1] print("Temperature node Id: %s" % temperature_node) print("Temperature node browse name: %s" % temperature_node.get_browse_name()) print("Temperature default value: %s" % temperature_node.get_value()) print() ######################################################################################## # starting! The server will continue running myserver.start() current_time = str(datetime.now().time())[:-7] print("{} - Server is initialising...".format(current_time)) while True: sleep(5) current_time = str(datetime.now().time())[:-7] current_temp = randint(10, 25) Temperature.set_value(current_temp) # publish temperature value print("{} - Current temperature: {} Celsius".format(current_time, current_temp))

 客户端程序:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from opcua import Client from datetime import datetime # Assign endpoint URL url = "localhost" port = 5001 endpoint = "opc.tcp://{}:{}".format(url, port) # Assign endpoint url on the OPC UA client address space myclient = Client(endpoint) # Connect to server myclient.connect() # Assign nodes Temperature_node = myclient.get_node("ns=2;i=3") # Subhandler Class from OPC UA class SubHandler(object): """ Subscription Handler. To receive events from server for a subscription """ def datachange_notification(self, node, val, data): """ called for every datachange notification from server """ global DataChange # Global variable current_time = str(datetime.now().time())[:-7] DataChange = val # Assigning value to global variable print("{} - Received temperature: {} Celsius".format(current_time, val)) # Initailise variable nodes = myclient.get_objects_node() DataChange = "null" # Create Sub handler handler = SubHandler() # create subscription by passing period in milliseconds and handler subscribe = myclient.create_subscription(0, handler) # pass variable node to subscribe data change method handler = subscribe.subscribe_data_change(Temperature_node)

程序运行效果:服务器

 程序运行效果:客户端

最后

以上就是忧伤钢笔最近收集整理的关于opcua-python学习踩坑总结前言OPCUA通信协议介绍OPCUA的两种工作方式OPCUA-python是什么?服务器常用函数客户端常用函数 示例程序:温度实时检测,每隔五秒传到客户端的全部内容,更多相关opcua-python学习踩坑总结前言OPCUA通信协议介绍OPCUA内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部