Struct from C to C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ulmus
    New Member
    • May 2013
    • 2

    Struct from C to C#

    I receive data from usb in specific record structure (cant change it). I have method to read buffer to Byte[]. Simple struct union would do the job but c# is broken in that case.
    My struct in C is:

    Code:
    typedef union
    {
    	struct
    	{
    		int Value;
    		int ValueFromADC;
    		int Zero;
    		int Temperature;
    		int RefTemperature;
    
    		unsigned char Metal;
    		unsigned char Range;
    		unsigned char Year;
    		unsigned char Month;
    
    		unsigned char Day;
    		unsigned char Hour;
    		unsigned char Minute;
    		unsigned char Second;
    
    		unsigned char MemoryStatus;
    		unsigned char Bank;
    		unsigned char Cell;
    		unsigned char NC;//not used structure element
    	};
    	unsigned int Data[8];
    	unsigned char Bytes[32];
    }Value_t;
    I am trying write received data from usb into Byte[] struct field, and read from structure using proper fields. Union like above would do tha thing but c#....
    I tried almost everything, amongs the others, something like:

    Code:
        [StructLayout(LayoutKind.Explicit, Size = 32)]
        public struct Value_t
        {
            //////////////////////////////
            [FieldOffset(0)][MarshalAs(UnmanagedType.LPArray, SizeConst=32)]
            public Byte[] Bytes;
            //////////////////////////////
            [FieldOffset(0)]public Int32 Value;
            [FieldOffset(4)]public Int32 ValueFromADC;
            [FieldOffset(8)]public Int32 Zero;
            [FieldOffset(12)]public Int32 Temperature;
            [FieldOffset(16)]public Int32 RefTemperature;
            [FieldOffset(20)]public Byte Metal;
            [FieldOffset(21)]public Byte Range;
            [FieldOffset(22)]public Byte Year;
            [FieldOffset(23)]public Byte Month;
            [FieldOffset(24)]public Byte Day;
            [FieldOffset(25)]public Byte Hour;
            [FieldOffset(26)]public Byte Minute;
            [FieldOffset(27)]public Byte Second;
            [FieldOffset(28)]public Byte MemoryStatus;
            [FieldOffset(29)]public Byte Bank;
            [FieldOffset(30)]public Byte Cell;
            [FieldOffset(31)]public Byte NC;
        };
    It compiles but after running it complains:
    ".... because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field." sic!!! WTF?!

    Anybody has any piece of advice or solution for that thing?
    Last edited by acoder; May 23 '13, 11:52 AM. Reason: Please use [code] tags when posting code
  • ulmus
    New Member
    • May 2013
    • 2

    #2
    Finally i did this, it is not as fast as union but does what it should do :
    Code:
        public struct Wynik
        {
            public Byte[] Bytes;
            //////////////////////////////////////
            public Int32 Value          { get { return BitConverter.ToInt32(Bytes, 0); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 0); } }
            public Int32 ValueFromADC   { get { return BitConverter.ToInt32(Bytes, 4); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 4); } }
            public Int32 Zero           { get { return BitConverter.ToInt32(Bytes, 8); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 8); } }
            public Int32 Temperature    { get { return BitConverter.ToInt32(Bytes, 12); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 12); } }
            public Int32 RefTemperature { get { return BitConverter.ToInt32(Bytes, 16); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 16); } }
    
            public Byte Metal           { get { return Bytes[20]; } set { Bytes[20] = value; } }
            public Byte Range           { get { return Bytes[21]; } set { Bytes[21] = value; } }
            public Byte Year            { get { return Bytes[22]; } set { Bytes[22] = value; } }//BCD
            public Byte Month           { get { return Bytes[23]; } set { Bytes[23] = value; } }//BCD
            public Byte Day             { get { return Bytes[24]; } set { Bytes[24] = value; } }//BCD
            public Byte Hour            { get { return Bytes[25]; } set { Bytes[25] = value; } }//BCD
            public Byte Minute          { get { return Bytes[26]; } set { Bytes[26] = value; } }//BCD
            public Byte Second          { get { return Bytes[27]; } set { Bytes[27] = value; } }//BCD
            public Byte MemoryStatus    { get { return Bytes[28]; } set { Bytes[28] = value; } }
            public Byte Bank            { get { return Bytes[29]; } set { Bytes[29] = value; } }
            public Byte Cell            { get { return Bytes[30]; } set { Bytes[30] = value; } }
            public Byte NC              { get { return Bytes[31]; } set { Bytes[31] = value; } }//not used structure element
        };
    One thing more:
    how can i exchange
    Code:
    BitConverter.GetBytes(value).CopyTo(Bytes, 12);
    into something copying direct to proper offset of byte array?
    just opposite to:
    Code:
    BitConverter.ToInt32(Bytes, 12);
    instead of copying array into array, like i did.

    Comment

    Working...