Click here to Skip to main content
13,042,819 members (84,086 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 1 Dec 2011

Using WM_POWER events to monitor a UPS

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Shows how to monitor a UPS, graph the details and shutdown the system


A client wanted an app to monitor and log the UPS for an embedded windows controller.

This toolbar widget provides a framework to capture win32 WM_POWER events, leverage them to draw a graph of UPS state and optionally shutdown/suspend or hibernate the system.

The app is very simple and should be a good framework for other, related projects.


As provided to our client, the application does more than as described here, however the additional functions were client specific and not pertinent.

When looking for a similar project, I found pieces, but nothing specific. As such, this project derives from projects by maharishi_b and lcady.

Using the Code

The app builds to a toolbar app that runs all the time and monitors windows power events. At each event, the app logs the event to a CSV log file and if the AC goes offline, the app pops up and shows a graph of the UPS capacity and the time left in mins.

All these features depend on your UPS being plugged in and supporting the standard windows HID power device profile.

During normal operations, the app may be viewed using the toolbar context menu (right click on the toolbar icon). Using 'show' will bring up the normal operations window:

This shows that the UPS is online with 100% battery capacity. The 'High' at the bottom is the windows power systems determination of the battery capacity. Full scale for the battery capacity is 100%.

When the AC line is lost, the main screen will popup and display the power status:

This shows that the AC is now offline with 67% battery capacity remaining. The system still regards this capacity as 'High'. Now that the AC is offline, the time remaining becomes relevant and is shown in red. Full scale for the time remaining is hard coded as 60mins.

After restoring power, the time remaining is no longer relevant and the system begins re-charging the battery:

Each event that occurs, is logged to the log.csv file in the directory from which PowerMonitor was started. The format is as follows:

Each event is logged by time, date and event type. Details of the event follow as per the system power status structure:

public class SystemPowerStatus
    public ACLineStatus ACLineStatus;
    public BatteryFlag BatteryFlag;
    public Byte BatteryLifePercent;
    public Byte Reserved1;
    public Int32 BatteryLifeTime;
    public Int32 BatteryFullLifeTime;

Most of the code is self explanatory. The strip chart is probably the most interesting - it is an evolution of lcady's strip chart. I am in the process of making it a general purpose strip chart class, but for now it is something of a simple hack.

I like the fact that this class draws to a BMP and then blits the BMP into a picture view. It means the chart construction is mostly independent of the viewing process and can be offloaded to other threads. This is particularly pertinent when doing data acquisition via potentially slow devices.

Points of Interest

It is exceptionally hard to map battery capacity to time left. UPS are getting better, but they mostly use battery voltage and this is a very poor indicator, especially when the battery is old, has been cycled a lot or has been recently cycled.

Time left is only an indicator and actions you take should be based on your own analysis of data collected during AC outages and recharge times.


  • 30th November, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionPower Management in Windows Forms Pin
Kim Togo6-Dec-11 0:30
memberKim Togo6-Dec-11 0:30 
General[My vote of 2] Not very accurate and useful, needs improvements in all areas. Pin
Galatei5-Dec-11 11:36
memberGalatei5-Dec-11 11:36 
GeneralRe: [My vote of 2] Not very accurate and useful, needs improvements in all areas. Pin
klinkenbecker8-Dec-11 18:41
memberklinkenbecker8-Dec-11 18:41 
QuestionThis assumes that UPS software is installed, right? Pin
Paulo Morgado5-Dec-11 9:28
memberPaulo Morgado5-Dec-11 9:28 
AnswerRe: This assumes that UPS software is installed, right? Pin
klinkenbecker8-Dec-11 18:32
memberklinkenbecker8-Dec-11 18:32 
GeneralRe: This assumes that UPS software is installed, right? Pin
Paulo Morgado11-Dec-11 23:20
memberPaulo Morgado11-Dec-11 23:20 
GeneralMy vote of 2 Pin
Dave Kreskowiak1-Dec-11 4:00
mvpDave Kreskowiak1-Dec-11 4:00 
QuestionWhoops... Pin
Dave Kreskowiak1-Dec-11 3:59
mvpDave Kreskowiak1-Dec-11 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by klinkenbecker
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid