简体   繁体   中英

How to byte array received from socket, to C# structure

I have to Develop a Service (C#) which read data from Network Device via TCP Socket and convert this is C# structure.

I am basing on existing, old Delphi application which is doing all this stuff and I have to migrate logic in C#.

EDITED: I got a snapshot from C-Source of original data-structure:

struct _RequestMsgStruct
{
    UCHAR           request_ver; //In DELPHI it is represented as Byte
    USHORT          mac_addr[3];    /* MAC Address */
    UINT            product_type; //In DELPHI - Cardinal
    UCHAR           supply_type; //In DELPHI - Byte
    short           reserved0; //In DELPHI - SmallInt
    UCHAR           oper_ver[4]; //In DELPHI - CARDINAL !!!
    USHORT          brd_id; //In DELPHI - WORD
    unsigned short  exp_id1; //In DELPHI - WORD

    //In DELPHI - string[15]; //Array [0..15] of char;
    UCHAR           serial_no[16]; /* Serial Number. 16th char have to be NULL */ 
    UCHAR           _name[32]; /* Name */ //Length of payload may vary //In DELPHI - string[31]

    float           data_avg; //In DELPHI - Single
    ULONG           key[5]; //In DELPHI - array [0..19] of Byte
}__attribute__ ((packed));

There is Delphi Packed record with over 200 fields of different types... it look approximately like:

  TREC_DATA = packed record
      ID            : Byte;
      MAC_ADDRESS   : array [0..5] of Byte;
      fieldCard     : cardinal;
      fieldSI       : SmallInt; 
      fieldW        : WORD;
      SERIAL_NUMBER : string[15]; //Array [0..15] of char;
      fieldSingle   : Single;
      fieldArrOfB   : array [0..19] of Byte;
    end;

To move byte array to structure in Delphi there is next code:

Move(inBytesArr[StartIdx], DelphiStruct, aMsgSize)

To convert string files (eg SERIAL_NUMBER) there is also such code:

var
  pc: Pchar;
...
pc := @inBytesArr[StartIdx + SerialN_Pos_Idx];
DelphiStruct.SERIAL_NUMBER := pc;

I having deal with such conversion for first time and I don't know from where to start:

  • How to convert this structure to c#? -- Should I use LayoutKind.Sequential or LayoutKind.Explicit , with or wiyhout [FieldOffset(N)] attribute? -- How I have to declare array of bytes in target c# structure: as fixed buffer or using [MarshalAs(UnmanagedType.ByValArray...)] attribute?

  • Which is better way to marshal input bytes array to final C# structure: using Marshal.PtrToStructure or GCHandle.Alloc(bytes, GCHandleType.Pinned) + AddrOfPinnedObject ?

Please help me, at least, to get start point in understating from where i need to start.

By default, Delphi's packed records align fields by single byte boundary.
Hence, you should use something like this:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct TREC_DATA
{
      public byte ID;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
      public byte[] MAC_ADDRESS;
      public uint fieldCard;
      public short fieldSI;
      public ushort fieldW;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
      public byte[] SERIAL_NUMBER;
      public float fieldSingle;
      [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
      public byte[] fieldArrOfB;    
} 

The only thing I'm not sure (and can't test now without Deplhi), is a SERIAL_NUMBER field.

After your update: in original, SERIAL_NUMBER is just a null-terminated string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM