Esempio n. 1
0
        static internal void ReadPowerMilliWattConverter(ref byte[] dataPacketReceived, out byte[] interpretedDataPacket)
        {
            // Step 1: Interpret the received data
            // Based on the linear conversion model in the PMBus spec
            byte       dataHighByte;
            byte       dataLowByte;
            int        NinTwosComplement;
            int        YinTowsComplement;
            int        N;
            int        Y;
            int        poutReadingInMilliWatts;
            const int  bitCountOfN      = 5;
            const int  bitCountOfY      = 11;
            const byte numMSBsToEncodeN = bitCountOfN;

            dataHighByte = dataPacketReceived[1];
            dataLowByte  = dataPacketReceived[0];

            NinTwosComplement = dataHighByte >> (8 - numMSBsToEncodeN);
            YinTowsComplement = (BitwiseOperationUtil.MaskOffMSBs(dataHighByte, numMSBsToEncodeN) << 8) | dataLowByte;

            N = NumberConversionUtil.ConvertFromTwosComplement(NinTwosComplement, bitCountOfN);
            Y = NumberConversionUtil.ConvertFromTwosComplement(YinTowsComplement, bitCountOfY);

            // Power (milliwatts) = Y * pow(2, N) * 1000.0
            poutReadingInMilliWatts = (int)((double)Y * Math.Pow(2, N) * 1000.0);
            interpretedDataPacket   = BitConverter.GetBytes(poutReadingInMilliWatts);
        }
        /// <summary>
        /// Convert the Linear Data Format based on the linear conversion model in the PMBus spec Rev. 1.1 Section 7.1
        /// </summary>
        /// <param name="dataPacketReceived">The data packet received.</param>
        /// <param name="interpretedDataPacket">The converted data packet.</param>
        static internal void PmBusLinearDataFormatConverter(ref byte[] dataPacketReceived, out byte[] interpretedDataPacket)
        {
            // Step 1: Interpret the received data
            byte       dataHighByte;
            byte       dataLowByte;
            int        NinTwosComplement;
            int        YinTowsComplement;
            int        N;
            int        Y;
            int        convertedValue;
            const int  bitCountOfN      = 5;
            const int  bitCountOfY      = 11;
            const byte numMSBsToEncodeN = bitCountOfN;

            dataHighByte = dataPacketReceived[1];
            dataLowByte  = dataPacketReceived[0];

            NinTwosComplement = dataHighByte >> (8 - numMSBsToEncodeN);
            YinTowsComplement = (BitwiseOperationUtil.MaskOffMSBs(dataHighByte, numMSBsToEncodeN) << 8) | dataLowByte;

            N = NumberConversionUtil.ConvertFromTwosComplement(NinTwosComplement, bitCountOfN);
            Y = NumberConversionUtil.ConvertFromTwosComplement(YinTowsComplement, bitCountOfY);

            // Converted Value = Y * pow(2, N)
            convertedValue        = (int)((double)Y * Math.Pow(2, N));
            interpretedDataPacket = BitConverter.GetBytes(convertedValue);
        }