110,561
社区成员
发帖
与我相关
我的任务
分享
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct retu
{
public byte a;//数据头
public byte b;//下位机地址
public byte c;//命令
public char d;//状态
public float e;//力
public float f;//电压
public float g;//电流
public float h;//运动时间
public byte i;//数据长度
public byte j;//数据尾
}
static retu GetRetu( byte[] mbyte )
{
if (Marshal.SizeOf(typeof(retu)) == mbyte.Length) //<-- check their sizes do agree.
{
GCHandle gch = GCHandle.Alloc(mbyte, GCHandleType.Pinned);
retu r = (retu)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(retu));
gch.Free();
return r;
}
else
{
throw new Exception( "no luck, you need to check the manual for the retu struct" );
}
}