If you have a UPS attached to your computer system or you are running off a laptop, you will at some point need to know if the system is at critical battery power. This will require you to gracefuly exit and cleanup your application before the operating system shuts down. The following labVIEW VI will give you feedback on the battery status which will allow you to make decisions on what to do next...
In order to acomplish this task we will tap into a Windows API function called GetSystemPowerStatus The GetSystemPowerStatus function retrieves the power status of the system. The status indicates whether the system is running on AC or DC power, whether the battery is currently charging, and how much battery life remains. The first step is to look at the online help provided from Microsoft on this function:
Function Prototype:
BOOL GetSystemPowerStatus(
LPSYSTEM_POWER_STATUS lpSystemPowerStatus
);
Parameters
lpSystemPowerStatus
[out] Pointer to a SYSTEM_POWER_STATUS structure that receives status information.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Library: Use Kernel32.lib.
So what it's telling us is that we need more information on the SYSTEM_POWER_STATUS structure. Further investigation reveals this for us:
The SYSTEM_POWER_STATUS structure contains information about the power status of the system.
typedef struct _SYSTEM_POWER_STATUS {
BYTE ACLineStatus;
BYTE BatteryFlag;
BYTE BatteryLifePercent;
BYTE Reserved1;
DWORD BatteryLifeTime;
DWORD BatteryFullLifeTime;
} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;
Members
ACLineStatus
AC power status. This member can be one of the following values.
Value Meaning
0 Offline
1 Online
255 Unknown status
BatteryFlag
Battery charge status. This member can be one or more of the following values.
Value Meaning
1 High
2 Low
4 Critical
8 Charging
128 No system battery
255 Unknown status
BatteryLifePercent
Percentage of full battery charge remaining. This member can be a value in the range 0 to 100, or 255 if status is unknown.
Reserved1
Reserved; must be zero.
BatteryLifeTime
Number of seconds of battery life remaining, or –1 if remaining seconds are unknown.
BatteryFullLifeTime
Number of seconds of battery life when at full charge, or –1 if full battery lifetime is unknown.
Remarks:
The system is only capable of estimating BatteryFullTime based on calculations on BatteryLifeTime and BatteryLifePercent. Without smart battery subsystems, this value may not be accurate enough to be useful.
So we're looking at a cluster with several numerics. To make this seasier for us we will create a cluster in LabVIEW and use the adapt to type selection in the call library node. The diagram will look like this:
As you can see we've defined the cluster as a constant on the diagram. From the documentation above we have all we need to make the DLL call. We place the node on the digram and in the library name field we put kernel32.dll as seen below:
I put some simple error handling in there to handle the case when it returns a zero since this constitutes an error. The front panel is the output of the cluster with the various status elements exposed:
That's all there is to it. Remember that this VI only works on computers with a configured UPS or on a laptop. Now in your application you can have your software trigger of the Battery Life Percent output. I like to use the AC Power Status in combination with the Battery Flag. I detect when the AC is offline and in battery mode. I then either shutdown immeadiatly or after the battery flag goes to low.
One thing to watch for is that the computer power options may be set to shutdown the system long before you shutdown your software. Here is an example of a configuration that puts the computer into standby at low battery level:
You have to make sure you shutdown the software and allow enough time to exit your application before the computer powers down.
LabVIEW 6.0 VI's: GetSystemPowerStatus.zip
Support or questions on this code should be done via the forum.