Windows CE battery status

0

Witam

czy ktoś próbował wczytać SystemInformation.PowerStatus.BatteryLifePercent.ToString() dla windowsa CE konkretnie dla czytnika kodów kreskowych motorola 2090

1

który windows? Dla motoroli masz dodatkowe API EMDT gdzie możesz odczytać stan karty wi-fi (włączona, poziom sygnału)

Poniżej masz jak się można dostać do jednego i drugiego (testowane na WinCe 6.0, na 5 nie wiem czy zadziała)

    public partial class MainForm : Form
    {
        private enum SignalStrengthEnum
        {
            PowerOff = -1,
            NoSignal = 0,
            Poor = 1,
            Fair = 2,
            Good = 3,
            VeryGood = 4,
            Excelent = 5,
        }

        private Adapter adapter;
        private WLAN wlan = null;
        private Adapter.SignalQualityHandler signalQualityHandler = null;

        public MainForm()
        {
            InitializeComponent();

            wlan = new WLAN(FusionAccessType.STATISTICS_MODE);
            adapter = wlan.Adapters[0];
            signalQualityHandler = new Adapter.SignalQualityHandler(signalQualityChanged);
            adapter.SignalQualityChanged += signalQualityHandler;

            signalStrength = SignalStrengthEnum.PowerOff;

            batteryLevel = 0;
        }


        private SignalStrengthEnum signalStrength;
        private SignalStrengthEnum SignalStrength
        {
            get { return this.signalStrength; }
            set
            {
                if (!this.signalStrength.Equals(value))
                {
                    this.signalStrength = value;
                }
            }
        }

        private byte batteryLevel;
        private byte BatteryLevel
        {
            get { return this.batteryLevel; }
            set
            {
                if (!this.batteryLevel.Equals(value))
                {
                    this.batteryLevel = value;
                }
            }
        }

        protected override void OnClosed(EventArgs e)
        {
		//WAŻNE!!!!!
            adapter.SignalQualityChanged -= signalQualityHandler;
            adapter = null;

            wlan.Dispose();
            wlan = null;

            base.OnClosed(e);
        }

        private void signalQualityChanged(object sender, StatusChangeArgs e)
        {
            if (e.Adapter.PowerState == Adapter.PowerStates.OFF)
            {
                SignalStrength = SignalStrengthEnum.PowerOff;
            }
            else
            {
                switch (e.SignalQuality)
                {
                    case Adapter.SignalQualityRange.NONE:
                        SignalStrength = SignalStrengthEnum.NoSignal;
                        break;
                    case Adapter.SignalQualityRange.POOR:
                        SignalStrength = SignalStrengthEnum.Poor;
                        break;
                    case Adapter.SignalQualityRange.FAIR:
                        SignalStrength = SignalStrengthEnum.Fair;
                        break;
                    case Adapter.SignalQualityRange.GOOD:
                        SignalStrength = SignalStrengthEnum.Good;
                        break;
                    case Adapter.SignalQualityRange.VERYGOOD:
                        SignalStrength = SignalStrengthEnum.VeryGood;
                        break;
                    case Adapter.SignalQualityRange.EXCELLENT:
                        SignalStrength = SignalStrengthEnum.Excelent;
                        break;
                }
            }
        }

        private void tmrTime_Tick(object sender, EventArgs e)
        {
            BatteryLevel = BatteryInfo.GetBatteryLifePercent();
        }

    }

BatteryInfo.cs

using System;
using System.Runtime.InteropServices;

namespace jkiśtam
{
    public sealed class BatteryInfo
    {
        public enum BatteryChemistry : byte
        {
            BATTERY_CHEMISTRY_ALKALINE = 0x01,  // Alkaline battery.
            BATTERY_CHEMISTRY_NICD = 0x02, // Nickel Cadmium battery.
            BATTERY_CHEMISTRY_NIMH = 0x03, // Nickel Metal Hydride battery.
            BATTERY_CHEMISTRY_LION = 0x04, // Lithium Ion battery.
            BATTERY_CHEMISTRY_LIPOLY = 0x05, // Lithium Polymer battery.
            BATTERY_CHEMISTRY_ZINCAIR = 0x06, // Zinc Air battery.
            BATTERY_CHEMISTRY_UNKNOWN = 0xFF // Battery chemistry is unknown.
        }

        public enum ACLineStatus : byte
        {
            AC_LINE_OFFLINE = 0, // Offline
            AC_LINE_ONLINE = 1, // Online
            AC_LINE_BACKUP_POWER = 2, // Backup Power
            AC_LINE_UNKNOWN = 0xFF, //
            Unknown = 0xFF, //status
        }

