2017-03-06 Update 1 Opc Update (Not test)
This commit is contained in:
@@ -6,7 +6,7 @@ namespace Modbus.Net.OPC
|
||||
/// <summary>
|
||||
/// Opc地址编码器
|
||||
/// </summary>
|
||||
public class AddressFormaterOpc : AddressFormater
|
||||
public class AddressFormaterOpc<TMachineKey,TUnitKey> : AddressFormater where TMachineKey : IEquatable<TMachineKey> where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// 协议构造器
|
||||
@@ -14,7 +14,7 @@ namespace Modbus.Net.OPC
|
||||
/// <param name="tagGeter">如何通过BaseMachine和AddressUnit构造Opc的标签</param>
|
||||
/// <param name="machine">调用这个编码器的设备</param>
|
||||
/// <param name="seperator">每两个标签之间用什么符号隔开,默认为/</param>
|
||||
public AddressFormaterOpc(Func<BaseMachine, AddressUnit, string[]> tagGeter, BaseMachine machine,
|
||||
public AddressFormaterOpc(Func<BaseMachine<TMachineKey, TUnitKey>, AddressUnit<TUnitKey>, string[]> tagGeter, BaseMachine<TMachineKey, TUnitKey> machine,
|
||||
char seperator = '/')
|
||||
{
|
||||
Machine = machine;
|
||||
@@ -22,9 +22,9 @@ namespace Modbus.Net.OPC
|
||||
Seperator = seperator;
|
||||
}
|
||||
|
||||
public BaseMachine Machine { get; set; }
|
||||
public BaseMachine<TMachineKey, TUnitKey> Machine { get; set; }
|
||||
|
||||
protected Func<BaseMachine, AddressUnit, string[]> TagGeter { get; set; }
|
||||
protected Func<BaseMachine<TMachineKey, TUnitKey>, AddressUnit<TUnitKey>, string[]> TagGeter { get; set; }
|
||||
|
||||
protected char Seperator { get; set; }
|
||||
|
||||
|
||||
42
Modbus.Net/Modbus.Net.OPC/ClientExtend.cs
Normal file
42
Modbus.Net/Modbus.Net.OPC/ClientExtend.cs
Normal file
@@ -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<T>(string tag);
|
||||
|
||||
void Write<T>(string tag, T item);
|
||||
|
||||
Task<T> ReadAsync<T>(string tag);
|
||||
|
||||
Task WriteAsync<T>(string tag, T item);
|
||||
|
||||
Task<Node> FindNodeAsync(string tag);
|
||||
|
||||
Task<IEnumerable<Node>> 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Read value full result
|
||||
/// </summary>
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a value on the specified opc tag
|
||||
/// </summary>
|
||||
/// <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>
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a tag asynchronusly
|
||||
/// </summary>
|
||||
public Task<OpcValueResult> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,19 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC.FBox
|
||||
{
|
||||
public class FBoxOpcDaMachine : OpcDaMachine
|
||||
public class FBoxOpcDaMachine : OpcDaMachine<string,string>
|
||||
{
|
||||
public string LocalSequence { get; set; }
|
||||
|
||||
public string LinkerName { get; set; }
|
||||
|
||||
public FBoxOpcDaMachine(string localSequence, string linkerName,
|
||||
IEnumerable<AddressUnit> getAddresses, bool keepConnect) : base(ConfigurationManager.FBoxOpcDaHost, getAddresses, keepConnect)
|
||||
IEnumerable<AddressUnit<string>> getAddresses, bool keepConnect) : base(ConfigurationManager.FBoxOpcDaHost, getAddresses, keepConnect)
|
||||
{
|
||||
LocalSequence = localSequence;
|
||||
LinkerName = linkerName;
|
||||
AddressFormater =
|
||||
new AddressFormaterOpc(
|
||||
new AddressFormaterOpc<string,string>(
|
||||
(machine, unit) =>
|
||||
new string[]
|
||||
{
|
||||
|
||||
@@ -31,33 +31,26 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="h-opc, Version=0.6.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\h-opc.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Reference Include="h-opc, Version=0.8.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\h-opc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Opc.Ua.Client, Version=1.2.334.4, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\Opc.Ua.Client.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\Opc.Ua.Client.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Opc.Ua.Configuration, Version=1.2.334.4, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\Opc.Ua.Configuration.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\Opc.Ua.Configuration.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Opc.Ua.Core, Version=1.2.334.4, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\Opc.Ua.Core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\Opc.Ua.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OpcComRcw, Version=2.0.105.1, Culture=neutral, PublicKeyToken=9a40e993cbface53, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\OpcComRcw.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\OpcComRcw.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OpcNetApi, Version=2.1.105.1, Culture=neutral, PublicKeyToken=9a40e993cbface53, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\OpcNetApi.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\OpcNetApi.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OpcNetApi.Com, Version=2.1.105.1, Culture=neutral, PublicKeyToken=9a40e993cbface53, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\H.Opc.0.7.0\lib\OpcNetApi.Com.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<HintPath>..\packages\H.Opc.0.8.1\lib\OpcNetApi.Com.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
@@ -71,14 +64,22 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AddressFormaterOpc.cs" />
|
||||
<Compile Include="AddressTranslatorOpc.cs" />
|
||||
<Compile Include="DaClientExtend.cs" />
|
||||
<Compile Include="ClientExtend.cs" />
|
||||
<Compile Include="FBox\FBoxOpcDaManchine.cs" />
|
||||
<Compile Include="OpcConnector.cs" />
|
||||
<Compile Include="OpcDaConnector.cs" />
|
||||
<Compile Include="OpcDaMachine.cs" />
|
||||
<Compile Include="OpcDaProtocal.cs" />
|
||||
<Compile Include="OpcDaProtocalLinker.cs" />
|
||||
<Compile Include="OpcDaUtility.cs" />
|
||||
<Compile Include="OpcMachine.cs" />
|
||||
<Compile Include="OpcProtocal.cs" />
|
||||
<Compile Include="OpcUaConnector.cs" />
|
||||
<Compile Include="OpcUaMachine.cs" />
|
||||
<Compile Include="OpcUaProtocal.cs" />
|
||||
<Compile Include="OpcUaProtocalLinker.cs" />
|
||||
<Compile Include="OpcUaUtility.cs" />
|
||||
<Compile Include="OpcUtility.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
|
||||
<metadata>
|
||||
<id>Modbus.Net.OPC</id>
|
||||
<version>1.2.3</version>
|
||||
<version>1.2.4</version>
|
||||
<title>Modbus.Net.OPC</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian IoT Science Technology Co.,Ltd.</owners>
|
||||
@@ -14,7 +14,7 @@
|
||||
<tags>hardware communicate protocal OPC DA Delian</tags>
|
||||
<dependencies>
|
||||
<dependency id="Modbus.Net" version="1.2.3.2" />
|
||||
<dependency id="H.Opc" version="0.7.0" />
|
||||
<dependency id="H.Opc" version="0.8.1" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
<files>
|
||||
|
||||
167
Modbus.Net/Modbus.Net.OPC/OpcConnector.cs
Normal file
167
Modbus.Net/Modbus.Net.OPC/OpcConnector.cs
Normal file
@@ -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<bool> SendMsgWithoutReturnAsync(byte[] message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override byte[] SendMsg(byte[] message)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SendMsgAsync(message));
|
||||
}
|
||||
|
||||
public override async Task<byte[]> 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<object>(answerTag);
|
||||
return BigEndianValueHelper.Instance.GetBytes(result, result.GetType());
|
||||
}
|
||||
return Encoding.ASCII.GetBytes("NoData");
|
||||
}
|
||||
else
|
||||
{
|
||||
var index = 0;
|
||||
for (var i = 1; i < message.Length - 3; i++)
|
||||
{
|
||||
if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff &&
|
||||
message[i + 3] == 0x00)
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var index2 = 0;
|
||||
for (var i = index + 4; i < message.Length - 3; i++)
|
||||
{
|
||||
if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff &&
|
||||
message[i + 3] == 0x00)
|
||||
{
|
||||
index2 = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var tagBytes = new byte[index - 1];
|
||||
Array.Copy(message, 1, tagBytes, 0, tagBytes.Length);
|
||||
var tag = Encoding.UTF8.GetString(tagBytes);
|
||||
var typeBytes = new byte[index2 - index - 4];
|
||||
Array.Copy(message, index + 4, typeBytes, 0, typeBytes.Length);
|
||||
var type = Type.GetType(Encoding.UTF8.GetString(typeBytes));
|
||||
var valueBytes = new byte[message.Length - index2 - 4];
|
||||
Array.Copy(message, index2 + 4, valueBytes, 0, valueBytes.Length);
|
||||
int mainpos = 0, subpos = 0;
|
||||
var value = BigEndianValueHelper.Instance.GetValue(valueBytes, ref mainpos, ref subpos, type);
|
||||
await Client.WriteAsync(tag, value);
|
||||
return new byte[] { 1 };
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//AddInfo("opc client exception:" + e);
|
||||
return Encoding.ASCII.GetBytes("NoData");
|
||||
//return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> SearchTag(string[] tags, int deep, IEnumerable<Node> nodes)
|
||||
{
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
var currentTag = node.Tag.Substring(node.Tag.LastIndexOf('/'));
|
||||
if (Regex.IsMatch(currentTag, tags[deep]))
|
||||
{
|
||||
if (deep == tags.Length) return currentTag;
|
||||
var subDirectories = await Client.ExploreFolderAsync(node.Tag);
|
||||
var answerTag = await SearchTag(tags, deep + 1, subDirectories);
|
||||
if (answerTag != null) return answerTag;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void AddInfo(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
public override bool Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
Client.Connect();
|
||||
_connect = true;
|
||||
AddInfo("client connected.");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddInfo("client connected exception: " + ex.Message);
|
||||
AddInfo("connect failed.");
|
||||
_connect = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<bool> ConnectAsync()
|
||||
{
|
||||
return Task.FromResult(Connect());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using Hylasoft.Opc.Common;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
@@ -8,21 +10,15 @@ namespace Modbus.Net.OPC
|
||||
/// <summary>
|
||||
/// OpcDa协议连接实现
|
||||
/// </summary>
|
||||
public class OpcDaConnector : BaseConnector
|
||||
public class OpcDaConnector : OpcConnector
|
||||
{
|
||||
protected static Dictionary<string, OpcDaConnector> _instances = new Dictionary<string, OpcDaConnector>();
|
||||
|
||||
protected bool _connect;
|
||||
protected MyDaClient _daClient;
|
||||
|
||||
protected OpcDaConnector(string host)
|
||||
protected OpcDaConnector(string host) : base(host)
|
||||
{
|
||||
ConnectionToken = host;
|
||||
Client = new MyDaClient(new Uri(ConnectionToken));
|
||||
}
|
||||
|
||||
public override string ConnectionToken { get; }
|
||||
public override bool IsConnected => _connect;
|
||||
|
||||
public static OpcDaConnector Instance(string host)
|
||||
{
|
||||
if (!_instances.ContainsKey(host))
|
||||
@@ -32,130 +28,5 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
return _instances[host];
|
||||
}
|
||||
|
||||
public override bool Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
_daClient = new MyDaClient(new Uri(ConnectionToken));
|
||||
_daClient.Connect();
|
||||
_connect = true;
|
||||
AddInfo("client connected.");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddInfo("client connected exception: " + ex.Message);
|
||||
AddInfo("connect failed.");
|
||||
_connect = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<bool> ConnectAsync()
|
||||
{
|
||||
return Task.FromResult(Connect());
|
||||
}
|
||||
|
||||
public override bool Disconnect()
|
||||
{
|
||||
try
|
||||
{
|
||||
_daClient?.Dispose();
|
||||
_daClient = 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<bool> SendMsgWithoutReturnAsync(byte[] message)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override byte[] SendMsg(byte[] message)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SendMsgAsync(message));
|
||||
}
|
||||
|
||||
public override async Task<byte[]> 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 result = await _daClient.ReadAsync(tag);
|
||||
if (result.QualityGood)
|
||||
{
|
||||
return BigEndianValueHelper.Instance.GetBytes(result.Value, result.Value.GetType());
|
||||
}
|
||||
return Encoding.ASCII.GetBytes("NoData");
|
||||
}
|
||||
else
|
||||
{
|
||||
var index = 0;
|
||||
for (var i = 1; i < message.Length - 3; i++)
|
||||
{
|
||||
if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff &&
|
||||
message[i + 3] == 0x00)
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var index2 = 0;
|
||||
for (var i = index + 4; i < message.Length - 3; i++)
|
||||
{
|
||||
if (message[i] == 0x00 && message[i + 1] == 0xff && message[i + 2] == 0xff &&
|
||||
message[i + 3] == 0x00)
|
||||
{
|
||||
index2 = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
var tagBytes = new byte[index - 1];
|
||||
Array.Copy(message, 1, tagBytes, 0, tagBytes.Length);
|
||||
var tag = Encoding.UTF8.GetString(tagBytes);
|
||||
var typeBytes = new byte[index2 - index - 4];
|
||||
Array.Copy(message, index + 4, typeBytes, 0, typeBytes.Length);
|
||||
var type = Type.GetType(Encoding.UTF8.GetString(typeBytes));
|
||||
var valueBytes = new byte[message.Length - index2 - 4];
|
||||
Array.Copy(message, index2 + 4, valueBytes, 0, valueBytes.Length);
|
||||
int mainpos = 0, subpos = 0;
|
||||
var value = BigEndianValueHelper.Instance.GetValue(valueBytes, ref mainpos, ref subpos, type);
|
||||
await _daClient.WriteAsync(tag, value);
|
||||
return new byte[] {1};
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//AddInfo("opc client exception:" + e);
|
||||
return Encoding.ASCII.GetBytes("NoData");
|
||||
//return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddInfo(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,12 @@ using System.Collections.Generic;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa设备
|
||||
/// </summary>
|
||||
public class OpcDaMachine<TKey, TUnitKey> : BaseMachine<TKey, TUnitKey> where TKey : IEquatable<TKey>
|
||||
where TUnitKey : IEquatable<TUnitKey>
|
||||
public class OpcDaMachine<TKey, TUnitKey> : OpcMachine<TKey, TUnitKey> where TKey : IEquatable<TKey> where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
: base(connectionString, getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcDaUtility(connectionString);
|
||||
AddressCombiner = new AddressCombinerSingle<TUnitKey>();
|
||||
AddressCombinerSet = new AddressCombinerSingle<TUnitKey>();
|
||||
}
|
||||
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||
@@ -23,17 +17,12 @@ namespace Modbus.Net.OPC
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OpcDa设备
|
||||
/// </summary>
|
||||
public class OpcDaMachine : BaseMachine
|
||||
public class OpcDaMachine : OpcMachine
|
||||
{
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
: base(connectionString, getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcDaUtility(connectionString);
|
||||
AddressCombiner = new AddressCombinerSingle();
|
||||
AddressCombinerSet = new AddressCombinerSingle();
|
||||
}
|
||||
|
||||
public OpcDaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses)
|
||||
|
||||
@@ -6,53 +6,11 @@ namespace Modbus.Net.OPC
|
||||
/// <summary>
|
||||
/// Opc Da协议Api入口
|
||||
/// </summary>
|
||||
public class OpcDaUtility : BaseUtility
|
||||
public class OpcDaUtility : OpcUtility
|
||||
{
|
||||
public OpcDaUtility(string connectionString) : base(0, 0)
|
||||
public OpcDaUtility(string connectionString) : base(connectionString)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
AddressTranslator = new AddressTranslatorOpc();
|
||||
Wrapper = new OpcDaProtocal(ConnectionString);
|
||||
}
|
||||
|
||||
public override Endian Endian => Endian.BigEndianLsb;
|
||||
|
||||
public override void SetConnectionType(int connectionType)
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress);
|
||||
var readRequestOpcOutputStruct =
|
||||
await
|
||||
Wrapper.SendReceiveAsync(Wrapper[typeof (ReadRequestOpcProtocal)], readRequestOpcInputStruct) as
|
||||
ReadRequestOpcOutputStruct;
|
||||
return readRequestOpcOutputStruct?.GetValue;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
|
||||
{
|
||||
try
|
||||
{
|
||||
var writeRequestOpcInputStruct = new WriteRequestOpcInputStruct(startAddress, setContents[0]);
|
||||
var writeRequestOpcOutputStruct =
|
||||
await
|
||||
Wrapper.SendReceiveAsync(Wrapper[typeof (WriteRequestOpcProtocal)], writeRequestOpcInputStruct)
|
||||
as WriteRequestOpcOutputStruct;
|
||||
return writeRequestOpcOutputStruct?.WriteResult == true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Modbus.Net/Modbus.Net.OPC/OpcMachine.cs
Normal file
35
Modbus.Net/Modbus.Net.OPC/OpcMachine.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa设备
|
||||
/// </summary>
|
||||
public abstract class OpcMachine<TKey, TUnitKey> : BaseMachine<TKey, TUnitKey> where TKey : IEquatable<TKey>
|
||||
where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
public OpcMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
AddressCombiner = new AddressCombinerSingle<TUnitKey>();
|
||||
AddressCombinerSet = new AddressCombinerSingle<TUnitKey>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OpcDa设备
|
||||
/// </summary>
|
||||
public abstract class OpcMachine : BaseMachine
|
||||
{
|
||||
public OpcMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(getAddresses, keepConnect)
|
||||
{
|
||||
AddressCombiner = new AddressCombinerSingle();
|
||||
AddressCombinerSet = new AddressCombinerSingle();
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Modbus.Net/Modbus.Net.OPC/OpcUaConnector.cs
Normal file
34
Modbus.Net/Modbus.Net.OPC/OpcUaConnector.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Hylasoft.Opc.Common;
|
||||
using Hylasoft.Opc.Ua;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcDa协议连接实现
|
||||
/// </summary>
|
||||
public class OpcUaConnector : OpcConnector
|
||||
{
|
||||
protected static Dictionary<string, OpcUaConnector> _instances = new Dictionary<string, OpcUaConnector>();
|
||||
|
||||
protected OpcUaConnector(string host) : base(host)
|
||||
{
|
||||
Client = new MyUaClient(new Uri(ConnectionToken));
|
||||
}
|
||||
|
||||
public static OpcUaConnector Instance(string host)
|
||||
{
|
||||
if (!_instances.ContainsKey(host))
|
||||
{
|
||||
var connector = new OpcUaConnector(host);
|
||||
_instances.Add(host, connector);
|
||||
}
|
||||
return _instances[host];
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs
Normal file
36
Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
public class OpcUaMachine<TKey, TUnitKey> : OpcMachine<TKey, TUnitKey> where TKey : IEquatable<TKey> where TUnitKey : IEquatable<TUnitKey>
|
||||
{
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||
: base(connectionString, getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcUaUtility(connectionString);
|
||||
}
|
||||
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||
: this(connectionString, getAddresses, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class OpcUaMachine : OpcMachine
|
||||
{
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||
: base(connectionString, getAddresses, keepConnect)
|
||||
{
|
||||
BaseUtility = new OpcUaUtility(connectionString);
|
||||
}
|
||||
|
||||
public OpcUaMachine(string connectionString, IEnumerable<AddressUnit> getAddresses)
|
||||
: this(connectionString, getAddresses, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Modbus.Net/Modbus.Net.OPC/OpcUaProtocal.cs
Normal file
36
Modbus.Net/Modbus.Net.OPC/OpcUaProtocal.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// OpcUa协议
|
||||
/// </summary>
|
||||
public class OpcUaProtocal : OpcProtocal
|
||||
{
|
||||
private readonly string _host;
|
||||
private int _connectTryCount;
|
||||
|
||||
public OpcUaProtocal(string host)
|
||||
{
|
||||
_host = host;
|
||||
}
|
||||
|
||||
public override bool Connect()
|
||||
{
|
||||
return AsyncHelper.RunSync(ConnectAsync);
|
||||
}
|
||||
|
||||
public override async Task<bool> ConnectAsync()
|
||||
{
|
||||
_connectTryCount++;
|
||||
ProtocalLinker = new OpcUaProtocalLinker(_host);
|
||||
if (!await ProtocalLinker.ConnectAsync()) return false;
|
||||
_connectTryCount = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Modbus.Net/Modbus.Net.OPC/OpcUaProtocalLinker.cs
Normal file
32
Modbus.Net/Modbus.Net.OPC/OpcUaProtocalLinker.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc Da协议连接器
|
||||
/// </summary>
|
||||
public class OpcUaProtocalLinker : ProtocalLinker
|
||||
{
|
||||
public OpcUaProtocalLinker() : this(ConfigurationManager.OpcUaHost)
|
||||
{
|
||||
}
|
||||
|
||||
public OpcUaProtocalLinker(string host)
|
||||
{
|
||||
BaseConnector = OpcUaConnector.Instance(host);
|
||||
}
|
||||
|
||||
public override bool? CheckRight(byte[] content)
|
||||
{
|
||||
if (content != null && content.Length == 6 && Encoding.ASCII.GetString(content) == "NoData")
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return base.CheckRight(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs
Normal file
19
Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
/// <summary>
|
||||
/// Opc Da协议Api入口
|
||||
/// </summary>
|
||||
public class OpcUaUtility : OpcUtility
|
||||
{
|
||||
public OpcUaUtility(string connectionString) : base(connectionString)
|
||||
{
|
||||
Wrapper = new OpcUaProtocal(ConnectionString);
|
||||
}
|
||||
}
|
||||
}
|
||||
58
Modbus.Net/Modbus.Net.OPC/OpcUtility.cs
Normal file
58
Modbus.Net/Modbus.Net.OPC/OpcUtility.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Modbus.Net.OPC
|
||||
{
|
||||
public abstract class OpcUtility : BaseUtility
|
||||
{
|
||||
protected OpcUtility(string connectionString) : base(0, 0)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
AddressTranslator = new AddressTranslatorOpc();
|
||||
}
|
||||
|
||||
public override Endian Endian => Endian.BigEndianLsb;
|
||||
|
||||
public override void SetConnectionType(int connectionType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override async Task<byte[]> GetDatasAsync(string startAddress, int getByteCount)
|
||||
{
|
||||
try
|
||||
{
|
||||
var readRequestOpcInputStruct = new ReadRequestOpcInputStruct(startAddress);
|
||||
var readRequestOpcOutputStruct =
|
||||
await
|
||||
Wrapper.SendReceiveAsync(Wrapper[typeof(ReadRequestOpcProtocal)], readRequestOpcInputStruct) as
|
||||
ReadRequestOpcOutputStruct;
|
||||
return readRequestOpcOutputStruct?.GetValue;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<bool> SetDatasAsync(string startAddress, object[] setContents)
|
||||
{
|
||||
try
|
||||
{
|
||||
var writeRequestOpcInputStruct = new WriteRequestOpcInputStruct(startAddress, setContents[0]);
|
||||
var writeRequestOpcOutputStruct =
|
||||
await
|
||||
Wrapper.SendReceiveAsync(Wrapper[typeof(WriteRequestOpcProtocal)], writeRequestOpcInputStruct)
|
||||
as WriteRequestOpcOutputStruct;
|
||||
return writeRequestOpcOutputStruct?.WriteResult == true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”: :
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.2.3")]
|
||||
[assembly: AssemblyFileVersion("1.2.3")]
|
||||
[assembly: AssemblyVersion("1.2.4")]
|
||||
[assembly: AssemblyFileVersion("1.2.4")]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="H.Opc" version="0.7.0" targetFramework="net45" />
|
||||
<package id="H.Opc" version="0.8.1" targetFramework="net45" />
|
||||
</packages>
|
||||
@@ -114,6 +114,15 @@ namespace Modbus.Net {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找类似 opc.tcp://localhost/... 的本地化字符串。
|
||||
/// </summary>
|
||||
public static string OpcUaHost {
|
||||
get {
|
||||
return ResourceManager.GetString("OpcUaHost", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找类似 102 的本地化字符串。
|
||||
/// </summary>
|
||||
|
||||
@@ -135,6 +135,9 @@
|
||||
<data name="OpcDaHost" xml:space="preserve">
|
||||
<value>opcda://localhost/...</value>
|
||||
</data>
|
||||
<data name="OpcUaHost" xml:space="preserve">
|
||||
<value>opc.tcp://localhost/...</value>
|
||||
</data>
|
||||
<data name="SiemensPort" xml:space="preserve">
|
||||
<value>102</value>
|
||||
</data>
|
||||
|
||||
@@ -230,22 +230,7 @@ namespace Modbus.Net
|
||||
{
|
||||
try
|
||||
{
|
||||
//调试代码,调试时取消下面一下代码的注释,会同步调用获取数据。
|
||||
//var ans = machine.GetDatas();
|
||||
//设置Cancellation Token
|
||||
var cts = new CancellationTokenSource();
|
||||
//超时后取消任务
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(GetCycle));
|
||||
//读取数据
|
||||
var ans = await machine.GetDatasAsync(GetDataType).WithCancellation(cts.Token);
|
||||
if (!machine.IsConnected)
|
||||
{
|
||||
MoveMachineToUnlinked(machine.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveMachineToLinked(machine.Id);
|
||||
}
|
||||
var ans = await GetValue(machine);
|
||||
ReturnValues?.Invoke(new TaskReturnDef
|
||||
{
|
||||
MachineId = machine.Id,
|
||||
@@ -790,22 +775,7 @@ namespace Modbus.Net
|
||||
{
|
||||
try
|
||||
{
|
||||
//调试代码,调试时取消下面一下代码的注释,会同步调用获取数据。
|
||||
//var ans = machine.GetDatas();
|
||||
//设置Cancellation Token
|
||||
var cts = new CancellationTokenSource();
|
||||
//超时后取消任务
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(_getCycle));
|
||||
//读取数据
|
||||
var ans = await machine.GetDatasAsync(GetDataType).WithCancellation(cts.Token);
|
||||
if (!machine.IsConnected)
|
||||
{
|
||||
MoveMachineToUnlinked(machine.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveMachineToLinked(machine.Id);
|
||||
}
|
||||
var ans = await GetValue(machine);
|
||||
ReturnValues?.Invoke(new TaskReturnDef<TMachineKey>
|
||||
{
|
||||
MachineId = machine.Id,
|
||||
@@ -825,5 +795,26 @@ namespace Modbus.Net
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task<Dictionary<string, ReturnUnit>> GetValue(IMachineProperty<TMachineKey> machine)
|
||||
{
|
||||
//调试代码,调试时取消下面一下代码的注释,会同步调用获取数据。
|
||||
//var ans = machine.GetDatas();
|
||||
//设置Cancellation Token
|
||||
var cts = new CancellationTokenSource();
|
||||
//超时后取消任务
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(_getCycle));
|
||||
//读取数据
|
||||
var ans = await machine.GetDatasAsync(GetDataType).WithCancellation(cts.Token);
|
||||
if (!machine.IsConnected)
|
||||
{
|
||||
MoveMachineToUnlinked(machine.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveMachineToLinked(machine.Id);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user