public AGVComFrame(AGVFrameTypes type, object serialData) { if (serialData != null) { string data = JsonConvert.SerializeObject(serialData); byte[] bData = ASCIIEncoding.ASCII.GetBytes(data); Data = bData; Head = new SeerHead(type, (UInt16)bData.Length); } else { Head = new SeerHead(type, 0); Data = null; } }
public static AGVComFrame Parse(byte[] buf) { if (buf.Length < HeadLength) { return(null); } IntPtr ptr = Marshal.AllocHGlobal(HeadLength); AGVComFrame tmp = null; try { Marshal.Copy(buf, 0, ptr, HeadLength); SeerHead head = Marshal.PtrToStructure <SeerHead>(ptr); head.number = (UInt16)((short)IPAddress.HostToNetworkOrder((short)head.number)); head.length = (UInt32)((int)IPAddress.HostToNetworkOrder((int)head.length)); head.type = (UInt16)((short)IPAddress.HostToNetworkOrder((short)head.type)); tmp = new AGVComFrame(); if (head.type > TYPE_RESPONSE_OFFSET) { tmp.IsResponseFrame = true; head.type = (ushort)(head.type - TYPE_RESPONSE_OFFSET); } tmp.FrameType = (AGVFrameTypes)head.type; uint length = head.length; if (length <= buf.Length - HeadLength) { tmp.Data = new byte[length]; Array.Copy(buf, HeadLength, tmp.Data, 0, head.length); } else { tmp = null; } } finally { Marshal.FreeHGlobal(ptr); } return(tmp); }