public AUTHORITY_PACKET MakeAuthorityPacket(int nCh, SOURCE_NAME srcName, byte byCmdGroup, byte byCmdType, AUTHORITY_DATA authorityData) { //http://www.csharpstudy.com/DevNote/Article/10 AUTHORITY_PACKET authorityPacket = new AUTHORITY_PACKET(); //int size = Marshal.SizeOf(typeof(AUTHORITY_PACKET)); //byte[] buffer = new byte[size]; authorityPacket.bySTX = AuthorityDefine.PROTO_STX; //Buffer.BlockCopy(SourceName, 0, authorityPacket.srcName.name, 0, AuthorityDefine.SRC_NAME_LEN); //sprintf(authorityPacket.srcName.name, "%s", (CStringA)m_strSourceName); authorityPacket.srcName = srcName; authorityPacket.byCmdGroup = byCmdGroup; authorityPacket.byCmdType = byCmdType; authorityPacket.byETX = AuthorityDefine.PROTO_ETX; authorityPacket.authorityData = authorityData; //authorityPacket.authorityData = authorityData; //Buffer.BlockCopy(authorityPacket.authorityData.data, 0, authorityPacket.authorityData.data, 0, AuthorityDefine.AUTHORITY_DATA_LEN); //memcpy(&authorityPacket.authorityData, &authorityData, sizeof(AUTHORITY_DATA)); /* * IntPtr ptr = Marshal.AllocHGlobal(size); * * Marshal.StructureToPtr(authorityPacket, ptr, true); * Marshal.Copy(ptr, buffer, 0, size); */ return(authorityPacket); }
public static AUTHORITY_PACKET MakeAuthorityPacket(string srcName, byte byCmdGroup, byte byCmdType, string data) { //http://www.csharpstudy.com/DevNote/Article/10 AUTHORITY_PACKET authorityPacket = new AUTHORITY_PACKET(); authorityPacket.bySTX = AuthorityDefine.PROTO_STX; authorityPacket.srcName.name = new byte[AuthorityDefine.SRC_NAME_LEN]; byte[] myName = AuthorityCommUtil.StringToByte(srcName); Buffer.BlockCopy(myName, 0, authorityPacket.srcName.name, 0, myName.Length); authorityPacket.byCmdGroup = byCmdGroup; authorityPacket.byCmdType = byCmdType; authorityPacket.byETX = AuthorityDefine.PROTO_ETX; authorityPacket.authorityData.data = new byte[AuthorityDefine.AUTHORITY_DATA_LEN]; byte[] myData = AuthorityCommUtil.StringToByte(data); Buffer.BlockCopy(myData, 0, authorityPacket.authorityData.data, 0, myData.Length); return(authorityPacket); }