Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:44:41+00:00 2026-06-04T01:44:41+00:00

I have following Delphi code: type RegbusReq2=packed record Funct:char; Device:char; Device1:char; Starting:integer; Quantity:smallint; _CRC:Word;

  • 0

I have following Delphi code:

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;

This code is working and in Delphi I watch:
http://i48.tinypic.com/ei6ph5.png

In C# I’ve started with following code of 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;
        }

And I try to convert srtuct to byte array with code:

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;
        }

And send it:

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();
                }
}

But I get wrong message from device. Need to correct convert Delphi code to C#. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:44:42+00:00Added an answer on June 4, 2026 at 1:44 am

    Your struct is wrong. It should be:

    [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 matches C# int. Delphi Smallint matches C# short. Delphi Word matches C# ushort.

    Your use of explicit layout just makes life hard for you. Use sequential and the Pack attribute to simplify matters.

    In the code where you initialise a struct you write:

    req2.Device = '0';
    req2.Device = '1';
    

    I expect that you meant to write

    req2.Device = '0';
    req2.Device1 = '1';
    

    I’ve not checked anything else, and there may be other errors that I have not found. If I were you I would add diagnostics code to emit byte by byte your serialized struct so that you can be sure that you are serializing it correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Delphi code: destructor TXX_XXXX.Destroy; var i: Integer; begin if Assigned(Allocations)
I have the following code in a Delphi 2007 application: function TBaseCriteriaObject.RecursiveCount( ObjType: TBaseCriteriaObjectClass):
I have a Delphi DLL that contains the following types: type TStepModeType = (smSingle,
I have the following bit of Delphi 7 code to increment a TDateTime value
This is for Delphi Prism. Say, I have the following enum SET type that
I have a Delphi program which contains the following code: procedure TForm1.Shape1MouseDown(Sender: TObject; Button:
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have the following code. It looks ugly, if the value equals to one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.