安装opc_ua服务
opcua_server安装
- 下载代码
git clone https://gitlab.com/latosai/opcua_server.git - 安装环境变量
python3 -m venv venv
source venv/bin/activate - 安装依赖
pip install -r requirements.txt - 运行服务
cd code
python3 server.py - 使用UaExpert连接测试
KEPServerEX安装
- KEPServerEX 下载
- 项目属性设置
服务器接口:
“启用”选“是” 将启用OPC UA服务器接口
“日志诊断”用于记录 OPC UA 连接及通讯信息(可选)
客户端会话:
“允许匿名登录”选“是” 此属性指定在建立连接时是否需要用户名及密码 - OPC UA配置管理器设置
选择使用的本机网卡,端口号,安全策略(本测试端口设置为49320,安全策 略全选,本服务器站点URL opc.tcp://192.168.1.32:49320) - 重启Kepware服务 停止Kepware服务,再启动Kepware服务,使OPC UA Server端设置生效
- 关闭防火墙
使用UaExpert测试服务端
下载
https://www.unified-automation.com/downloads/opc-ua-clients.html
安装配置
- 添加服务端
- 连接服务端
- 选择属性查看
至此,如果效果与图一致,则表明ocp ua 服务启动都正常,再使用程序语言进行下一步连接测试
程序测试连接
from queue import PriorityQueue
import time
from opcua import Client, ua
class SubHandler(object):
def datachange_notification(self, node, val, data):
print("Python: New data change event", node, val, data)
def get_text():
client = Client('opc.tcp://192.168.0.74:49320/', timeout=10)
# client.set_user('will')
# client.set_password('123456') # 需要在连接前调用
# client.set_security_string(r"Basic256,SignAndEncrypt,C:\Users\42082\Desktop\certificate-example.der,C:\Users\42082\Desktop\private-key-example.pem") # 只支持Basic128Rsa15, Basic256 or Basic256Sha256三种生成证书的方法
client.connect()
objects = client.get_objects_node()
print("objects:")
print(objects)
print("\n")
root = client.get_root_node()
print("children node:")
print(root.get_children()[0].get_children())
print("\n")
namespaces = client.get_namespace_array()
print("namespaces:")
print(namespaces)
print("\n")
endpoints = client.get_endpoints()
print("endpoints")
print(endpoints)
print("\n")
snode = client.get_server_node()
print("snode:")
print(snode)
print("\n")
try:
node = client.get_node('ns=2;s=通道 1.设备 1.标记 1')
print("\n")
print(node.get_value())
dv = ua.DataValue(ua.Variant(200.0, ua.VariantType.Float))
node.set_value(dv)
handler = SubHandler()
sub = client.create_subscription(500, handler)
sub.subscribe_data_change(node)
time.sleep(1000)
except Exception as e:
print(e)
finally:
client.disconnect()
if __name__ == "__main__":
get_text()
或
import asyncio
import logging
from asyncua import Client, ua
from future.moves import sys
class SubHandler(object):
def datachange_notification(self, node, val, data):
idNode = node.nodeid.Identifier.split('.', maxsplit=2)
idDevice = idNode[1]
tag = idNode[2]
print(idDevice, tag, val)
async def main():
url = "opc.tcp://192.168.0.74:49320"
# url = "opc.tcp://localhost:53530/OPCUA/SimulationServer/"
# url = "opc.tcp://olivier:olivierpass@localhost:53530/OPCUA/SimulationServer/"
async with Client(url,10) as client:
#print("Root children are", await client.nodes.root.get_children())
tag1 = client.get_node("ns=2;s=通道 1.设备 1.标记 1")
tag1Val = await tag1.read_value()
print(f"tag1:{tag1}, VALUE={tag1Val}, TYPE={type(tag1Val)}")
tag2 = client.get_node("ns=2;s=通道 1.设备 1.标记 2")
tag2Val = await tag2.read_value()
print(f"tag2:{tag2}, VALUE={tag2Val}, TYPE={type(tag2Val)}")
if __name__ == "__main__":
logging.basicConfig(level=logging.WARN)
asyncio.run(main())
gateway连接
tb_gateway.json配置
"connectors": [
{
"name": "OPC-UA Connector",
"type": "opcua",
"configuration": "opcua.json"
}
],
opcua.json配置
{
"server": {
"name": "OPC-UA Default Server",
"url": "192.168.0.74:49320",
"timeoutInMillis": 5000,
"scanPeriodInMillis": 5000,
"disableSubscriptions": false,
"subCheckPeriodInMillis": 100,
"showMap": false,
"security": "Basic128Rsa15",
"identity": {
"type": "anonymous"
},
"mapping": [
{
"deviceNodePattern": "Root\\.Objects\\.Device1",
"deviceNamePattern": "OPC001",
"attributes": [
{
"key": "temperature",
"path": "${ns=2;s=Device1.OPC001.temperature}"
}
],
"timeseries": [
{
"key": "humidity",
"path": "${Root\\.Objects\\.Device1\\.OPC001\\.Humidity}"
}
],
"rpc_methods": [
{
"method": "multiply",
"arguments": [
2,
4
]
}
],
"attributes_updates": [
{
"attributeOnThingsBoard": "deviceName",
"attributeOnDevice": "Root\\.Objects\\.Device1\\.OPC001\\.temperature"
}
]
}
]
}
}
server根据配置新建通道与属性
- 新建通道
- 选择通道类型
- 指定通道名称
按照配置文件中的名字定义,如"deviceNodePattern": “Root\.Objects\.Device1”,通道名称为Device1
- 添加设备
根据设备名称字段定义,如"deviceNamePattern": “Device ${Root\.Objects\.Device1\.OPC001}”
- 添加标记
设备的属性名称以及属性地址,对应
“attributes”: [
{
“key”: “temperature”,
“path”: “${Root\.Objects\.Device1\.OPC001\.temperature}”
}
地址值说明
- 查看添加好的值
获取到NodeId,写入到Path中
ns=2;s=Device1.OPC001.temperature
- 启动gateway
代码拿下来后运行是存在问题的,无法获取到属性值,源码讲解以及如何修改后续补上
- 查看Thingsboard页面中是否有名称为"deviceNamePattern": "OPC001"的设备注册上去