Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

说明:Client节点作用于和OPC-UA服务端进行通信。

1、准备工作

1-1、OPC-UA 服务端

模拟器名称:OPC UA Simulation Server 。

官方下载地址:

https://www.prosysopc.cn/products/opc-ua-simulation-server

备用下载:

prosys-opc-ua-simulation-server-2.3.2-146.zip

1-2、服务端软件使用

1、打开模拟器【Prosys OPC UA Simulation Server】,自动创建【Server】。

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

需要注意的是:

  • 运行状态,是否变为【Running】
  • 复制OPC连接地址【opc.tcp://windows10.microdone.cn:53530/OPCUA/SimulationServer】

注意这个地址改为自己对应的IP【opc.tcp://192.168.1.27:53530/OPCUA/SimulationServer】

2、关闭模拟器上动态数据。

需要注意的是:下图中,在标记2处,【取消勾选】;

作用:后续演示写入时,避免数据一直在动态更新,无法看到写入数据。

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2、OPC-UA 读写操作

2-1、示例代码

[{"id":"8b2c225641d92cdd","type":"debug","z":"d98cdac76eb3b270","name":"调试 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":240,"wires":[]},{"id":"74d584427517f13f","type":"inject","z":"d98cdac76eb3b270","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":240,"wires":[["dc03dcad80de2d8e"]]},{"id":"dc03dcad80de2d8e","type":"OpcUa-Item","z":"d98cdac76eb3b270","item":"ns=5;s=Random1","datatype":"Double","value":"","name":"","x":300,"y":240,"wires":[["1733786d4628d8eb"]]},{"id":"1733786d4628d8eb","type":"OpcUa-Client","z":"d98cdac76eb3b270","endpoint":"43413eccf75617cf","action":"read","deadbandtype":"a","deadbandvalue":1,"time":10,"timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","useTransport":false,"maxChunkCount":1,"maxMessageSize":8192,"receiveBufferSize":8192,"sendBufferSize":8192,"name":"","x":480,"y":240,"wires":[["8b2c225641d92cdd"],[]]},{"id":"aec5118faa8c6bdd","type":"comment","z":"d98cdac76eb3b270","name":"读取单个变量","info":"","x":150,"y":200,"wires":[]},{"id":"7f62a3f7b5fab914","type":"debug","z":"d98cdac76eb3b270","name":"调试 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":420,"wires":[]},{"id":"7b526545b841ea52","type":"OpcUa-Client","z":"d98cdac76eb3b270","endpoint":"43413eccf75617cf","action":"readmultiple","deadbandtype":"a","deadbandvalue":1,"time":10,"timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","useTransport":false,"maxChunkCount":1,"maxMessageSize":8192,"receiveBufferSize":8192,"sendBufferSize":8192,"name":"","x":500,"y":420,"wires":[["7f62a3f7b5fab914"],[]]},{"id":"f1d7a460e206820e","type":"comment","z":"d98cdac76eb3b270","name":"读取多个变量","info":"","x":150,"y":380,"wires":[]},{"id":"19d05e83fcd237a8","type":"debug","z":"d98cdac76eb3b270","name":"调试 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":620,"wires":[]},{"id":"7aec04cc2aaedbec","type":"inject","z":"d98cdac76eb3b270","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":620,"wires":[["5669170e7c9fdb6e"]]},{"id":"804f0b56f2e862c4","type":"OpcUa-Client","z":"d98cdac76eb3b270","endpoint":"43413eccf75617cf","action":"write","deadbandtype":"a","deadbandvalue":1,"time":10,"timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","useTransport":false,"maxChunkCount":1,"maxMessageSize":8192,"receiveBufferSize":8192,"sendBufferSize":8192,"name":"","x":520,"y":620,"wires":[["19d05e83fcd237a8"],[]]},{"id":"aa29d426cfa3ee3f","type":"comment","z":"d98cdac76eb3b270","name":"写入单个变量","info":"","x":150,"y":580,"wires":[]},{"id":"5669170e7c9fdb6e","type":"function","z":"d98cdac76eb3b270","name":"设置写入数据的参数及值","func":"msg.payload = 2.225;\nmsg.topic = \"ns=5;s=Random1;datatype=Double\";\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":620,"wires":[["804f0b56f2e862c4"]]},{"id":"d18e7b23e312b18a","type":"function","z":"d98cdac76eb3b270","name":"设置多个变量的标识符","func":"msg.payload = [\"ns=5;s=Random1\",\"ns=5;s=Square1\"];\nmsg.topic = \"readmultiple\";\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":420,"wires":[["7b526545b841ea52"]]},{"id":"94dbedfc623e1d94","type":"inject","z":"d98cdac76eb3b270","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":420,"wires":[["d18e7b23e312b18a"]]},{"id":"d9b2498c41005549","type":"comment","z":"d98cdac76eb3b270","name":"OPC-UA【客户端模式】读写操作","info":"","x":210,"y":100,"wires":[]},{"id":"d11364a456c96318","type":"comment","z":"d98cdac76eb3b270","name":"双击打开查看【官方使用文档】","info":"官方使用文档:\nhttps://yunteng.yuque.com/to2an3/rrd8yw/hw4lf5vcwxzv018d","x":210,"y":60,"wires":[]},{"id":"43413eccf75617cf","type":"OpcUa-Endpoint","endpoint":"opc.tcp://192.168.1.27:53530/OPCUA/SimulationServer","secpol":"None","secmode":"None","none":false,"login":false,"usercert":false,"usercertificate":"","userprivatekey":""}]

2-2、读取单个变量

2-2-1、先找到变量ID及值

NodeID 为【ns=5;s=Random1

Value 为【0.9978083145017194】

数据类型为【Double】

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-2-2、配置OPC-UA-Item

双击打开【OPC-UA-Item】节点,设置NodeID和数据类型。

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-2-3、配置OPC-UA-Client

双击打开【OPC-UA-Client】节点,进行服务端配置。

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

将前面服务端自动生成的地址,拷贝过来

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

最后,执行动作,设置为【READ】。

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-2-4、执行测试,查看打印结果

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-3、读取多个变量

读取NodeID为【ns=5;s=Random1】和【ns=5;s=Square1】

2-3-1、设置function多个变量

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-3-2、配置OPC-UA-Client动作为【READ MULTIPLE】

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-3-3、执行测试,查看打印结果

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)
Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-4、写入单个变量

2-4-1、为function配置写入内容

设置NodeID为【ns=5;s=Random1】的写入值及数据类型。

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-4-2、配置OPC-UA-Client动作为WRITE

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)

2-4-3、执行测试,查看打印结果

Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)
Node-RED软网关使用手册-最佳实践(OPC-UA【Client】读写—示例)