diff --git a/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs b/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs
index ea16eb8..d4b4ca6 100644
--- a/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs
+++ b/Modbus.Net/Modbus.Net.OPC/AddressFormaterOpc.cs
@@ -6,7 +6,7 @@ namespace Modbus.Net.OPC
///
/// Opc地址编码器
///
- public class AddressFormaterOpc : AddressFormater
+ public class AddressFormaterOpc : AddressFormater where TMachineKey : IEquatable where TUnitKey : IEquatable
{
///
/// 协议构造器
@@ -14,7 +14,7 @@ namespace Modbus.Net.OPC
/// 如何通过BaseMachine和AddressUnit构造Opc的标签
/// 调用这个编码器的设备
/// 每两个标签之间用什么符号隔开,默认为/
- public AddressFormaterOpc(Func tagGeter, BaseMachine machine,
+ public AddressFormaterOpc(Func, AddressUnit, string[]> tagGeter, BaseMachine machine,
char seperator = '/')
{
Machine = machine;
@@ -22,9 +22,9 @@ namespace Modbus.Net.OPC
Seperator = seperator;
}
- public BaseMachine Machine { get; set; }
+ public BaseMachine Machine { get; set; }
- protected Func TagGeter { get; set; }
+ protected Func, AddressUnit, string[]> TagGeter { get; set; }
protected char Seperator { get; set; }
diff --git a/Modbus.Net/Modbus.Net.OPC/ClientExtend.cs b/Modbus.Net/Modbus.Net.OPC/ClientExtend.cs
new file mode 100644
index 0000000..dc363db
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.OPC/ClientExtend.cs
@@ -0,0 +1,42 @@
+using System;
+using System.Threading.Tasks;
+using Hylasoft.Opc.Common;
+using Hylasoft.Opc.Da;
+using Opc;
+using Opc.Da;
+using System.Collections.Generic;
+using Hylasoft.Opc.Ua;
+
+namespace Modbus.Net.OPC
+{
+ public interface IClientExtend : IDisposable
+ {
+ void Connect();
+
+ T Read(string tag);
+
+ void Write(string tag, T item);
+
+ Task ReadAsync(string tag);
+
+ Task WriteAsync(string tag, T item);
+
+ Task FindNodeAsync(string tag);
+
+ Task> ExploreFolderAsync(string tag);
+ }
+
+ public class MyDaClient : DaClient, IClientExtend
+ {
+ public MyDaClient(Uri serverUrl) : base(serverUrl)
+ {
+ }
+ }
+
+ public class MyUaClient : UaClient, IClientExtend
+ {
+ public MyUaClient(Uri serverUrl) : base(serverUrl)
+ {
+ }
+ }
+}
\ No newline at end of file
diff --git a/Modbus.Net/Modbus.Net.OPC/DaClientExtend.cs b/Modbus.Net/Modbus.Net.OPC/DaClientExtend.cs
deleted file mode 100644
index f5c196a..0000000
--- a/Modbus.Net/Modbus.Net.OPC/DaClientExtend.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using Hylasoft.Opc.Common;
-using Hylasoft.Opc.Da;
-using Opc;
-using Opc.Da;
-
-namespace Modbus.Net.OPC
-{
- ///
- /// Read value full result
- ///
- public class OpcValueResult
- {
- public object Value { get; set; }
- public DateTime Timestamp { get; set; }
- public bool QualityGood { get; set; }
- }
-
- public class MyDaClient : DaClient
- {
- public MyDaClient(Uri serverUrl) : base(serverUrl)
- {
- }
-
- ///
- /// Write a value on the specified opc 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`
- ///
- public OpcValueResult Read(string tag)
- {
- var item = new Item {ItemName = tag};
- var result = Server.Read(new[] {item})[0];
- CheckResult(result, tag);
- return new OpcValueResult
- {
- Value = result.Value,
- Timestamp = result.Timestamp,
- QualityGood = result.Quality == Quality.Good
- };
- }
-
- ///
- /// Read a tag asynchronusly
- ///
- public Task ReadAsync(string tag)
- {
- return Task.Run(() => Read(tag));
- }
-
- private static void CheckResult(IResult result, string tag)
- {
- if (result == null)
- throw new OpcException("The server replied with an empty response");
- if (result.ResultID.ToString() != "S_OK")
- throw new OpcException(
- string.Format("Invalid response from the server. (Response Status: {0}, Opc Tag: {1})",
- result.ResultID, tag));
- }
- }
-}
\ No newline at end of file
diff --git a/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs b/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs
index 06b2d51..2e7b069 100644
--- a/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs
+++ b/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs
@@ -6,19 +6,19 @@ using System.Threading.Tasks;
namespace Modbus.Net.OPC.FBox
{
- public class FBoxOpcDaMachine : OpcDaMachine
+ public class FBoxOpcDaMachine : OpcDaMachine
{
public string LocalSequence { get; set; }
public string LinkerName { get; set; }
public FBoxOpcDaMachine(string localSequence, string linkerName,
- IEnumerable getAddresses, bool keepConnect) : base(ConfigurationManager.FBoxOpcDaHost, getAddresses, keepConnect)
+ IEnumerable> getAddresses, bool keepConnect) : base(ConfigurationManager.FBoxOpcDaHost, getAddresses, keepConnect)
{
LocalSequence = localSequence;
LinkerName = linkerName;
AddressFormater =
- new AddressFormaterOpc(
+ new AddressFormaterOpc(
(machine, unit) =>
new string[]
{
diff --git a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj
index 71afb08..48124b1 100644
--- a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj
+++ b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.csproj
@@ -31,33 +31,26 @@
4
-
- ..\packages\H.Opc.0.7.0\lib\h-opc.dll
- True
+
+ ..\packages\H.Opc.0.8.1\lib\h-opc.dll
- ..\packages\H.Opc.0.7.0\lib\Opc.Ua.Client.dll
- True
+ ..\packages\H.Opc.0.8.1\lib\Opc.Ua.Client.dll
- ..\packages\H.Opc.0.7.0\lib\Opc.Ua.Configuration.dll
- True
+ ..\packages\H.Opc.0.8.1\lib\Opc.Ua.Configuration.dll
- ..\packages\H.Opc.0.7.0\lib\Opc.Ua.Core.dll
- True
+ ..\packages\H.Opc.0.8.1\lib\Opc.Ua.Core.dll
- ..\packages\H.Opc.0.7.0\lib\OpcComRcw.dll
- True
+ ..\packages\H.Opc.0.8.1\lib\OpcComRcw.dll
- ..\packages\H.Opc.0.7.0\lib\OpcNetApi.dll
- True
+ ..\packages\H.Opc.0.8.1\lib\OpcNetApi.dll
- ..\packages\H.Opc.0.7.0\lib\OpcNetApi.Com.dll
- True
+ ..\packages\H.Opc.0.8.1\lib\OpcNetApi.Com.dll
@@ -71,14 +64,22 @@
-
+
+
+
+
+
+
+
+
+
diff --git a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec
index 73dfd19..e8e8965 100644
--- a/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec
+++ b/Modbus.Net/Modbus.Net.OPC/Modbus.Net.OPC.nuspec
@@ -2,7 +2,7 @@
Modbus.Net.OPC
- 1.2.3
+ 1.2.4
Modbus.Net.OPC
Chris L.(Luo Sheng)
Hangzhou Delian IoT Science Technology Co.,Ltd.
@@ -14,7 +14,7 @@
hardware communicate protocal OPC DA Delian
-
+
diff --git a/Modbus.Net/Modbus.Net.OPC/OpcConnector.cs b/Modbus.Net/Modbus.Net.OPC/OpcConnector.cs
new file mode 100644
index 0000000..b5fd889
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.OPC/OpcConnector.cs
@@ -0,0 +1,167 @@
+using Hylasoft.Opc.Common;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+
+namespace Modbus.Net.OPC
+{
+ public abstract class OpcConnector : BaseConnector
+ {
+ protected IClientExtend Client;
+
+ protected bool _connect;
+ public override string ConnectionToken { get; }
+ public override bool IsConnected => _connect;
+
+ protected OpcConnector(string host)
+ {
+ ConnectionToken = host;
+ }
+
+ public override bool Disconnect()
+ {
+ try
+ {
+ Client?.Dispose();
+ Client = null;
+ _connect = false;
+ AddInfo("client disconnected successfully.");
+ return true;
+ }
+ catch (Exception ex)
+ {
+ AddInfo("client disconnected exception: " + ex.Message);
+ _connect = false;
+ return false;
+ }
+ }
+
+ public override bool SendMsgWithoutReturn(byte[] message)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override Task SendMsgWithoutReturnAsync(byte[] message)
+ {
+ throw new NotImplementedException();
+ }
+
+ public override byte[] SendMsg(byte[] message)
+ {
+ return AsyncHelper.RunSync(() => SendMsgAsync(message));
+ }
+
+ public override async Task SendMsgAsync(byte[] message)
+ {
+ try
+ {
+ var pos = 0;
+ var protocal = BigEndianValueHelper.Instance.GetByte(message, ref pos);
+ if (protocal == 0)
+ {
+ var tagBytes = new byte[message.Length - 1];
+ Array.Copy(message, 1, tagBytes, 0, tagBytes.Length);
+ var tag = Encoding.UTF8.GetString(tagBytes);
+ var tagSplit = tag.Split('/');
+ var rootDirectory = await Client.ExploreFolderAsync("/");
+ var answerTag = await SearchTag(tagSplit, 0, rootDirectory);
+ if (answerTag != null)
+ {
+ var result = await Client.ReadAsync