Overview
Ever wanted to know whether your laptop battery has enough remaining power?
Or do you want to know whether the mains are off in your UPS-powered PC so that you can close your job and save test data without loosing any information?
If yes, this tip is for you!
Description
The Windows Power management service takes care of controlling battery status on laptops, as well as controlling the status of a connected UPS that adhere to Windows standards: in this example I show how to receive events from the service and react to them.
The system informs all applications of a change in power status by broadcasting WM_POWERBROADCAST message that every application can receive. Inside CVI, a callback function can be installed that automatically fires up when a specific message is recevied from the OS: this is done with InstallWinMsgCallback instruction.
Once received, the program can derive infos on the power status using GetSystemPowerStatus API command and react on actual status according to system requirements: from a simple message to the operator up to ending runnning functions, closing the program or even shutting down the system.
Steps to Implement or Execute Code
The sample program can be run as-is: experiment by connecting and disconnecting AC power to your laptop or your UPS and see changes in program window. Power status is given either in separate indicators and in a single, system-like message. Events received are logged in a listbox.
To integrate this idea into an application the following steps are to be perfomed:
Requirements
Software
The sample program has been developed in CVI 2012SP1, but it can be run in older versions provided the UIR file is downgraded accordingly.
This example relies on Win32 APIs to operate, therefore the Interface to Win32API (or Windows SDK) must be installed.
Hardware
No hardware is needed to run this example.
Program screenshots
This is the program window when the system is connected to the mains:
This is the program window when the power is off: you can see the events triggered by the OS in the log control at the bottom of the panel.
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
Hi, where is the link to download the sample program? Thank you
Apparently I forgot to attach the example
I have added it now.