如果你的包是基于this,我不会建议使用char
表示字节,因为在C char
#是16位数值(有序)值。相反,我建议对于8位无符号值使用byte
,对于16位无符号值,推荐使用UInt16
。然后,你可以这样做:
[StructLayout(LayoutKind.Sequential)]
public struct ModBusPacket
{
// http://en.wikipedia.org/wiki/Modbus#Frame_format
// The byte order is Big-Endian (first byte contains MSB).
public const bool IsLittleEndian = false;
public UInt16 TransactionIdentifier;
public UInt16 ProtocolIdentifier;
public UInt16 Length;
public byte UnitIdentifier;
public byte FunctionCode;
public byte[] Data;
static int PostIncrement(ref int index, int inc)
{
int old = index;
index += inc;
return old;
}
static byte[] ElementArray(byte[] buffer, ref byte[] swapBuffer, ref int index, int size)
{
if (swapBuffer == null || swapBuffer.Length < size)
Array.Resize(ref swapBuffer, size);
Array.Copy(buffer, PostIncrement(ref index, size), swapBuffer, 0, size);
if (BitConverter.IsLittleEndian != IsLittleEndian)
Array.Reverse(swapBuffer);
return swapBuffer;
}
public ModBusPacket(byte[] buffer)
{
int pos = 0;
byte[] swapBuffer = null;
TransactionIdentifier = (buffer.Length >= pos + 2 ? BitConverter.ToUInt16(ElementArray(buffer, ref swapBuffer, ref pos, 2), 0) : (UInt16)0);
ProtocolIdentifier = (buffer.Length >= pos + 2 ? BitConverter.ToUInt16(ElementArray(buffer, ref swapBuffer, ref pos, 2), 0) : (UInt16)0);
Length = (buffer.Length >= pos + 2 ? BitConverter.ToUInt16(ElementArray(buffer, ref swapBuffer, ref pos, 2), 0) : (UInt16)0);
UnitIdentifier = (buffer.Length >= pos + 1 ? buffer[PostIncrement(ref pos, 1)] : (byte)0);
FunctionCode = (buffer.Length >= pos + 1 ? buffer[PostIncrement(ref pos, 1)] : (byte)0);
var length = Math.Max(buffer.Length - pos, 0);
Data = new byte[length];
if (length > 0)
Array.Copy(buffer, pos, Data, 0, length);
}
public override string ToString()
{
return ObjectExtensions.ToStringWithReflection(this);
}
}
public static class ObjectExtensions
{
public static string ToStringWithReflection<T>(this T obj)
{
if (obj == null)
return string.Empty;
var type = obj.GetType();
var fields = type.GetFields();
var properties = type.GetProperties().Where(p => p.GetIndexParameters().Length == 0 && p.GetGetMethod(true) != null);
var values = new List<KeyValuePair<string, object>>();
Array.ForEach(fields, (field) => values.Add(new KeyValuePair<string, object>(field.Name, field.GetValue(obj))));
foreach (var property in properties)
if (property.CanRead)
values.Add(new KeyValuePair<string, object>(property.Name, property.GetValue(obj, null)));
return values.Aggregate(new StringBuilder(), (s, pair) => (s.Length == 0 ? s.Append("{").Append(obj.GetType().Name).Append(": ") : s.Append("; ")).Append(pair)).Append("}").ToString();
}
}
已经这样做了,在即时窗口中,您可以在即时窗口中键入buffer.ToPacket()
或监视窗口,看到格式化的数据。或者你可以使用conversion operator来将你的字节数组转换为ModBusPacket
,如果这会更有吸引力。
来源
2014-12-01 19:04:16
dbc
看看'元帅'级。 – SLaks 2014-12-01 16:02:53
如果您显示“Struct”的结构以及与您的问题和/或问题有关的所有相关代码,它确实有帮助。我们不能看到你所看到 – MethodMan 2014-12-01 16:03:01
如果你的'ModBusPacket'有构造函数/帮手从数组创建 - 您可以使用即时窗口来构造对象('新ModBusPacket(response_buffer)'),看看它的属性。 – 2014-12-01 16:03:02