using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace ModBus.Net { public abstract class ProtocalUnit : IProtocalFormatting { /// /// 格式化,将输入结构转换为字节数组 /// /// /// public abstract byte[] Format(InputStruct message); /// /// 格式化,将对象数组转换为字节数组 /// /// /// public virtual byte[] Format(params object[] message) { return TranslateContent(message); } /// /// 结构化,将字节数组转换为输出结构。 /// /// /// /// public abstract OutputStruct Unformat(byte[] messageBytes, ref int pos); /// /// 转换静态方法,把对象数组转换为字节数组。 /// /// /// public static byte[] TranslateContent(params object[] contents) { bool b = false; //先查找传入的结构中有没有数组,有的话将其打开 var newContentsList = new List(); foreach (object content in contents) { string t = content.GetType().ToString(); if (t.Substring(t.Length - 2, 2) == "[]") { b = true; IEnumerable contentArray = ArrayList.Adapter((Array)content).ToArray(typeof(object)).OfType(); newContentsList.AddRange(contentArray); } else { newContentsList.Add(content); } } //重新调用一边这个函数,这个传入的参数中一定没有数组。 if (b) return TranslateContent(newContentsList.ToArray()); //把参数一个一个翻译为相对应的字节,然后拼成一个队列 var translateTarget = new List(); foreach (object content in contents) { string t = content.GetType().ToString(); switch (t) { case "System.Int16": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((short)content)); break; } case "System.Int32": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((int)content)); break; } case "System.Int64": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((long)content)); break; } case "System.UInt16": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((ushort)content)); break; } case "System.UInt32": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((uint)content)); break; } case "System.UInt64": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((ulong)content)); break; } case "System.Single": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((float)content)); break; } case "System.Double": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((double)content)); break; } case "System.Byte": { translateTarget.AddRange(ValueHelper.Instance.GetBytes((byte)content)); break; } default: { throw new NotImplementedException("没有实现除整数以外的其它转换方式"); } } } //最后把队列转换为数组 return translateTarget.ToArray(); } } /// /// 输入结构 /// public class InputStruct { } /// /// 输出结构 /// public class OutputStruct { } }