2014-08-01 update 6 Add comment
This commit is contained in:
@@ -4,10 +4,13 @@ using System.Reflection;
|
||||
|
||||
namespace ModBus.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// 基本协议
|
||||
/// </summary>
|
||||
public abstract class BaseProtocal
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送数据
|
||||
/// 发送数据
|
||||
/// </summary>
|
||||
/// <param name="content">需要发送的数据</param>
|
||||
/// <returns>数据是否正确接收</returns>
|
||||
@@ -18,6 +21,11 @@ namespace ModBus.Net
|
||||
Protocals = new Dictionary<string, ProtocalUnit>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 协议索引器,这是一个懒加载协议,当字典中不存在协议时自动加载协议,否则调用已经加载的协议
|
||||
/// </summary>
|
||||
/// <param name="protocalName">协议的类的名称</param>
|
||||
/// <returns></returns>
|
||||
public ProtocalUnit this[string protocalName]
|
||||
{
|
||||
get
|
||||
@@ -26,6 +34,7 @@ namespace ModBus.Net
|
||||
{
|
||||
return Protocals[protocalName];
|
||||
}
|
||||
//自动寻找存在的协议并将其加载
|
||||
var protocalUnit =
|
||||
Assembly.Load("ModBus.Net").CreateInstance("ModBus.Net." + protocalName) as ProtocalUnit;
|
||||
if (protocalUnit == null) throw new InvalidCastException("没有相应的协议内容");
|
||||
@@ -36,24 +45,29 @@ namespace ModBus.Net
|
||||
|
||||
protected Dictionary<string, ProtocalUnit> Protocals { get; private set; }
|
||||
|
||||
public void Register(ProtocalUnit linkProtocal)
|
||||
protected void Register(ProtocalUnit linkProtocal)
|
||||
{
|
||||
if (linkProtocal == null) return;
|
||||
Protocals.Add(linkProtocal.GetType().Name, linkProtocal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送协议内容并接收,一般方法
|
||||
/// </summary>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public virtual byte[] SendReceive(params object[] content)
|
||||
{
|
||||
int t;
|
||||
return _protocalLinker.SendReceive(ProtocalUnit.TranslateContent(content));
|
||||
}
|
||||
|
||||
public virtual OutputStruct SendReceive(ProtocalUnit unit, params object[] content)
|
||||
{
|
||||
int t = 0;
|
||||
return unit.Unformat(_protocalLinker.SendReceive(unit.Format(content)), ref t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送协议,通过传入需要使用的协议内容和输入结构
|
||||
/// </summary>
|
||||
/// <param name="unit"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public virtual OutputStruct SendReceive(ProtocalUnit unit, InputStruct content)
|
||||
{
|
||||
int t = 0;
|
||||
@@ -61,14 +75,22 @@ namespace ModBus.Net
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收数据
|
||||
/// 仅发送数据
|
||||
/// </summary>
|
||||
/// <returns>接收到的数据</returns>
|
||||
/// <param name="unit"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool SendOnly(ProtocalUnit unit, params object[] content)
|
||||
{
|
||||
return _protocalLinker.SendOnly(unit.Format(content));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 仅发送数据
|
||||
/// </summary>
|
||||
/// <param name="unit"></param>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public virtual bool SendOnly(ProtocalUnit unit, InputStruct content)
|
||||
{
|
||||
return _protocalLinker.SendOnly(unit.Format(content));
|
||||
|
||||
Reference in New Issue
Block a user