From f6845154464ec4959d7273cf1a7d3db56282d365 Mon Sep 17 00:00:00 2001 From: luosheng Date: Fri, 26 May 2023 05:51:17 +0800 Subject: [PATCH] OPC EntryPoint Fix --- Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs | 74 -------------------- Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs | 18 ----- Modbus.Net/Modbus.Net.OPC/OpcMachine.cs | 35 +++------ Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs | 74 -------------------- Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs | 18 ----- Modbus.Net/Modbus.Net.OPC/OpcUtility.cs | 68 +++++++++++++++++- Modbus.Net/Modbus.Net/Machine/BaseMachine.cs | 4 +- 7 files changed, 79 insertions(+), 212 deletions(-) delete mode 100644 Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs delete mode 100644 Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs delete mode 100644 Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs delete mode 100644 Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs deleted file mode 100644 index 53eb504..0000000 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Modbus.Net.OPC -{ - /// - /// Opc DA设备 - /// - /// 设备Id类型 - /// 设备包含的地址的Id类型 - public class OpcDaMachine : OpcMachine where TKey : IEquatable - where TUnitKey : IEquatable - { - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - /// 是否保持连接 - /// 是否开启正则匹配 - public OpcDaMachine(TKey id, string connectionString, IEnumerable> getAddresses, bool keepConnect, bool isRegexOn = false) - : base(id, getAddresses, keepConnect) - { - BaseUtility = new OpcDaUtility(connectionString, isRegexOn); - ((OpcUtility)BaseUtility).GetSeperator += - () => ((AddressFormaterOpc)AddressFormater).Seperator; - } - - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - public OpcDaMachine(TKey id, string connectionString, IEnumerable> getAddresses) - : this(id, connectionString, getAddresses, false) - { - } - } - - /// - /// Opc DA设备 - /// - public class OpcDaMachine : OpcMachine - { - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - /// 是否保持连接 - /// 是否开启正则匹配 - public OpcDaMachine(string id, string connectionString, IEnumerable getAddresses, bool keepConnect, bool isRegexOn = false) - : base(id, getAddresses, keepConnect) - { - BaseUtility = new OpcDaUtility(connectionString, isRegexOn); - ((OpcUtility)BaseUtility).GetSeperator += - () => ((AddressFormaterOpc)AddressFormater).Seperator; - } - - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - public OpcDaMachine(string id, string connectionString, IEnumerable getAddresses) - : this(id, connectionString, getAddresses, false) - { - } - } -} \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs deleted file mode 100644 index a778185..0000000 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaUtility.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Modbus.Net.OPC -{ - /// - /// Opc Da协议Api入口 - /// - public class OpcDaUtility : OpcUtility - { - /// - /// 构造函数 - /// - /// 连接地址 - /// 是否开启正则匹配 - public OpcDaUtility(string connectionString, bool isRegexOn = false) : base(connectionString) - { - Wrapper = new OpcDaProtocol(ConnectionString, isRegexOn); - } - } -} \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs index dd0d00d..d83a929 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs @@ -4,41 +4,28 @@ using System.Collections.Generic; namespace Modbus.Net.OPC { /// - /// Opc Da设备 + /// Opc设备 /// - public abstract class OpcMachine : BaseMachine where TKey : IEquatable + public class OpcMachine : BaseMachine where TKey : IEquatable where TUnitKey : IEquatable { /// /// 构造函数 /// /// 设备的ID号 + /// 连接类型 + /// 连接地址 /// 需要读写的地址 - /// 是否保持连接 - protected OpcMachine(TKey id, IEnumerable> getAddresses, bool keepConnect) - : base(id, getAddresses, keepConnect) + /// 开启正则匹配 + protected OpcMachine(TKey id, OpcType connectionType, string connectionString, IEnumerable> getAddresses, bool isRegexOn = false) + : base(id, getAddresses, true) { + BaseUtility = new OpcUtility(connectionType, connectionString, isRegexOn); + AddressFormater = new AddressFormaterOpc((machine, unit) => { return new string[] { unit.Area }; }, this); + ((OpcUtility)BaseUtility).GetSeperator += + () => ((AddressFormaterOpc)AddressFormater).Seperator; AddressCombiner = new AddressCombinerSingle(); AddressCombinerSet = new AddressCombinerSingle(); } } - - /// - /// Opc Da设备 - /// - public abstract class OpcMachine : BaseMachine - { - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 需要读写的地址 - /// 是否保持连接 - protected OpcMachine(string id, IEnumerable getAddresses, bool keepConnect) - : base(id, getAddresses, keepConnect) - { - AddressCombiner = new AddressCombinerSingle(); - AddressCombinerSet = new AddressCombinerSingle(); - } - } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs deleted file mode 100644 index e829614..0000000 --- a/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Modbus.Net.OPC -{ - /// - /// Opc UA设备 - /// - /// 设备Id的类型 - /// 设备中地址的Id的类型 - public class OpcUaMachine : OpcMachine where TKey : IEquatable - where TUnitKey : IEquatable - { - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - /// 是否保持连接 - /// 是否开启正则匹配 - public OpcUaMachine(TKey id, string connectionString, IEnumerable> getAddresses, bool keepConnect, bool isRegexOn = false) - : base(id, getAddresses, keepConnect) - { - BaseUtility = new OpcUaUtility(connectionString, isRegexOn); - ((OpcUtility)BaseUtility).GetSeperator += - () => ((AddressFormaterOpc)AddressFormater).Seperator; - } - - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - public OpcUaMachine(TKey id, string connectionString, IEnumerable> getAddresses) - : this(id, connectionString, getAddresses, false) - { - } - } - - /// - /// Opc UA设备 - /// - public class OpcUaMachine : OpcMachine - { - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - /// 是否保持连接 - /// 是否开启正则匹配 - public OpcUaMachine(string id, string connectionString, IEnumerable getAddresses, bool keepConnect, bool isRegexOn = false) - : base(id, getAddresses, keepConnect) - { - BaseUtility = new OpcUaUtility(connectionString, isRegexOn); - ((OpcUtility)BaseUtility).GetSeperator += - () => ((AddressFormaterOpc)AddressFormater).Seperator; - } - - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接地址 - /// 需要读写的数据 - public OpcUaMachine(string id, string connectionString, IEnumerable getAddresses) - : this(id, connectionString, getAddresses, false) - { - } - } -} \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs deleted file mode 100644 index c1f4aae..0000000 --- a/Modbus.Net/Modbus.Net.OPC/OpcUaUtility.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Modbus.Net.OPC -{ - /// - /// Opc Ua协议Api入口 - /// - public class OpcUaUtility : OpcUtility - { - /// - /// 构造函数 - /// - /// 连接地址 - /// 是否开启正则匹配 - public OpcUaUtility(string connectionString, bool isRegexOn = false) : base(connectionString) - { - Wrapper = new OpcUaProtocol(ConnectionString, isRegexOn); - } - } -} \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs b/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs index 48c4bf9..257f4f0 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcUtility.cs @@ -4,15 +4,61 @@ using System.Threading.Tasks; namespace Modbus.Net.OPC { + /// + /// Opc类型 + /// + public enum OpcType + { + /// + /// DA连接 + /// + Da=0, + /// + /// UA连接 + /// + Ua=1 + } + /// /// Opc通用Api入口 /// - public abstract class OpcUtility : BaseUtility, + public class OpcUtility : BaseUtility, PipeUnit, ProtocolUnit>> { private static readonly ILogger logger = LogProvider.CreateLogger(); + private OpcType _opcType; + + private bool IsRegexOn { get; set; } + + /// + /// 协议类型 + /// + public OpcType OpcType + { + get { return _opcType; } + set + { + _opcType = value; + switch (_opcType) + { + //Da协议 + case OpcType.Da: + { + Wrapper = new OpcDaProtocol(ConnectionString, IsRegexOn); + break; + } + //Ua协议 + case OpcType.Ua: + { + Wrapper = new OpcUaProtocol(ConnectionString, IsRegexOn); + break; + } + } + } + } + /// /// 获取分隔符 /// @@ -22,10 +68,28 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 连接类型 /// 连接地址 - protected OpcUtility(string connectionString) : base(0, 0) + /// 是否开启正则匹配 + public OpcUtility(int connectionType, string connectionString, bool isRegexOn = false) : base(0, 0) { + OpcType = (OpcType)connectionType; ConnectionString = connectionString; + IsRegexOn = isRegexOn; + AddressTranslator = new AddressTranslatorOpc(); + } + + /// + /// 构造函数 + /// + /// 连接类型 + /// 连接地址 + /// 是否开启正则匹配 + public OpcUtility(OpcType connectionType, string connectionString, bool isRegexOn = false) : base(0, 0) + { + OpcType = connectionType; + ConnectionString = connectionString; + IsRegexOn = isRegexOn; AddressTranslator = new AddressTranslatorOpc(); } diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs index c67b52d..8eb42b4 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs @@ -281,10 +281,10 @@ namespace Modbus.Net new ReturnUnit { DeviceValue = - Convert.ToDouble( + Math.Round(Convert.ToDouble( ValueHelper.GetInstance(BaseUtility.Endian) .GetValue(datas.Datas, ref localMainPos, ref localSubPos, - address.DataType)) * address.Zoom, + address.DataType)) * address.Zoom, address.DecimalPos), AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(), }); }