2017-06-14 update 2 Fix OPC Regex tags combine bug.
This commit is contained in:
@@ -53,7 +53,7 @@ namespace Modbus.Net.OPC
|
||||
var strings = TagGeter(Machine, findAddress);
|
||||
var ans = "";
|
||||
for (var i = 0; i < strings.Length; i++)
|
||||
ans += strings[i].Trim().Replace(" ", "") + Seperator;
|
||||
ans += strings[i].Trim().Replace(" ", "") + '\r';
|
||||
ans = ans.Substring(0, ans.Length - 1);
|
||||
return ans;
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ namespace Modbus.Net.OPC
|
||||
/// <summary>
|
||||
/// Tag of a node
|
||||
/// </summary>
|
||||
public string Tag { get; set; }
|
||||
public string[] Tag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tag splitter of a node
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Modbus.Net.OPC.FBox
|
||||
IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(
|
||||
ConfigurationManager.AppSettings["FBoxOpcDaHost"] ?? "opcda://localhost/FBoxOpcServer", getAddresses,
|
||||
keepConnect)
|
||||
keepConnect, true)
|
||||
{
|
||||
LocalSequence = localSequence;
|
||||
LinkerName = linkerName;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<AssemblyName>Modbus.Net.OPC</AssemblyName>
|
||||
<RootNamespace>Modbus.Net.OPC</RootNamespace>
|
||||
<PackageId>Modbus.Net.OPC</PackageId>
|
||||
<Version>1.3.9-beta1</Version>
|
||||
<Version>1.3.9-beta2</Version>
|
||||
<Authors>Chris L.(Luo Sheng)</Authors>
|
||||
<Company>Hangzhou Delian IoT Science Technology Co.,Ltd.</Company>
|
||||
<Description>Modbus.Net OPC Implementation</Description>
|
||||
|
||||
@@ -24,13 +24,20 @@ namespace Modbus.Net.OPC
|
||||
/// </summary>
|
||||
protected IClientExtend Client;
|
||||
|
||||
/// <summary>
|
||||
/// 是否开启正则匹配
|
||||
/// </summary>
|
||||
protected bool RegexOn { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">服务端url</param>
|
||||
protected OpcConnector(string host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
protected OpcConnector(string host, bool isRegexOn)
|
||||
{
|
||||
ConnectionToken = host;
|
||||
RegexOn = isRegexOn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -95,47 +102,6 @@ namespace Modbus.Net.OPC
|
||||
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++)
|
||||
if (tagSplitList[i].Count(ch => ch == startChar) > tagSplitList[i].Count(ch => ch == endChar))
|
||||
for (var j = i + 1; j < tagSplitList.Count; j++)
|
||||
if (tagSplitList[j].Contains(endChar))
|
||||
{
|
||||
for (var k = i + 1; k <= j; k++)
|
||||
{
|
||||
tagSplitList[i] += splitChar + tagSplitList[i + 1];
|
||||
tagSplitList.RemoveAt(i + 1);
|
||||
}
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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();
|
||||
|
||||
FoldWith(tagSplitList, split, '(', ')');
|
||||
FoldWith(tagSplitList, split, '[', ']');
|
||||
FoldWith(tagSplitList, split, '{', '}');
|
||||
|
||||
return tagSplitList.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 带返回发送数据
|
||||
/// </summary>
|
||||
@@ -149,9 +115,8 @@ namespace Modbus.Net.OPC
|
||||
{
|
||||
var split = message.Split;
|
||||
var tag = message.Tag;
|
||||
var tagSplit = SplitTag(tag, split);
|
||||
var rootDirectory = await Client.ExploreFolderAsync("");
|
||||
var answerTag = await SearchTag(tagSplit, split, 0, rootDirectory);
|
||||
var answerTag = await SearchTag(tag, split, 0, rootDirectory);
|
||||
if (answerTag != null)
|
||||
{
|
||||
var result = await Client.ReadAsync<object>(answerTag);
|
||||
@@ -175,8 +140,7 @@ namespace Modbus.Net.OPC
|
||||
var value = message.SetValue;
|
||||
|
||||
var rootDirectory = await Client.ExploreFolderAsync("");
|
||||
var tagSplit = SplitTag(tag, split);
|
||||
var answerTag = await SearchTag(tagSplit, split, 0, rootDirectory);
|
||||
var answerTag = await SearchTag(tag, split, 0, rootDirectory);
|
||||
if (answerTag != null)
|
||||
{
|
||||
try
|
||||
@@ -227,7 +191,7 @@ namespace Modbus.Net.OPC
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
var currentTag = node.Tag.Substring(node.Tag.LastIndexOf(split) + 1);
|
||||
if (Regex.IsMatch(currentTag, tags[deep]))
|
||||
if (RegexOn && Regex.IsMatch(currentTag, tags[deep]) || !RegexOn && currentTag == tags[deep])
|
||||
{
|
||||
if (deep == tags.Length - 1) return node.Tag;
|
||||
var subDirectories = await Client.ExploreFolderAsync(node.Tag);
|
||||
|
||||
@@ -17,7 +17,8 @@ namespace Modbus.Net.OPC
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA 服务地址</param>
|
||||
protected OpcDaConnector(string host) : base(host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
protected OpcDaConnector(string host, bool isRegexOn) : base(host, isRegexOn)
|
||||
{
|
||||
Client = new MyDaClient(new Uri(ConnectionToken));
|
||||
}
|
||||
@@ -26,12 +27,13 @@ namespace Modbus.Net.OPC
|
||||
/// 根据服务地址生成DA单例
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA 服务地址</param>
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
/// <returns>Opc DA 连接器实例</returns>
|
||||
public static OpcDaConnector Instance(string host)
|
||||
public static OpcDaConnector Instance(string host, bool isRegexOn)
|
||||
{
|
||||
if (!_instances.ContainsKey(host))
|
||||
{
|
||||
var connector = new OpcDaConnector(host);
|
||||
var connector = new OpcDaConnector(host, isRegexOn);
|
||||
_instances.Add(host, connector);
|
||||
}
|
||||
return _instances[host];
|
||||
|
||||
@@ -17,10 +17,11 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect, bool isRegexOn = false)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcDaUtility(connectionString);
|
||||
BaseUtility = new OpcDaUtility(connectionString, isRegexOn);
|
||||
((OpcUtility) BaseUtility).GetSeperator +=
|
||||
() => ((AddressFormaterOpc<TKey, TUnitKey>) AddressFormater).Seperator;
|
||||
}
|
||||
@@ -47,10 +48,11 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect, bool isRegexOn = false)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcDaUtility(connectionString);
|
||||
BaseUtility = new OpcDaUtility(connectionString, isRegexOn);
|
||||
((OpcUtility) BaseUtility).GetSeperator +=
|
||||
() => ((AddressFormaterOpc<string, string>) AddressFormater).Seperator;
|
||||
}
|
||||
|
||||
@@ -9,13 +9,17 @@ namespace Modbus.Net.OPC
|
||||
{
|
||||
private readonly string _host;
|
||||
|
||||
private readonly bool _isRegexOn;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA服务地址</param>
|
||||
public OpcDaProtocal(string host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcDaProtocal(string host, bool isRegexOn)
|
||||
{
|
||||
_host = host;
|
||||
_isRegexOn = isRegexOn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -33,7 +37,7 @@ namespace Modbus.Net.OPC
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
ProtocalLinker = new OpcDaProtocalLinker(_host);
|
||||
ProtocalLinker = new OpcDaProtocalLinker(_host, _isRegexOn);
|
||||
if (!await ProtocalLinker.ConnectAsync())
|
||||
return false;
|
||||
return true;
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace Modbus.Net.OPC
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public OpcDaProtocalLinker() : this(ConfigurationManager.AppSettings["OpcDaHost"])
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcDaProtocalLinker(bool isRegexOn) : this(ConfigurationManager.AppSettings["OpcDaHost"], isRegexOn)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -18,9 +19,10 @@ namespace Modbus.Net.OPC
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc DA服务地址</param>
|
||||
public OpcDaProtocalLinker(string host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcDaProtocalLinker(string host, bool isRegexOn)
|
||||
{
|
||||
BaseConnector = OpcDaConnector.Instance(host);
|
||||
BaseConnector = OpcDaConnector.Instance(host, isRegexOn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,10 @@
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
public OpcDaUtility(string connectionString) : base(connectionString)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcDaUtility(string connectionString, bool isRegexOn = false) : base(connectionString)
|
||||
{
|
||||
Wrapper = new OpcDaProtocal(ConnectionString);
|
||||
Wrapper = new OpcDaProtocal(ConnectionString, isRegexOn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@
|
||||
/// </summary>
|
||||
/// <param name="tag">标签</param>
|
||||
/// <param name="split">分隔符</param>
|
||||
public ReadRequestOpcInputStruct(string tag, char split)
|
||||
public ReadRequestOpcInputStruct(string[] tag, char split)
|
||||
{
|
||||
Tag = tag;
|
||||
Split = split;
|
||||
@@ -34,7 +34,7 @@
|
||||
/// <summary>
|
||||
/// 标签
|
||||
/// </summary>
|
||||
public string Tag { get; }
|
||||
public string[] Tag { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 分隔符
|
||||
@@ -111,7 +111,7 @@
|
||||
/// <param name="tag">标签</param>
|
||||
/// <param name="split">分隔符</param>
|
||||
/// <param name="setValue">写入的数据</param>
|
||||
public WriteRequestOpcInputStruct(string tag, char split, object setValue)
|
||||
public WriteRequestOpcInputStruct(string[] tag, char split, object setValue)
|
||||
{
|
||||
Tag = tag;
|
||||
Split = split;
|
||||
@@ -121,7 +121,7 @@
|
||||
/// <summary>
|
||||
/// 标签
|
||||
/// </summary>
|
||||
public string Tag { get; }
|
||||
public string[] Tag { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 分隔符
|
||||
|
||||
@@ -17,7 +17,8 @@ namespace Modbus.Net.OPC
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA 服务地址</param>
|
||||
protected OpcUaConnector(string host) : base(host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
protected OpcUaConnector(string host, bool isRegexOn) : base(host, isRegexOn)
|
||||
{
|
||||
Client = new MyUaClient(new Uri(ConnectionToken));
|
||||
}
|
||||
@@ -26,12 +27,13 @@ namespace Modbus.Net.OPC
|
||||
/// 根据地址获取UA连接器单例
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA服务地址</param>
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
/// <returns>OPC UA实例</returns>
|
||||
public static OpcUaConnector Instance(string host)
|
||||
public static OpcUaConnector Instance(string host, bool isRegexOn)
|
||||
{
|
||||
if (!_instances.ContainsKey(host))
|
||||
{
|
||||
var connector = new OpcUaConnector(host);
|
||||
var connector = new OpcUaConnector(host, isRegexOn);
|
||||
_instances.Add(host, connector);
|
||||
}
|
||||
return _instances[host];
|
||||
|
||||
@@ -17,10 +17,11 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect, bool isRegexOn = false)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcUaUtility(connectionString);
|
||||
BaseUtility = new OpcUaUtility(connectionString, isRegexOn);
|
||||
((OpcUtility) BaseUtility).GetSeperator +=
|
||||
() => ((AddressFormaterOpc<string, string>) AddressFormater).Seperator;
|
||||
}
|
||||
@@ -47,10 +48,11 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
/// <param name="getAddresses">需要读写的数据</param>
|
||||
/// <param name="keepConnect">是否保持连接</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect, bool isRegexOn = false)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcUaUtility(connectionString);
|
||||
BaseUtility = new OpcUaUtility(connectionString, isRegexOn);
|
||||
((OpcUtility) BaseUtility).GetSeperator +=
|
||||
() => ((AddressFormaterOpc<string, string>) AddressFormater).Seperator;
|
||||
}
|
||||
|
||||
@@ -9,13 +9,17 @@ namespace Modbus.Net.OPC
|
||||
{
|
||||
private readonly string _host;
|
||||
|
||||
private readonly bool _isRegexOn;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA服务地址</param>
|
||||
public OpcUaProtocal(string host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcUaProtocal(string host, bool isRegexOn)
|
||||
{
|
||||
_host = host;
|
||||
_isRegexOn = isRegexOn;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -33,7 +37,7 @@ namespace Modbus.Net.OPC
|
||||
/// <returns>是否连接成功</returns>
|
||||
public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
ProtocalLinker = new OpcUaProtocalLinker(_host);
|
||||
ProtocalLinker = new OpcUaProtocalLinker(_host, _isRegexOn);
|
||||
if (!await ProtocalLinker.ConnectAsync()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace Modbus.Net.OPC
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public OpcUaProtocalLinker() : this(ConfigurationManager.AppSettings["OpcUaHost"])
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcUaProtocalLinker(bool isRegexOn) : this(ConfigurationManager.AppSettings["OpcUaHost"], isRegexOn)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -18,9 +19,10 @@ namespace Modbus.Net.OPC
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="host">Opc UA服务地址</param>
|
||||
public OpcUaProtocalLinker(string host)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcUaProtocalLinker(string host, bool isRegexOn)
|
||||
{
|
||||
BaseConnector = OpcUaConnector.Instance(host);
|
||||
BaseConnector = OpcUaConnector.Instance(host, isRegexOn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,10 @@
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="connectionString">连接地址</param>
|
||||
public OpcUaUtility(string connectionString) : base(connectionString)
|
||||
/// <param name="isRegexOn">是否开启正则匹配</param>
|
||||
public OpcUaUtility(string connectionString, bool isRegexOn = false) : base(connectionString)
|
||||
{
|
||||
Wrapper = new OpcUaProtocal(ConnectionString);
|
||||
Wrapper = new OpcUaProtocal(ConnectionString, isRegexOn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ namespace Modbus.Net.OPC
|
||||
try
|
||||
{
|
||||
var split = GetSeperator?.Invoke() ?? '/';
|
||||
var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress, split);
|
||||
var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress.Split('\r'), split);
|
||||
var readRequestOpcOutputStruct =
|
||||
await
|
||||
Wrapper.SendReceiveAsync<ReadRequestOpcOutputStruct>(Wrapper[typeof(ReadRequestOpcProtocal)],
|
||||
@@ -80,7 +80,7 @@ namespace Modbus.Net.OPC
|
||||
try
|
||||
{
|
||||
var split = GetSeperator?.Invoke() ?? '/';
|
||||
var writeRequestOpcInputStruct = new WriteRequestOpcInputStruct(startAddress, split, setContents[0]);
|
||||
var writeRequestOpcInputStruct = new WriteRequestOpcInputStruct(startAddress.Split('\r'), split, setContents[0]);
|
||||
var writeRequestOpcOutputStruct =
|
||||
await
|
||||
Wrapper.SendReceiveAsync<WriteRequestOpcOutputStruct>(Wrapper[typeof(WriteRequestOpcProtocal)],
|
||||
|
||||
Reference in New Issue
Block a user