|
@@ -1,10 +1,11 @@
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
import redis
|
|
import redis
|
|
|
|
+import asyncio
|
|
|
|
|
|
from asyncua import Client
|
|
from asyncua import Client
|
|
from datetime import datetime
|
|
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'
|
|
redis_ip = '127.0.0.1'
|
|
|
|
|
|
|
|
|
|
@@ -14,7 +15,19 @@ def get_point_arr():
|
|
return three_digit_numbers
|
|
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
|
|
ns = 2
|
|
opc_point_arr = get_point_arr()
|
|
opc_point_arr = get_point_arr()
|
|
|
|
|
|
@@ -22,7 +35,7 @@ def get_opc_data():
|
|
pool = redis.ConnectionPool(host=redis_ip, password='')
|
|
pool = redis.ConnectionPool(host=redis_ip, password='')
|
|
redis_conn = redis.Redis(connection_pool=pool)
|
|
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
|
|
i = 0
|
|
result_arr = {}
|
|
result_arr = {}
|
|
print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
|
|
print(datetime.now().strftime('Start_time:%Y-%m-%d %H:%M:%S.%f'))
|
|
@@ -30,7 +43,7 @@ def get_opc_data():
|
|
try:
|
|
try:
|
|
node = f"ns={ns};s={opc_point_arr[i]}"
|
|
node = f"ns={ns};s={opc_point_arr[i]}"
|
|
tag = client.get_node(node)
|
|
tag = client.get_node(node)
|
|
- value = tag.read_value()
|
|
|
|
|
|
+ value = await tag.read_value()
|
|
# redis值
|
|
# redis值
|
|
redis_conn.set(str(opc_point_arr[i]), str(value))
|
|
redis_conn.set(str(opc_point_arr[i]), str(value))
|
|
arr_key = node.split("=")
|
|
arr_key = node.split("=")
|