1
Reply

help with program structure

jacob lillie

jacob lillie

Mar 16 2010 10:12 AM
2.2k
Hi,

I'm pretty new to C# and I have a problem finding a good way to structure my program.
I'm reading bytes from the COM port, and each bit in the bytes can represent some parameter that I want to print to a string.
So far I have managed to extract the meaning of the different bits for a single byte but there are up to 100 bytes with different parameters, so I want to structure my program correctly from the beginning.
This is my code so far, its only purpose is to look at bits 5 & 6 and print which meaning they represent.

class PMBus
    {
        const byte CAPABILITY_MAX_BUS_SPEED_100kHz = 0X00;  //none of bits 5 & 6 are set
        const byte CAPABILITY_MAX_BUS_SPEED_400kHz = 0X20;  //bit 5 is set
        const byte CAPABILITY_MAX_BUS_SPEED_RESERVED0 = 0X40;   //bits 6 is set
        const byte CAPABILITY_MAX_BUS_SPEED_RESERVED1 = 0X60;   //bits 5-6 are both set

        const byte CAPABILITY_MAX_BUS_SPEED = 0X60; //bit 5 and bit 6 define bus speed
        const byte CAPABILITY_SMBALERT = 0X10;
        const byte CAPABILITY_RESERVED = 0X08;


        public void GetCapabilityBusSpeed(byte _byte)
        {
            byte result = (byte)(_byte & CAPABILITY_MAX_BUS_SPEED);
           
            switch(result)
            {
                case CAPABILITY_MAX_BUS_SPEED_100kHz:
                    {
                        Console.WriteLine("Max bus speed=100kHz");
                        break;
                    }
                case CAPABILITY_MAX_BUS_SPEED_400kHz:
                    {
                        Console.WriteLine("Max bus speed=400kHz");
                        break;
                    }
                case CAPABILITY_MAX_BUS_SPEED_RESERVED0:
                    {
                        Console.WriteLine("Reserved 0");
                        break;
                    }
                case CAPABILITY_MAX_BUS_SPEED_RESERVED1:
                    {
                        Console.WriteLine("Reserved 1");
                        break;
                    }
            }
           
        }
    }

With many bytes and different bit meanings I would like some ideas or proposals of how to structure the program in a better way.
Could I make a struct for each byte that could hold the bit addresses and output strings?
Thanks in advance :)

Jacob

Answers (1)