From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Controlling power in laptops or UPS-operated PCs in LabWindows/CVI

Code and Documents

Attachment

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:

  1. #include <windows.h> in the source file where you are calling GetSystemPowerStatus
  2. Add the appropriate InstallWinMsgCallback to your program: it cannot be installed on child panels, so I suggest you to install it on program main panel; remember to include windows.h even in this file if it is different from the previous one and you want to use WM_POWERBROADCAST constant in the call
  3. Design the callback that handles system messages depending on application requirements: DisplayPowerStatus () function in my example gives an example of how to discriminate system status
  4. Do not forget to call RemoveWinMsgCallback when you have finished handling system messages

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:

Power ON.png


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.

Power off.png



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
ulivinico
Member
Member
on

Hi, where is the link to download the sample program? Thank you

RobertoBozzolo
Proven Zealot
Proven Zealot
on

Apparently I forgot to attach the example

I have added it now.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Contributors