我创建了一个如下结构:
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public class NodRecord
{
[FieldOffset(0)]
public ushort Driver;
[FieldOffset(2)]
public ushort BaudRate;
[FieldOffset(4)]
public ushort EnquiryInterval;
[FieldOffset(6)]
public byte Protocol;
[FieldOffset(7)]
public ushort Delay;
[FieldOffset(9)]
public NodIPAddress IP_Addr_Other;
[FieldOffset(13)]
public ushort IP_Port_Other;
[FieldOffset(15)]
public ushort IP_Port_Own;
[FieldOffset(17)]
public ushort Application;
}
然后我用下面的代码阅读了它:
readBuffer = reader.ReadBytes(sizeType);
handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);
nodes = (NodRecord)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(NodRecord));
handle.Free();
我想知道在 x86、x64 和 cortex arm 处理器中一切正常,但在 Atmel ARM9 AT91SAM9G20 上的ushort
变量我得到了错误的值,例如第一个和第三个下一个字节值替换而不是第一个和第二个下一个字节,但是对于字节值一切都很好