using System;
using System.Collections.Generic;
using System.Reflection;
namespace ModBus.Net
{
///
/// 基本协议
///
public abstract class BaseProtocal
{
///
/// 发送数据
///
/// 需要发送的数据
/// 数据是否正确接收
protected ProtocalLinker _protocalLinker;
protected BaseProtocal()
{
Protocals = new Dictionary();
}
///
/// 协议索引器,这是一个懒加载协议,当字典中不存在协议时自动加载协议,否则调用已经加载的协议
///
/// 协议的类的名称
///
public ProtocalUnit this[string protocalName]
{
get
{
if (Protocals.ContainsKey(protocalName))
{
return Protocals[protocalName];
}
//自动寻找存在的协议并将其加载
var protocalUnit =
Assembly.Load("ModBus.Net").CreateInstance("ModBus.Net." + protocalName) as ProtocalUnit;
if (protocalUnit == null) throw new InvalidCastException("没有相应的协议内容");
Register(protocalUnit);
return Protocals[protocalName];
}
}
protected Dictionary Protocals { get; private set; }
protected void Register(ProtocalUnit linkProtocal)
{
if (linkProtocal == null) return;
Protocals.Add(linkProtocal.GetType().Name, linkProtocal);
}
///
/// 发送协议内容并接收,一般方法
///
///
///
public virtual byte[] SendReceive(params object[] content)
{
int t;
return _protocalLinker.SendReceive(ProtocalUnit.TranslateContent(content));
}
///
/// 发送协议,通过传入需要使用的协议内容和输入结构
///
///
///
///
public virtual OutputStruct SendReceive(ProtocalUnit unit, InputStruct content)
{
int t = 0;
return unit.Unformat(_protocalLinker.SendReceive(unit.Format(content)), ref t);
}
///
/// 仅发送数据
///
///
///
///
public virtual bool SendOnly(ProtocalUnit unit, params object[] content)
{
return _protocalLinker.SendOnly(unit.Format(content));
}
///
/// 仅发送数据
///
///
///
///
public virtual bool SendOnly(ProtocalUnit unit, InputStruct content)
{
return _protocalLinker.SendOnly(unit.Format(content));
}
}
}