2021-12-24 06:22:45 +00:00
|
|
|
|
using PluginInterface;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Opc.Ua;
|
|
|
|
|
using Opc.Ua.Client;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Opc.Ua.Configuration;
|
2021-12-26 06:48:28 +00:00
|
|
|
|
using OpcUaHelper;
|
2021-12-24 06:22:45 +00:00
|
|
|
|
|
|
|
|
|
namespace DriverOPCUaClient
|
|
|
|
|
{
|
|
|
|
|
[DriverSupported("OPC UA")]
|
|
|
|
|
[DriverInfoAttribute("OPCUaClient", "V1.0.0", "Copyright WHD© 2021-12-19")]
|
|
|
|
|
public class OPCUaClient : IDriver
|
|
|
|
|
{
|
2021-12-26 06:48:28 +00:00
|
|
|
|
OpcUaClientHelper opcUaClient = null;
|
2021-12-24 06:22:45 +00:00
|
|
|
|
#region 配置参数
|
|
|
|
|
|
|
|
|
|
[ConfigParameter("设备Id")]
|
|
|
|
|
public Guid DeviceId { get; set; }
|
|
|
|
|
|
|
|
|
|
[ConfigParameter("uri")]
|
|
|
|
|
public string Uri { get; set; } = "opc.tcp://localhost:62541/Quickstarts/ReferenceServer";
|
|
|
|
|
|
|
|
|
|
[ConfigParameter("超时时间ms")]
|
|
|
|
|
public uint Timeout { get; set; } = 3000;
|
|
|
|
|
|
|
|
|
|
[ConfigParameter("最小通讯周期ms")]
|
|
|
|
|
public uint MinPeriod { get; set; } = 3000;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public OPCUaClient(Guid deviceId)
|
|
|
|
|
{
|
|
|
|
|
DeviceId = deviceId;
|
|
|
|
|
|
2021-12-26 06:48:28 +00:00
|
|
|
|
opcUaClient = new OpcUaClientHelper();
|
2021-12-24 06:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsConnected
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
|
2021-12-26 06:48:28 +00:00
|
|
|
|
return opcUaClient != null && opcUaClient.Connected;
|
2021-12-24 06:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Connect()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-26 06:48:28 +00:00
|
|
|
|
opcUaClient.ConnectServer(Uri).Wait((int)Timeout);
|
2021-12-24 06:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return IsConnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-26 06:48:28 +00:00
|
|
|
|
opcUaClient?.Disconnect();
|
2021-12-24 06:22:45 +00:00
|
|
|
|
return !IsConnected;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-26 06:48:28 +00:00
|
|
|
|
opcUaClient = null;
|
2021-12-24 06:22:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Method("读OPCUa", description: "读OPCUa节点")]
|
|
|
|
|
public DriverReturnValueModel ReadNode(DriverAddressIoArgModel ioarg)
|
|
|
|
|
{
|
|
|
|
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
|
|
|
|
|
|
|
|
|
if (IsConnected)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-12-26 06:48:28 +00:00
|
|
|
|
var dataValue = opcUaClient.ReadNode(new NodeId(ioarg.Address));
|
2021-12-24 06:22:45 +00:00
|
|
|
|
if (DataValue.IsGood(dataValue))
|
|
|
|
|
ret.Value = dataValue.Value;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = $"读取失败,{ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Method("测试方法", description: "测试方法,返回当前时间")]
|
|
|
|
|
public DriverReturnValueModel Read(DriverAddressIoArgModel ioarg)
|
|
|
|
|
{
|
|
|
|
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
|
|
|
|
|
|
|
|
|
if (IsConnected)
|
|
|
|
|
ret.Value = DateTime.Now;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|