        private class SYSTEM_POWER_STATUS_EX2
        {
            //AC power status. 
            public ACLineStatus ACLineStatus;
            //Battery charge status
            public BatteryFlag BatteryFlag;
            // Percentage of full battery charge remaining. Must be in 
            // the range 0 to 100, or BATTERY_PERCENTAGE_UNKNOWN if 
            // percentage of battery life remaining is unknown
            public byte BatteryLifePercent;
            byte Reserved1;
            //Percentage of full battery charge remaining. Must be 
            // in the range 0 to 100, or BATTERY_PERCENTAGE_UNKNOWN 
            // if percentage of battery life remaining is unknown. 
            public int BatteryLifeTime;
            // Number of seconds of battery life when at full charge, 
            // or BATTERY_LIFE_UNKNOWN if full lifetime of battery is unknown
            public int BatteryFullLifeTime;
            byte Reserved2;
            // Backup battery charge status.
            public BatteryFlag BackupBatteryFlag;
            // Percentage of full backup battery charge remaining. Must be in 
            // the range 0 to 100, or BATTERY_PERCENTAGE_UNKNOWN if percentage 
            // of backup battery life remaining is unknown. 

            public byte BackupBatteryLifePercent;
            byte Reserved3;
            // Number of seconds of backup battery life when at full charge, or 
            // BATTERY_LIFE_UNKNOWN if number of seconds of backup battery life 
            // remaining is unknown. 
            public int BackupBatteryLifeTime;
            // Number of seconds of backup battery life when at full charge, or 
            // BATTERY_LIFE_UNKNOWN if full lifetime of backup battery is unknown
            public int BackupBatteryFullLifeTime;
            // Number of millivolts (mV) of battery voltage. It can range from 0 
            // to 65535
            public int BatteryVoltage;
            // Number of milliamps (mA) of instantaneous current drain. It can 
            // range from 0 to 32767 for charge and 0 to –32768 for discharge. 
            public int BatteryCurrent;
            //Number of milliseconds (mS) that is the time constant interval 
            // used in reporting BatteryAverageCurrent. 
            public int BatteryAverageCurrent;
            // Number of milliseconds (mS) that is the time constant interval 
            // used in reporting BatteryAverageCurrent. 

            public int BatteryAverageInterval;
            // Average number of milliamp hours (mAh) of long-term cumulative 
            // average discharge. It can range from 0 to –32768. This value is 
            // reset when the batteries are charged or changed. 

            public int BatterymAHourConsumed;
            // Battery temperature reported in 0.1 degree Celsius increments. It 
            // can range from –3276.8 to 3276.7. 
            public int BatteryTemperature;
            // Number of millivolts (mV) of backup battery voltage. It can range 
            // from 0 to 65535.
            public int BackupBatteryVoltage;
            // Type of battery.
            public BatteryChemistry BatteryChemistry;
            //  Add any extra information after the BatteryChemistry member.
        }

        private enum BatteryFlag : byte
        {
            BATTERY_FLAG_HIGH = 0x01,
            BATTERY_FLAG_CRITICAL = 0x04,
            BATTERY_FLAG_CHARGING = 0x08,
            BATTERY_FLAG_NO_BATTERY = 0x80,
            BATTERY_FLAG_UNKNOWN = 0xFF,
            BATTERY_FLAG_LOW = 0x02
        }

        public enum BatteryLevel
        {
            Low,
            Medium,
            High,
            VeryHigh
        };

        [DllImport("CoreDLL")]
        private static extern int GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 statusInfo, int length, int getLatest);


        public static BatteryLevel GetSystemPowerStatus()
        {
            SYSTEM_POWER_STATUS_EX2 retVal = new SYSTEM_POWER_STATUS_EX2();
            int result = GetSystemPowerStatusEx2(retVal, Marshal.SizeOf(retVal), 0);
            switch (retVal.BatteryFlag)
            {
                case BatteryFlag.BATTERY_FLAG_CRITICAL:
                    return BatteryLevel.Medium;
                case BatteryFlag.BATTERY_FLAG_LOW:
                    return BatteryLevel.High;
                case BatteryFlag.BATTERY_FLAG_HIGH:
                    return BatteryLevel.VeryHigh;
                default:
                    return BatteryLevel.Low;
            }
        }

        public static Byte GetBatteryLifePercent()
        {
            SYSTEM_POWER_STATUS_EX2 retVal = new SYSTEM_POWER_STATUS_EX2();
            int result = GetSystemPowerStatusEx2(retVal, Marshal.SizeOf(retVal), 0);
            return retVal.BatteryLifePercent;
        }
    }
}
0

Dopiero zaczynam w C#
Czy mógłbyś dopisać jak dopisać stan baterii do progresbar ?

0

przecież Masz Metodę GetBatteryLifePercent...
porównujesz do 100 i gotowe...
nie pisałem w CE ale w Winformsach jest podobnie, masz PrograssBar i ustawiasz mu Value, przepinasz wartość z tej funkcji powyżej i gotowe :)

0

Win ce 5
progressBar1.Value = GetBatteryLifePercent();
niestety nie pokazuje nic

1 użytkowników online, w tym zalogowanych: 0, gości: 1