473,378 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Struct from C to C#

2
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:

Expand|Select|Wrap|Line Numbers
  1. typedef union
  2. {
  3.     struct
  4.     {
  5.         int Value;
  6.         int ValueFromADC;
  7.         int Zero;
  8.         int Temperature;
  9.         int RefTemperature;
  10.  
  11.         unsigned char Metal;
  12.         unsigned char Range;
  13.         unsigned char Year;
  14.         unsigned char Month;
  15.  
  16.         unsigned char Day;
  17.         unsigned char Hour;
  18.         unsigned char Minute;
  19.         unsigned char Second;
  20.  
  21.         unsigned char MemoryStatus;
  22.         unsigned char Bank;
  23.         unsigned char Cell;
  24.         unsigned char NC;//not used structure element
  25.     };
  26.     unsigned int Data[8];
  27.     unsigned char Bytes[32];
  28. }Value_t;
  29.  
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:

Expand|Select|Wrap|Line Numbers
  1.     [StructLayout(LayoutKind.Explicit, Size = 32)]
  2.     public struct Value_t
  3.     {
  4.         //////////////////////////////
  5.         [FieldOffset(0)][MarshalAs(UnmanagedType.LPArray, SizeConst=32)]
  6.         public Byte[] Bytes;
  7.         //////////////////////////////
  8.         [FieldOffset(0)]public Int32 Value;
  9.         [FieldOffset(4)]public Int32 ValueFromADC;
  10.         [FieldOffset(8)]public Int32 Zero;
  11.         [FieldOffset(12)]public Int32 Temperature;
  12.         [FieldOffset(16)]public Int32 RefTemperature;
  13.         [FieldOffset(20)]public Byte Metal;
  14.         [FieldOffset(21)]public Byte Range;
  15.         [FieldOffset(22)]public Byte Year;
  16.         [FieldOffset(23)]public Byte Month;
  17.         [FieldOffset(24)]public Byte Day;
  18.         [FieldOffset(25)]public Byte Hour;
  19.         [FieldOffset(26)]public Byte Minute;
  20.         [FieldOffset(27)]public Byte Second;
  21.         [FieldOffset(28)]public Byte MemoryStatus;
  22.         [FieldOffset(29)]public Byte Bank;
  23.         [FieldOffset(30)]public Byte Cell;
  24.         [FieldOffset(31)]public Byte NC;
  25.     };
  26.  
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?
May 23 '13 #1
1 1532
ulmus
2
Finally i did this, it is not as fast as union but does what it should do :
Expand|Select|Wrap|Line Numbers
  1.     public struct Wynik
  2.     {
  3.         public Byte[] Bytes;
  4.         //////////////////////////////////////
  5.         public Int32 Value          { get { return BitConverter.ToInt32(Bytes, 0); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 0); } }
  6.         public Int32 ValueFromADC   { get { return BitConverter.ToInt32(Bytes, 4); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 4); } }
  7.         public Int32 Zero           { get { return BitConverter.ToInt32(Bytes, 8); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 8); } }
  8.         public Int32 Temperature    { get { return BitConverter.ToInt32(Bytes, 12); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 12); } }
  9.         public Int32 RefTemperature { get { return BitConverter.ToInt32(Bytes, 16); } set { BitConverter.GetBytes(value).CopyTo(Bytes, 16); } }
  10.  
  11.         public Byte Metal           { get { return Bytes[20]; } set { Bytes[20] = value; } }
  12.         public Byte Range           { get { return Bytes[21]; } set { Bytes[21] = value; } }
  13.         public Byte Year            { get { return Bytes[22]; } set { Bytes[22] = value; } }//BCD
  14.         public Byte Month           { get { return Bytes[23]; } set { Bytes[23] = value; } }//BCD
  15.         public Byte Day             { get { return Bytes[24]; } set { Bytes[24] = value; } }//BCD
  16.         public Byte Hour            { get { return Bytes[25]; } set { Bytes[25] = value; } }//BCD
  17.         public Byte Minute          { get { return Bytes[26]; } set { Bytes[26] = value; } }//BCD
  18.         public Byte Second          { get { return Bytes[27]; } set { Bytes[27] = value; } }//BCD
  19.         public Byte MemoryStatus    { get { return Bytes[28]; } set { Bytes[28] = value; } }
  20.         public Byte Bank            { get { return Bytes[29]; } set { Bytes[29] = value; } }
  21.         public Byte Cell            { get { return Bytes[30]; } set { Bytes[30] = value; } }
  22.         public Byte NC              { get { return Bytes[31]; } set { Bytes[31] = value; } }//not used structure element
  23.     };
  24.  
  25.  
One thing more:
how can i exchange
Expand|Select|Wrap|Line Numbers
  1. BitConverter.GetBytes(value).CopyTo(Bytes, 12);
into something copying direct to proper offset of byte array?
just opposite to:
Expand|Select|Wrap|Line Numbers
  1. BitConverter.ToInt32(Bytes, 12);
instead of copying array into array, like i did.
May 24 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: manishie | last post by:
hi all, i'm running apache 2.0. here's my php.conf: LoadModule php4_module modules/libphp4.so AddType application/x-httpd-php .php .php4 .php3 .phtml .html DirectoryIndex index.php...
0
by: KathyB | last post by:
Further to my earlier message tonight, here is more info on my button issue. I have two controls: a button and a textbox. This code for the button is in the page load (if not postback): ...
4
by: Stephen Miller | last post by:
Hi, I am running v1.1.4322 on Win2K server and unable to debug a ASP.Net application running locally, using a full URL (ie www.mysite.com). When I hit F5, I get the following error message: ...
4
by: Amil Hanish | last post by:
Don't reply to this unless you know what you are talking about. Installed brand spanking new 2003 Server. Configured to be application server. Then installed Visual Studio .NET. Copied over...
4
by: JC | last post by:
Hello- I have a form which has a listbox, a textbox and a submit button. The user types a phone number into the textbox, clicks button, text gets added to the listbox. After this process I need...
4
by: LP | last post by:
Hi, My webservice is currently deployed on WIndows 2000 server and runs pretty fine. I am trying to run my webservice on a Windows 2003 server. My webservice tries to write to a eventlog. The...
3
by: nsh | last post by:
mailing.database.mysql, comp.lang.php subject: does "LOAD DATA" EVER work?!? I've tried EVERYTHING! version info: my isp is running my web page on a linux box with php ver. 4.4.1 according to...
4
by: sorcerdon | last post by:
Hello! I am looking for someone who has solved this multi-million people's problem. EVERYONE seems to ahve this problem. Im a creating a data set and populating it with a call to a store proc. ...
0
by: shapper | last post by:
Hello, I am creating a web site with nested master pages. Inside of one of this master pages I added a user control. I need to access an Asp.Net Panel which is inside Global.master from my...
3
by: IT Couple | last post by:
Hi Again the same subject. I have tried everything what is recommended what I could find and it is still so slow. I open the db file in the root of the network and it takes 2s to open the table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.