如何从字节接到插座阵列,以C#结构 [英] How to byte array received from socket, to C# structure
问题描述
我要开发一种通过TCP套接字读取网络设备的数据服务(C#),并转换为C#结构。
I have to Develop a Service (C#) which read data from Network Device via TCP Socket and convert this is C# structure.
我基于现有产品,老德尔福应用程序,它在做这一切的东西,我要在C#中迁移的逻辑。
I am basing on existing, old Delphi application which is doing all this stuff and I have to migrate logic in C#.
编辑:
我从原始数据结构的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));
有与不同类型的200多场德尔福盒装记录......它看起来像约
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)
要转换成字符串的文件(如SERIAL_NUMBER)也有这样的代码:
To convert string files (e.g. 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:
-
如何这个结构到C#转换?
- 我应该使用LayoutKind.Sequential
或LayoutKind.Explicit
,有或wiyhout[FieldOffset(N)]
属性?
- 我怎么也得申报的字节数组中目标C#结构:以固定
缓冲或使用[的MarshalAs(UnmanagedType.ByValArray。 ..)]
属性
How to convert this structure to c#? -- Should I use
LayoutKind.Sequential
orLayoutKind.Explicit
, with or wiyhout[FieldOffset(N)]
attribute? -- How I have to declare array of bytes in target c# structure: asfixed
buffer or using[MarshalAs(UnmanagedType.ByValArray...)]
attribute?
这是更好的方式来封送输入字节数组最后的C#结构:使用 Marshal.PtrToStructure
或GCHandle.Alloc(字节,GCHandleType.Pinned)+ AddrOfPinnedObject
?
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;
}
我不知道(也不能唯一现在测试没有Deplhi),是一个SERIAL_NUMBER场
在您的更新:。在原来,SERIAL_NUMBER只是一个空值终止字符串
After your update: in original, SERIAL_NUMBER is just a null-terminated string.
这篇关于如何从字节接到插座阵列,以C#结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!