我有以下Delphi代码:
type
RegbusReq2=packed record
Funct:char;
Device:char;
Device1:char;
Starting:integer;
Quantity:smallint;
_CRC:Word; //CRC
stroka:char;
end;
type
crcReg=packed record
buf:array[0..2] of byte;
value:array[0..5] of byte;
end;
type
myRB=record
case byte of
0:(data:RegbusReq2);
1:(Buff:crcReg);
end;
...
procedure TForm1.Button3Click(Sender: TObject);
var
DataReq:myRB;
Output:array[1..15] of Byte;
i:integer;
nomP:string;
st:string;
begin
cs1.Address:=edit5.Text;
cs1.Port := 6001;
typecon:=2;
DataReq.data.Funct:=chr(63);
DataReq.data.Device:=chr(48);
DataReq.data.Device1:=chr(49);
DataReq.data.Starting:=768;
DataReq.data.Quantity:=7;
DataReq.data._CRC:=CRC2(@DataReq.Buff.value,6);
memo1.Lines.Add(IntToStr(DataReq.data._CRC));
DataReq.data.stroka:=chr(13);
application.ProcessMessages();
cs1.Active:=true;
cs1.Socket.SendBuf(DataReq.data,SizeOf(DataReq.data));
application.ProcessMessages();
cs1.Socket.ReceiveBuf(output,SizeOf(output));
application.ProcessMessages();
cs1.Active:=false;
application.ProcessMessages();
if output[1]<>62 then begin
showmessage('îøèáêà ñâÿçè');
exit;
end;
for i:=10 to 15 do
begin
nomp:= nomp + chr(Output[i]);
st:=st + '_' + inttostr(output[i]);
end;
memo1.Lines.Add(inttostr(sizeof(DataReq.data)));
memo1.Lines.Add(st);
memo1.Lines.Add(DataReq.data.Funct);
form1.Caption:=nomp;
Button1.Enabled:=true;
end;
此代码正常工作,在Delphi我看: http://i48.tinypic.com/ei6ph5.png
在C#中,我开始使用以下struct结构代码:
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Size=12)]
struct RegBusRec
{
[FieldOffset(0)]
public char Funct;
[FieldOffset(1)]
public char Device;
[FieldOffset(2)]
public char Device1;
[FieldOffset(6)]
public uint Starting;
[FieldOffset(8)]
public ushort Quantity;
[FieldOffset(10)]
public uint _CRC;
[FieldOffset(11)]
public char Message;
}
我尝试使用代码将srtuct转换为字节数组:
public static byte[] Serialize(object obj)
{
Type objectType = obj.GetType();
int objectSize = Marshal.SizeOf(obj);
IntPtr buffer = Marshal.AllocHGlobal(objectSize);
Marshal.StructureToPtr(obj, buffer, false);
byte[] array = new byte[objectSize];
Marshal.Copy(buffer, array, 0, objectSize);
Marshal.FreeHGlobal(buffer);
return array;
}
并发送:
System.Net.Sockets.TcpClient cl = new System.Net.Sockets.TcpClient();
cl.Connect(IPAddress.Parse("xxx.xxx.xxx.xxx"), 6001);
if (cl.Connected)
{
RegBusRec req2 = new RegBusRec();
req2.Funct = '?';
req2.Device = '0';
req2.Device = '1';
req2.Starting = 768;
req2.Quantity = 7;
req2.Message = '\r';
byte[] data = Serialize(req2);
cl.Client.Send(data);
cl.Client.Receive(output);
if (output[0] != 62)
{
Console.WriteLine("Connection error!");
Console.ReadLine();
}
}
但我从设备收到错误的消息。需要纠正将Delphi代码转换为C#。提前谢谢。
答案 0 :(得分:5)
你的结构错了。它应该是:
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
struct RegBusRec
{
public char Funct;
public char Device;
public char Device1;
public int Starting;
public short Quantity;
public ushort _CRC;
public char Message;
}
Delphi Integer
与C#int
匹配。 Delphi Smallint
与C#short
匹配。 Delphi Word
与C#ushort
匹配。
您使用明确的布局只会让您的生活变得艰难。使用顺序和Pack
属性来简化问题。
在初始化您编写的结构的代码中:
req2.Device = '0';
req2.Device = '1';
我希望你打算写
req2.Device = '0';
req2.Device1 = '1';
我没有检查任何其他内容,可能还有其他错误,我没有找到。如果我是你,我会添加诊断代码,逐字节地发出你的序列化结构,这样你就可以确定你正确地序列化了它。