C#, VS15, StructLayout

In C, I have the struct and union in embedded MCU

			union {
				uINT32 wSetSerialPolicy;
					uINT32 I2C:2;			        // Only for A4/A5 MUX.
					uINT32 SPI0:2;			// SPI0 Bus including SSEL
					uINT32 SPICS:2;			// SPI Chip Select
					uINT32 UART:2;			// UART (TX/RX)
					uINT32 OWI:2;			// OWI  (I/P Only)
					uINT32 SENT:2;			// Sent (I/P Only)
					uINT32 CAN:2;			// Can Bus

Open in new window

The above data will be transferred to C# window (cannot use C++) for remote adjustment. I had a look into StructLayout but unsure if I know what I'm doing.

The question
(1) How to read/write whole 32 bit ie wSetSerialPolicy?
(2) Is there way to do this to read/write whole word wSetSerialPolicyand do discrete adjustment at the bits level.
(3) Below is the code in C# bit unsure if they actually in sync with layout?,
(4) I like to see how to check bits layout this since debug shown sorted order but not in sync with layout.
(4) How to display binary in debug (is there other way to display for Console.WriteLine( "----INFO: {0}", wSetSerialPolicy.ToString() );
(5) I aware of BitValue and BitArray but unsure if this support 2 bits configuration.
(6) I aware of http://stackoverflow.com/questions/14464/bit-fields-in-c-sharp but looking for better way to do this.
(7) I open for solution

wPolicyMIxedStruct sPolicySerial;

        // ==================================================================Getter/Setter
        public UInt32? wPolicySerial { get; set; }

        [StructLayout(LayoutKind.Explicit, Size = 32, CharSet = CharSet.Ansi)]
        public class wPolicySerialStruct
            public ushort I2C;
            public ushort SPI0;
            public ushort SPICS;
            public ushort UART;
            public ushort OWI;
            public ushort SENT;
            public ushort CAN;

        // ==================================================================constructor
        public zEVKITPortPolicy()
            sPolicySerial = new wPolicySerialStruct();
            sPolicySerial.OWI= 3;
            sPolicySerial.CAN = 1;
            Debug.WriteLine("---INFO: "+sPolicySerial.ToString());

Open in new window

Richard PayneChief Technology EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
C# is not oriented toward that level of work. It is purely object oriented, so it is lacking when you start working at the system level. This is one of the reasons why drivers are still written in C or even in assembler in Windows.

For one, structures are small objects in a .NET environment. Microsoft recommends to limit them to 16 bits for performance reasons, and yours is definitively bigger than that.

Is there a reason why you cannot use C++? This seems to be more appropriate for the type of work that the comments in your structure seems to point to.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.