2014-08-01 update 6 Add comment
This commit is contained in:
@@ -7,21 +7,40 @@ namespace ModBus.Net
|
||||
{
|
||||
public abstract class ProtocalUnit : IProtocalFormatting
|
||||
{
|
||||
/// <summary>
|
||||
/// 格式化,将输入结构转换为字节数组
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public abstract byte[] Format(InputStruct message);
|
||||
|
||||
/// <summary>
|
||||
/// 格式化,将对象数组转换为字节数组
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public virtual byte[] Format(params object[] message)
|
||||
{
|
||||
return TranslateContent(message);
|
||||
}
|
||||
|
||||
public abstract OutputStruct Unformat(byte[] messageBytes, ref int pos);
|
||||
/// <summary>
|
||||
/// 结构化,将字节数组转换为输出结构。
|
||||
/// </summary>
|
||||
/// <param name="messageBytes"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public abstract OutputStruct Unformat(byte[] messageBytes, ref int pos);
|
||||
|
||||
/// <summary>
|
||||
/// 转换静态方法,把对象数组转换为字节数组。
|
||||
/// </summary>
|
||||
/// <param name="contents"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] TranslateContent(params object[] contents)
|
||||
{
|
||||
bool b = false;
|
||||
//先查找传入的结构中有没有数组,有的话将其打开
|
||||
var newContentsList = new List<object>();
|
||||
foreach (object content in contents)
|
||||
{
|
||||
@@ -38,7 +57,9 @@ namespace ModBus.Net
|
||||
newContentsList.Add(content);
|
||||
}
|
||||
}
|
||||
//重新调用一边这个函数,这个传入的参数中一定没有数组。
|
||||
if (b) return TranslateContent(newContentsList.ToArray());
|
||||
//把参数一个一个翻译为相对应的字节,然后拼成一个队列
|
||||
var translateTarget = new List<byte>();
|
||||
foreach (object content in contents)
|
||||
{
|
||||
@@ -96,14 +117,21 @@ namespace ModBus.Net
|
||||
}
|
||||
}
|
||||
}
|
||||
//最后把队列转换为数组
|
||||
return translateTarget.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 输入结构
|
||||
/// </summary>
|
||||
public class InputStruct
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 输出结构
|
||||
/// </summary>
|
||||
public class OutputStruct
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user