q 1 năm trước cách đây
mục cha
commit
5a3a7e9889
1 tập tin đã thay đổi với 17 bổ sung4 xóa
  1. 17 4
      jjq_support_1.py

+ 17 - 4
jjq_support_1.py

@@ -1,10 +1,11 @@
 #!/usr/bin/env python3
 import redis
+import asyncio
 
 from asyncua import Client
 from datetime import datetime
 
-opc_server_url = "opc.tcp://192.168.10.133:49320"
+opc_server_url = "opc.tcp://192.168.10.133:49320"  # OPC UA服务器的URL
 redis_ip = '127.0.0.1'
 
 
@@ -14,7 +15,19 @@ def get_point_arr():
     return three_digit_numbers
 
 
-def get_opc_data():
+# async def connect_and_read_opcua_data():
+#
+#     async with Client(url=opc_server_url) as client:
+#         # 连接到服务器
+#         await client.connect()
+#
+#         # 读取节点的值
+#         node = await client.nodes.root.get_child(get_point_arr())  # 替换成你要读取的节点路径
+#         value = await node.read_value()
+#
+#         print("Node Value:", value)
+
+async def get_opc_data():
     ns = 2
     opc_point_arr = get_point_arr()
 
@@ -22,7 +35,7 @@ def get_opc_data():
     pool = redis.ConnectionPool(host=redis_ip, password='')
     redis_conn = redis.Redis(connection_pool=pool)
 
-    with Client(url=opc_server_url) as client:
+    async with Client(url=opc_server_url) as client:
         i = 0
         result_arr = {}
         print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
@@ -30,7 +43,7 @@ def get_opc_data():
             try:
                 node = f"ns={ns};s={opc_point_arr[i]}"
                 tag = client.get_node(node)
-                value = tag.read_value()
+                value = await tag.read_value()
                 # redis值
                 redis_conn.set(str(opc_point_arr[i]), str(value))
                 arr_key = node.split("=")