Add Comments
This commit is contained in:
@@ -24,12 +24,28 @@ namespace Modbus.Net.OPC
|
||||
Seperator = seperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备
|
||||
/// </summary>
|
||||
public BaseMachine<TMachineKey, TUnitKey> Machine { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标签构造器
|
||||
/// (设备,地址)->不具备分隔符的标签数组
|
||||
/// </summary>
|
||||
protected Func<BaseMachine<TMachineKey, TUnitKey>, AddressUnit<TUnitKey>, string[]> TagGeter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分割符
|
||||
/// </summary>
|
||||
public char Seperator { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// 编码地址
|
||||
/// </summary>
|
||||
/// <param name="area">地址所在的数据区域</param>
|
||||
/// <param name="address">地址</param>
|
||||
/// <returns>编码后的地址</returns>
|
||||
public override string FormatAddress(string area, int address)
|
||||
{
|
||||
var findAddress = Machine?.GetAddresses.FirstOrDefault(p => p.Area == area && p.Address == address);
|
||||
@@ -42,6 +58,13 @@ namespace Modbus.Net.OPC
|
||||
return ans;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 编码地址
|
||||
/// </summary>
|
||||
/// <param name="area">地址所在的数据区域</param>
|
||||
/// <param name="address">地址</param>
|
||||
/// <param name="subAddress">子地址(忽略)</param>
|
||||
/// <returns>编码后的地址</returns>
|
||||
public override string FormatAddress(string area, int address, int subAddress)
|
||||
{
|
||||
return FormatAddress(area, address);
|
||||
|
||||
@@ -7,11 +7,22 @@ namespace Modbus.Net.OPC
|
||||
/// </summary>
|
||||
public class AddressTranslatorOpc : AddressTranslator
|
||||
{
|
||||
/// <summary>
|
||||
/// 地址转换
|
||||
/// </summary>
|
||||
/// <param name="address">格式化的地址</param>
|
||||
/// <param name="isRead">是否为读取,是为读取,否为写入</param>
|
||||
/// <returns>翻译后的地址</returns>
|
||||
public override AddressDef AddressTranslate(string address, bool isRead)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取区域中的单个地址占用的字节长度
|
||||
/// </summary>
|
||||
/// <param name="area">区域名称</param>
|
||||
/// <returns>字节长度</returns>
|
||||
public override double GetAreaByteLength(string area)
|
||||
{
|
||||
return 1;
|
||||
|
||||
@@ -7,54 +7,140 @@ using Hylasoft.Opc.Ua;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc Client Extend interface, Unified for DA and UA
|
||||
/// </summary>
|
||||
public interface IClientExtend : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Unified Root Node
|
||||
/// </summary>
|
||||
Node RootNodeBase { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Connect the client to the OPC Server
|
||||
/// </summary>
|
||||
void Connect();
|
||||
|
||||
/// <summary>
|
||||
/// Read a tag
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of tag to read</typeparam>
|
||||
/// <param name="tag">
|
||||
/// The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` reads the tag `bar` on the folder `foo`
|
||||
/// </param>
|
||||
/// <returns>The value retrieved from the OPC</returns>
|
||||
T Read<T>(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Write a value on the specified opc tag
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of tag to write on</typeparam>
|
||||
/// <param name="tag">
|
||||
/// The fully-qualified identifier of the tag. You can specify a subfolder by using a comma delimited name.
|
||||
/// E.g: the tag `foo.bar` writes on the tag `bar` on the folder `foo`
|
||||
/// </param>
|
||||
/// <param name="item"></param>
|
||||
void Write<T>(string tag, T item);
|
||||
|
||||
/// <summary>
|
||||
/// Read a tag asynchronusly
|
||||
/// </summary>
|
||||
Task<T> ReadAsync<T>(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Write a value on the specified opc tag asynchronously
|
||||
/// </summary>
|
||||
Task WriteAsync<T>(string tag, T item);
|
||||
|
||||
/// <summary>
|
||||
/// Finds a node on the Opc Server asynchronously
|
||||
/// </summary>
|
||||
Task<Node> FindNodeAsync(string tag);
|
||||
|
||||
/// <summary>
|
||||
/// Explore a folder on the Opc Server asynchronously
|
||||
/// </summary>
|
||||
Task<IEnumerable<Node>> ExploreFolderAsync(string tag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UaClient Extend
|
||||
/// </summary>
|
||||
public class MyDaClient : DaClient, IClientExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// UaClient Extend
|
||||
/// </summary>
|
||||
/// <param name="serverUrl">Url address of Opc UA server</param>
|
||||
public MyDaClient(Uri serverUrl) : base(serverUrl)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unified root node
|
||||
/// </summary>
|
||||
public Node RootNodeBase => RootNode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DaClient Extend
|
||||
/// </summary>
|
||||
public class MyUaClient : UaClient, IClientExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// DaClient Extend
|
||||
/// </summary>
|
||||
public MyUaClient(Uri serverUrl) : base(serverUrl)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unified root node
|
||||
/// </summary>
|
||||
public Node RootNodeBase => RootNode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Param input of OpcConnector
|
||||
/// </summary>
|
||||
public class OpcParamIn
|
||||
{
|
||||
/// <summary>
|
||||
/// Is the action read (not is write)
|
||||
/// </summary>
|
||||
public bool IsRead { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tag of a node
|
||||
/// </summary>
|
||||
public string Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tag splitter of a node
|
||||
/// </summary>
|
||||
public char Split { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The value set to node(only available when IsRead is false
|
||||
/// </summary>
|
||||
public object SetValue { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Param output of OpcConnector
|
||||
/// </summary>
|
||||
public class OpcParamOut
|
||||
{
|
||||
/// <summary>
|
||||
/// Is the action success
|
||||
/// </summary>
|
||||
public bool Success { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Action return values
|
||||
/// </summary>
|
||||
public byte[] Value { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,18 @@ using System.Configuration;
|
||||
|
||||
namespace Modbus.Net.OPC.FBox
|
||||
{
|
||||
/// <summary>
|
||||
/// FBox的Opc服务设备
|
||||
/// </summary>
|
||||
public class FBoxOpcDaMachine : OpcDaMachine
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="localSequence">页名称</param>
|
||||
/// <param name="linkerName">设备名称</param>
|
||||
/// <param name="getAddresses">获取地址</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public FBoxOpcDaMachine(string localSequence, string linkerName,
|
||||
IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(ConfigurationManager.AppSettings["FBoxOpcDaHost"], getAddresses, keepConnect)
|
||||
@@ -21,14 +31,26 @@ namespace Modbus.Net.OPC.FBox
|
||||
}, this, '.');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="localSequence">页名称</param>
|
||||
/// <param name="linkerName">设备名称</param>
|
||||
/// <param name="getAddresses">获取地址</param>
|
||||
public FBoxOpcDaMachine(string localSequence, string linkerName,
|
||||
IEnumerable<AddressUnit> getAddresses)
|
||||
: this(localSequence, linkerName, getAddresses, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 页名称
|
||||
/// </summary>
|
||||
public string LocalSequence { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备名称
|
||||
/// </summary>
|
||||
public string LinkerName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -9,19 +9,44 @@ using Serilog;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc连接器
|
||||
/// </summary>
|
||||
public abstract class OpcConnector : BaseConnector<OpcParamIn, OpcParamOut>
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否正在连接
|
||||
/// </summary>
|
||||
protected bool _connect;
|
||||
|
||||
/// <summary>
|
||||
/// Opc客户端
|
||||
/// </summary>
|
||||
protected IClientExtend Client;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">服务端url</param>
|
||||
protected OpcConnector(string host)
|
||||
{
|
||||
ConnectionToken = host;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接标识
|
||||
/// </summary>
|
||||
public override string ConnectionToken { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否正在连接
|
||||
/// </summary>
|
||||
public override bool IsConnected => _connect;
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override bool Disconnect()
|
||||
{
|
||||
try
|
||||
@@ -40,21 +65,43 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 无返回发送数据
|
||||
/// </summary>
|
||||
/// <param name="message">需要发送的数据</param>
|
||||
/// <returns>是否发送成功</returns>
|
||||
public override bool SendMsgWithoutReturn(OpcParamIn message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 无返回发送数据
|
||||
/// </summary>
|
||||
/// <param name="message">需要发送的数据</param>
|
||||
/// <returns>是否发送成功</returns>
|
||||
public override Task<bool> SendMsgWithoutReturnAsync(OpcParamIn message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带返回发送数据
|
||||
/// </summary>
|
||||
/// <param name="message">需要发送的数据</param>
|
||||
/// <returns>是否发送成功</returns>
|
||||
public override OpcParamOut SendMsg(OpcParamIn message)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SendMsgAsync(message));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据括号折叠已经打开的标签
|
||||
/// </summary>
|
||||
/// <param name="tagSplitList">已经打开的标签</param>
|
||||
/// <param name="splitChar">分割符</param>
|
||||
/// <param name="startChar">开始字符</param>
|
||||
/// <param name="endChar">结束字符</param>
|
||||
private void FoldWith(List<string> tagSplitList, char splitChar, char startChar, char endChar)
|
||||
{
|
||||
for (var i = 0; i < tagSplitList.Count; i++)
|
||||
@@ -72,6 +119,12 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据分隔符切分标签
|
||||
/// </summary>
|
||||
/// <param name="tag">标签</param>
|
||||
/// <param name="split">分隔符</param>
|
||||
/// <returns>分割后的标签</returns>
|
||||
private string[] SplitTag(string tag, char split)
|
||||
{
|
||||
var tagSplitList = tag.Split(split).ToList();
|
||||
@@ -83,6 +136,11 @@ namespace Modbus.Net.OPC
|
||||
return tagSplitList.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带返回发送数据
|
||||
/// </summary>
|
||||
/// <param name="message">需要发送的数据</param>
|
||||
/// <returns>是否发送成功</returns>
|
||||
public override async Task<OpcParamOut> SendMsgAsync(OpcParamIn message)
|
||||
{
|
||||
try
|
||||
@@ -154,6 +212,14 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 搜索标签
|
||||
/// </summary>
|
||||
/// <param name="tags">标签</param>
|
||||
/// <param name="split">分隔符</param>
|
||||
/// <param name="deep">递归深度(第几级标签)</param>
|
||||
/// <param name="nodes">当前搜索的节点</param>
|
||||
/// <returns>搜索到的标签</returns>
|
||||
private async Task<string> SearchTag(string[] tags, char split, int deep, IEnumerable<Node> nodes)
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
@@ -170,11 +236,10 @@ namespace Modbus.Net.OPC
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void AddInfo(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接PLC
|
||||
/// </summary>
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override bool Connect()
|
||||
{
|
||||
try
|
||||
@@ -192,6 +257,10 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接PLC,异步
|
||||
/// </summary>
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override Task<bool> ConnectAsync()
|
||||
{
|
||||
return Task.FromResult(Connect());
|
||||
|
||||
@@ -4,17 +4,29 @@ using System.Collections.Generic;
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa协议连接实现
|
||||
/// Opc DA连接实现
|
||||
/// </summary>
|
||||
public class OpcDaConnector : OpcConnector
|
||||
{
|
||||
/// <summary>
|
||||
/// DA单例管理
|
||||
/// </summary>
|
||||
protected static Dictionary<string, OpcDaConnector> _instances = new Dictionary<string, OpcDaConnector>();
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA 服务地址</param>
|
||||
protected OpcDaConnector(string host) : base(host)
|
||||
{
|
||||
Client = new MyDaClient(new Uri(ConnectionToken));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据服务地址生成DA单例
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA 服务地址</param>
|
||||
/// <returns>Opc DA 连接器实例</returns>
|
||||
public static OpcDaConnector Instance(string host)
|
||||
{
|
||||
if (!_instances.ContainsKey(host))
|
||||
|
||||
@@ -3,9 +3,20 @@ using System.Collections.Generic;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc DA设备
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">设备Id类型</typeparam>
|
||||
/// <typeparam name="TUnitKey">设备包含的地址的Id类型</typeparam>
|
||||
public class OpcDaMachine<TKey, TUnitKey> : OpcMachine<TKey, TUnitKey> where TKey : IEquatable<TKey>
|
||||
where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
@@ -14,14 +25,28 @@ namespace Modbus.Net.OPC
|
||||
() => ((AddressFormaterOpc<TKey, TUnitKey>) AddressFormater).Seperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||
: this(connectionString, getAddresses, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opc DA设备
|
||||
/// </summary>
|
||||
public class OpcDaMachine : OpcMachine
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
@@ -30,6 +55,11 @@ namespace Modbus.Net.OPC
|
||||
() => ((AddressFormaterOpc<string, string>) AddressFormater).Seperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses)
|
||||
: this(connectionString, getAddresses, false)
|
||||
{
|
||||
|
||||
@@ -3,29 +3,39 @@
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa协议
|
||||
/// Opc Da协议
|
||||
/// </summary>
|
||||
public class OpcDaProtocal : OpcProtocal
|
||||
{
|
||||
private readonly string _host;
|
||||
private int _connectTryCount;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA服务地址</param>
|
||||
public OpcDaProtocal(string host)
|
||||
{
|
||||
_host = host;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接设备
|
||||
/// </summary>
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override bool Connect()
|
||||
{
|
||||
return AsyncHelper.RunSync(ConnectAsync);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接设备
|
||||
/// </summary>
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
_connectTryCount++;
|
||||
ProtocalLinker = new OpcDaProtocalLinker(_host);
|
||||
if (!await ProtocalLinker.ConnectAsync()) return false;
|
||||
_connectTryCount = 0;
|
||||
if (!await ProtocalLinker.ConnectAsync())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,17 @@ namespace Modbus.Net.OPC
|
||||
/// </summary>
|
||||
public class OpcDaProtocalLinker : OpcProtocalLinker
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public OpcDaProtocalLinker() : this(ConfigurationManager.AppSettings["OpcDaHost"])
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA服务地址</param>
|
||||
public OpcDaProtocalLinker(string host)
|
||||
{
|
||||
BaseConnector = OpcDaConnector.Instance(host);
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
/// </summary>
|
||||
public class OpcDaUtility : OpcUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
public OpcDaUtility(string connectionString) : base(connectionString)
|
||||
{
|
||||
Wrapper = new OpcDaProtocal(ConnectionString);
|
||||
|
||||
@@ -4,11 +4,16 @@ using System.Collections.Generic;
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa设备
|
||||
/// Opc Da设备
|
||||
/// </summary>
|
||||
public abstract class OpcMachine<TKey, TUnitKey> : BaseMachine<TKey, TUnitKey> where TKey : IEquatable<TKey>
|
||||
where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="getAddresses">需要读写的地址</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
protected OpcMachine(IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
@@ -18,10 +23,15 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OpcDa设备
|
||||
/// Opc Da设备
|
||||
/// </summary>
|
||||
public abstract class OpcMachine : BaseMachine
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="getAddresses">需要读写的地址</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
protected OpcMachine(IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
/// </summary>
|
||||
public abstract class OpcProtocal : BaseProtocal<OpcParamIn, OpcParamOut, ProtocalUnit<OpcParamIn, OpcParamOut>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
protected OpcProtocal() : base(0, 0, Endian.BigEndianLsb)
|
||||
{
|
||||
}
|
||||
@@ -12,30 +15,63 @@
|
||||
|
||||
#region 读数据
|
||||
|
||||
/// <summary>
|
||||
/// 读数据输入
|
||||
/// </summary>
|
||||
public class ReadRequestOpcInputStruct : IInputStruct
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="tag">标签</param>
|
||||
/// <param name="split">分隔符</param>
|
||||
public ReadRequestOpcInputStruct(string tag, char split)
|
||||
{
|
||||
Tag = tag;
|
||||
Split = split;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签
|
||||
/// </summary>
|
||||
public string Tag { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 分隔符
|
||||
/// </summary>
|
||||
public char Split { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读地址输出
|
||||
/// </summary>
|
||||
public class ReadRequestOpcOutputStruct : IOutputStruct
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="value">读取的数据</param>
|
||||
public ReadRequestOpcOutputStruct(byte[] value)
|
||||
{
|
||||
GetValue = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取的地址
|
||||
/// </summary>
|
||||
public byte[] GetValue { get; private set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读数据协议
|
||||
/// </summary>
|
||||
public class ReadRequestOpcProtocal : ProtocalUnit<OpcParamIn, OpcParamOut>, ISpecialProtocalUnit
|
||||
{
|
||||
/// <summary>
|
||||
/// 从对象的参数数组格式化
|
||||
/// </summary>
|
||||
/// <param name="message">非结构化的输入数据</param>
|
||||
/// <returns>格式化后的字节流</returns>
|
||||
public override OpcParamIn Format(IInputStruct message)
|
||||
{
|
||||
var r_message = (ReadRequestOpcInputStruct) message;
|
||||
@@ -47,6 +83,12 @@
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把仪器返回的内容填充到输出结构中
|
||||
/// </summary>
|
||||
/// <param name="messageBytes">返回数据的字节流</param>
|
||||
/// <param name="pos">转换标记位</param>
|
||||
/// <returns>结构化的输出数据</returns>
|
||||
public override IOutputStruct Unformat(OpcParamOut messageBytes, ref int pos)
|
||||
{
|
||||
return new ReadRequestOpcOutputStruct(messageBytes.Value);
|
||||
@@ -57,8 +99,17 @@
|
||||
|
||||
#region 写数据
|
||||
|
||||
/// <summary>
|
||||
/// 写数据输入
|
||||
/// </summary>
|
||||
public class WriteRequestOpcInputStruct : IInputStruct
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="tag">标签</param>
|
||||
/// <param name="split">分隔符</param>
|
||||
/// <param name="setValue">写入的数据</param>
|
||||
public WriteRequestOpcInputStruct(string tag, char split, object setValue)
|
||||
{
|
||||
Tag = tag;
|
||||
@@ -66,23 +117,52 @@
|
||||
SetValue = setValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标签
|
||||
/// </summary>
|
||||
public string Tag { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 分隔符
|
||||
/// </summary>
|
||||
public char Split { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 写入的数据
|
||||
/// </summary>
|
||||
public object SetValue { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写数据输出
|
||||
/// </summary>
|
||||
public class WriteRequestOpcOutputStruct : IOutputStruct
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="writeResult">写入是否成功</param>
|
||||
public WriteRequestOpcOutputStruct(bool writeResult)
|
||||
{
|
||||
WriteResult = writeResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入是否成功
|
||||
/// </summary>
|
||||
public bool WriteResult { get; private set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写数据协议
|
||||
/// </summary>
|
||||
public class WriteRequestOpcProtocal : ProtocalUnit<OpcParamIn, OpcParamOut>, ISpecialProtocalUnit
|
||||
{
|
||||
/// <summary>
|
||||
/// 从对象的参数数组格式化
|
||||
/// </summary>
|
||||
/// <param name="message">非结构化的输入数据</param>
|
||||
/// <returns>格式化后的字节流</returns>
|
||||
public override OpcParamIn Format(IInputStruct message)
|
||||
{
|
||||
var r_message = (WriteRequestOpcInputStruct) message;
|
||||
@@ -95,6 +175,12 @@
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把仪器返回的内容填充到输出结构中
|
||||
/// </summary>
|
||||
/// <param name="messageBytes">返回数据的字节流</param>
|
||||
/// <param name="pos">转换标记位</param>
|
||||
/// <returns>结构化的输出数据</returns>
|
||||
public override IOutputStruct Unformat(OpcParamOut messageBytes, ref int pos)
|
||||
{
|
||||
var ansByte = BigEndianValueHelper.Instance.GetByte(messageBytes.Value, ref pos);
|
||||
|
||||
@@ -4,17 +4,29 @@ using System.Collections.Generic;
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa协议连接实现
|
||||
/// Opc UA连接实现
|
||||
/// </summary>
|
||||
public class OpcUaConnector : OpcConnector
|
||||
{
|
||||
/// <summary>
|
||||
/// UA单例管理
|
||||
/// </summary>
|
||||
protected static Dictionary<string, OpcUaConnector> _instances = new Dictionary<string, OpcUaConnector>();
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA 服务地址</param>
|
||||
protected OpcUaConnector(string host) : base(host)
|
||||
{
|
||||
Client = new MyUaClient(new Uri(ConnectionToken));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据地址获取UA连接器单例
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA服务地址</param>
|
||||
/// <returns>OPC UA实例</returns>
|
||||
public static OpcUaConnector Instance(string host)
|
||||
{
|
||||
if (!_instances.ContainsKey(host))
|
||||
|
||||
@@ -3,9 +3,20 @@ using System.Collections.Generic;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc UA设备
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">设备Id的类型</typeparam>
|
||||
/// <typeparam name="TUnitKey">设备中地址的Id的类型</typeparam>
|
||||
public class OpcUaMachine<TKey, TUnitKey> : OpcMachine<TKey, TUnitKey> where TKey : IEquatable<TKey>
|
||||
where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
@@ -14,14 +25,28 @@ namespace Modbus.Net.OPC
|
||||
() => ((AddressFormaterOpc<string, string>) AddressFormater).Seperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||
: this(connectionString, getAddresses, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opc UA设备
|
||||
/// </summary>
|
||||
public class OpcUaMachine : OpcMachine
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
@@ -30,6 +55,11 @@ namespace Modbus.Net.OPC
|
||||
() => ((AddressFormaterOpc<string, string>) AddressFormater).Seperator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses)
|
||||
: this(connectionString, getAddresses, false)
|
||||
{
|
||||
|
||||
@@ -3,29 +3,38 @@
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcUa协议
|
||||
/// Opc UA协议
|
||||
/// </summary>
|
||||
public class OpcUaProtocal : OpcProtocal
|
||||
{
|
||||
private readonly string _host;
|
||||
private int _connectTryCount;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA服务地址</param>
|
||||
public OpcUaProtocal(string host)
|
||||
{
|
||||
_host = host;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接设备
|
||||
/// </summary>
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override bool Connect()
|
||||
{
|
||||
return AsyncHelper.RunSync(ConnectAsync);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接设备
|
||||
/// </summary>
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
_connectTryCount++;
|
||||
ProtocalLinker = new OpcUaProtocalLinker(_host);
|
||||
if (!await ProtocalLinker.ConnectAsync()) return false;
|
||||
_connectTryCount = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,21 @@
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc Da协议连接器
|
||||
/// Opc UA协议连接器
|
||||
/// </summary>
|
||||
public class OpcUaProtocalLinker : OpcProtocalLinker
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public OpcUaProtocalLinker() : this(ConfigurationManager.AppSettings["OpcUaHost"])
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA服务地址</param>
|
||||
public OpcUaProtocalLinker(string host)
|
||||
{
|
||||
BaseConnector = OpcUaConnector.Instance(host);
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc Da协议Api入口
|
||||
/// Opc Ua协议Api入口
|
||||
/// </summary>
|
||||
public class OpcUaUtility : OpcUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
public OpcUaUtility(string connectionString) : base(connectionString)
|
||||
{
|
||||
Wrapper = new OpcUaProtocal(ConnectionString);
|
||||
|
||||
@@ -4,25 +4,52 @@ using Serilog;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc通用Api入口
|
||||
/// </summary>
|
||||
public abstract class OpcUtility : BaseUtility<OpcParamIn, OpcParamOut, ProtocalUnit<OpcParamIn, OpcParamOut>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取分隔符
|
||||
/// </summary>
|
||||
/// <returns>分隔符</returns>
|
||||
public delegate char GetSeperatorDelegate();
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
protected OpcUtility(string connectionString) : base(0, 0)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
AddressTranslator = new AddressTranslatorOpc();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 端格式(大端)
|
||||
/// </summary>
|
||||
public override Endian Endian => Endian.BigEndianLsb;
|
||||
|
||||
/// <summary>
|
||||
/// 获取分隔符
|
||||
/// </summary>
|
||||
public event GetSeperatorDelegate GetSeperator;
|
||||
|
||||
/// <summary>
|
||||
/// 设置连接方式(Opc忽略该函数)
|
||||
/// </summary>
|
||||
/// <param name="connectionType">连接方式</param>
|
||||
public override void SetConnectionType(int connectionType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//ignore
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
/// </summary>
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="getByteCount">获取字节数个数</param>
|
||||
/// <returns>接收到的byte数据</returns>
|
||||
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
|
||||
{
|
||||
try
|
||||
@@ -42,6 +69,12 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置数据
|
||||
/// </summary>
|
||||
/// <param name="startAddress">开始地址</param>
|
||||
/// <param name="setContents">设置数据</param>
|
||||
/// <returns>是否设置成功</returns>
|
||||
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user