From 156b8a370b374d8a09d8e59cee219eb323d847a1 Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Tue, 23 May 2017 17:32:50 +0800 Subject: [PATCH] Use Interfaces --- Modbus.Net/src/Base.Common/BaseProtocal.cs | 77 ++++++++++--------- Modbus.Net/src/Base.Common/BaseUtility.cs | 2 +- Modbus.Net/src/Base.Common/IProtocal.cs | 63 +++++++++++++-- Modbus.Net/src/Base.Common/IProtocalLinker.cs | 28 +++++++ Modbus.Net/src/Base.Common/ProtocalLinker.cs | 63 ++++++++------- 5 files changed, 160 insertions(+), 73 deletions(-) diff --git a/Modbus.Net/src/Base.Common/BaseProtocal.cs b/Modbus.Net/src/Base.Common/BaseProtocal.cs index 08473da..155ce91 100644 --- a/Modbus.Net/src/Base.Common/BaseProtocal.cs +++ b/Modbus.Net/src/Base.Common/BaseProtocal.cs @@ -38,7 +38,8 @@ namespace Modbus.Net /// 基本协议 /// public abstract class BaseProtocal : - IProtocal where TProtocalUnit : ProtocalUnit where TParamOut : class + IProtocal where TProtocalUnit : ProtocalUnit + where TParamOut : class { /// /// 构造器 @@ -67,9 +68,9 @@ namespace Modbus.Net public byte MasterAddress { get; set; } /// - /// 协议的连接器 + /// 协议集合 /// - public ProtocalLinker ProtocalLinker { get; protected set; } + protected Dictionary Protocals { get; } /// /// 协议索引器,这是一个懒加载协议,当字典中不存在协议时自动加载协议,否则调用已经加载的协议 @@ -85,7 +86,9 @@ namespace Modbus.Net lock (Protocals) { if (Protocals.ContainsKey(protocalName)) + { protocalUnitReturn = Protocals[protocalName]; + } else { //自动寻找存在的协议并将其加载 @@ -95,16 +98,39 @@ namespace Modbus.Net throw new InvalidCastException($"No ProtocalUnit {nameof(TProtocalUnit)} implemented"); protocalUnit.Endian = Endian; Register(protocalUnit); - } + } } return protocalUnitReturn ?? Protocals[protocalName]; } } /// - /// 协议集合 + /// 协议的连接器 /// - protected Dictionary Protocals { get; } + public IProtocalLinker ProtocalLinker { get; protected set; } + + /// + /// 协议连接开始 + /// + /// + public abstract bool Connect(); + + /// + /// 协议连接开始(异步) + /// + /// + public abstract Task ConnectAsync(); + + /// + /// 协议连接断开 + /// + /// + public virtual bool Disconnect() + { + if (ProtocalLinker != null) + return ProtocalLinker.Disconnect(); + return false; + } /// /// 发送协议,通过传入需要使用的协议内容和输入结构 @@ -133,31 +159,21 @@ namespace Modbus.Net /// /// 写入的内容,使用对象数组描述 /// 从设备获取的字节流 - public virtual byte[] SendReceive(params object[] content) + public virtual TParamOut SendReceive(params object[] content) { return AsyncHelper.RunSync(() => SendReceiveAsync(content)); } /// - /// 发送协议内容并接收,一般方法 + /// 发送协议内容并接收,一般方法(不能使用,如需使用请继承) /// /// 写入的内容,使用对象数组描述 /// 从设备获取的字节流 - public virtual Task SendReceiveAsync(params object[] content) + public virtual Task SendReceiveAsync(params object[] content) { throw new NotImplementedException(); } - /// - /// 注册一个协议 - /// - /// 需要注册的协议 - protected void Register(TProtocalUnit linkProtocal) - { - if (linkProtocal == null) return; - Protocals.Add(linkProtocal.GetType().FullName, linkProtocal); - } - /// /// 发送协议,通过传入需要使用的协议内容和输入结构 /// @@ -197,26 +213,13 @@ namespace Modbus.Net } /// - /// 协议连接开始 + /// 注册一个协议 /// - /// - public abstract bool Connect(); - - /// - /// 协议连接开始(异步) - /// - /// - public abstract Task ConnectAsync(); - - /// - /// 协议连接断开 - /// - /// - public virtual bool Disconnect() + /// 需要注册的协议 + protected void Register(TProtocalUnit linkProtocal) { - if (ProtocalLinker != null) - return ProtocalLinker.Disconnect(); - return false; + if (linkProtocal == null) return; + Protocals.Add(linkProtocal.GetType().FullName, linkProtocal); } } } \ No newline at end of file diff --git a/Modbus.Net/src/Base.Common/BaseUtility.cs b/Modbus.Net/src/Base.Common/BaseUtility.cs index 3b069dd..f7ede9e 100644 --- a/Modbus.Net/src/Base.Common/BaseUtility.cs +++ b/Modbus.Net/src/Base.Common/BaseUtility.cs @@ -49,7 +49,7 @@ namespace Modbus.Net /// /// 协议收发主体 /// - protected BaseProtocal Wrapper; + protected IProtocal Wrapper; /// /// 构造器 diff --git a/Modbus.Net/src/Base.Common/IProtocal.cs b/Modbus.Net/src/Base.Common/IProtocal.cs index 2bb48bd..0739ee7 100644 --- a/Modbus.Net/src/Base.Common/IProtocal.cs +++ b/Modbus.Net/src/Base.Common/IProtocal.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; namespace Modbus.Net { @@ -8,22 +9,52 @@ namespace Modbus.Net /// 向Connector传入的类型 /// 从Connector返回的类型 /// 协议单元的类型 - public interface IProtocal + public interface IProtocal where TProtocalUnit : IProtocalFormatting { /// - /// 发送协议内容并接收,一般方法 + /// 协议的连接器 /// - /// 写入的内容,使用对象数组描述 - /// 从设备获取的字节流 - byte[] SendReceive(params object[] content); + IProtocalLinker ProtocalLinker { get; } + + /// + /// 协议索引器,这是一个懒加载协议,当字典中不存在协议时自动加载协议,否则调用已经加载的协议 + /// + /// 协议的类的GetType + /// 协议的实例 + TProtocalUnit this[Type type] { get; } + + /// + /// 协议连接开始 + /// + /// + bool Connect(); + + /// + /// 协议连接开始(异步) + /// + /// + Task ConnectAsync(); + + /// + /// 协议连接断开 + /// + /// + bool Disconnect(); /// /// 发送协议内容并接收,一般方法 /// /// 写入的内容,使用对象数组描述 /// 从设备获取的字节流 - Task SendReceiveAsync(params object[] content); + TParamOut SendReceive(params object[] content); + + /// + /// 发送协议内容并接收,一般方法 + /// + /// 写入的内容,使用对象数组描述 + /// 从设备获取的字节流 + Task SendReceiveAsync(params object[] content); /// /// 发送协议,通过传入需要使用的协议内容和输入结构 @@ -40,5 +71,23 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 Task SendReceiveAsync(TProtocalUnit unit, IInputStruct content); + + /// + /// 发送协议,通过传入需要使用的协议内容和输入结构 + /// + /// 协议的实例 + /// 输入信息的结构化描述 + /// 输出信息的结构化描述 + /// IOutputStruct的具体类型 + T SendReceive(TProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct; + + /// + /// 发送协议,通过传入需要使用的协议内容和输入结构 + /// + /// 协议的实例 + /// 输入信息的结构化描述 + /// 输出信息的结构化描述 + /// IOutputStruct的具体类型 + Task SendReceiveAsync(TProtocalUnit unit, IInputStruct content) where T : class, IOutputStruct; } } \ No newline at end of file diff --git a/Modbus.Net/src/Base.Common/IProtocalLinker.cs b/Modbus.Net/src/Base.Common/IProtocalLinker.cs index eb1091a..2dde9e8 100644 --- a/Modbus.Net/src/Base.Common/IProtocalLinker.cs +++ b/Modbus.Net/src/Base.Common/IProtocalLinker.cs @@ -9,6 +9,34 @@ namespace Modbus.Net /// 从Connector返回的数据类型 public interface IProtocalLinker { + /// + /// 通讯字符串 + /// + string ConnectionToken { get; } + + /// + /// 设备是否连接 + /// + bool IsConnected { get; } + + /// + /// 连接设备 + /// + /// 设备是否连接成功 + bool Connect(); + + /// + /// 连接设备 + /// + /// 设备是否连接成功 + Task ConnectAsync(); + + /// + /// 断开设备 + /// + /// 设备是否断开成功 + bool Disconnect(); + /// /// 发送并接收数据 /// diff --git a/Modbus.Net/src/Base.Common/ProtocalLinker.cs b/Modbus.Net/src/Base.Common/ProtocalLinker.cs index 783e879..9c9716c 100644 --- a/Modbus.Net/src/Base.Common/ProtocalLinker.cs +++ b/Modbus.Net/src/Base.Common/ProtocalLinker.cs @@ -70,13 +70,41 @@ namespace Modbus.Net /// /// 基本的协议连接器 /// - public abstract class ProtocalLinker : IProtocalLinker where TParamOut : class + public abstract class ProtocalLinker : IProtocalLinker + where TParamOut : class { /// /// 传输连接器 /// protected BaseConnector BaseConnector; + /// + /// 连接设备 + /// + /// 设备是否连接成功 + public bool Connect() + { + return BaseConnector.Connect(); + } + + /// + /// 连接设备 + /// + /// 设备是否连接成功 + public async Task ConnectAsync() + { + return await BaseConnector.ConnectAsync(); + } + + /// + /// 断开设备 + /// + /// 设备是否断开成功 + public bool Disconnect() + { + return BaseConnector.Disconnect(); + } + /// /// 通讯字符串 /// @@ -151,40 +179,19 @@ namespace Modbus.Net /// /// 扩展前的基本协议内容 /// 扩展后的协议内容 - public abstract TParamIn BytesExtend(TParamIn content); + public virtual TParamIn BytesExtend(TParamIn content) + { + throw new NotImplementedException(); + } /// /// 协议内容缩减,接收时根据需要缩减 /// /// 缩减前的完整协议内容 /// 缩减后的协议内容 - public abstract TParamOut BytesDecact(TParamOut content); - - /// - /// 连接设备 - /// - /// 设备是否连接成功 - public bool Connect() + public virtual TParamOut BytesDecact(TParamOut content) { - return BaseConnector.Connect(); - } - - /// - /// 连接设备 - /// - /// 设备是否连接成功 - public async Task ConnectAsync() - { - return await BaseConnector.ConnectAsync(); - } - - /// - /// 断开设备 - /// - /// 设备是否断开成功 - public bool Disconnect() - { - return BaseConnector.Disconnect(); + throw new NotImplementedException(); } } } \ No newline at end of file