12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/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 UA服务器的URL
- redis_ip = '127.0.0.1'
- def get_point_arr():
- for i in range(137):
- three_digit_numbers = [f"OPC.电液控.支架集.{num:03d}.立柱压力.前柱压力" for num in range(1, 138)]
- return three_digit_numbers
- # 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()
- # redis
- pool = redis.ConnectionPool(host=redis_ip, password='')
- redis_conn = redis.Redis(connection_pool=pool)
- 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'))
- for i in range(len(opc_point_arr)):
- try:
- node = f"ns={ns};s={opc_point_arr[i]}"
- tag = client.get_node(node)
- value = await tag.read_value()
- # redis值
- redis_conn.set(str(opc_point_arr[i]), str(value))
- arr_key = node.split("=")
- result_arr[arr_key[2]] = value
- finally:
- continue
- return result_arr
- if __name__ == '__main__':
- print(get_opc_data())
|