Beijer Electronics (formerly QSI Corporation)
http://qsiforums.com/

Quick TREQ-DX example for performing a soft power off
http://qsiforums.com/viewtopic.php?f=8&t=3926
Page 1 of 1

Author:  danieln [ Wed Mar 24, 2010 8:16 am ]
Post subject:  Quick TREQ-DX example for performing a soft power off

This example is a command line utility for performing a soft power-off. It accepts one paramter, in minutes, that specifies how long the device should wait before shutting down. It checks once a minute, and starts counting once it detects the device is in user idle or system idle mode.

This app was primarily used in a situation where the TREQ-DX went into system idle when it detected the truck was powered off via the ignition signal. This is configurable via a registry setting found at [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\TreqPM\Ignition Pin Options]. More details can be found in the DX user guide http://www.qsicorp.com/engfiles/TREQ/TREQ-DX/TREQ-DX_UserGuide.pdf on page 64.

Code:
// ShutOffCE.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "TreqPublic.h"
#include "TreqIoCtl.h"

BOOL enablecounter;
int counter;
int death;
void CertainDeath();
DWORD dwPowerState;
HANDLE hDevice;

int _tmain(int argc, _TCHAR* argv[])
{
   if (argc!=2)
      return -1;
   death=_wtoi(argv[1]);
   if (death<=0)
      return -1;

   enablecounter=false;
    counter=0;

    CertainDeath();

   return 0;
}


void CertainDeath()
{
   while (true)
   {
      Sleep(60000);
            
      hDevice = CreateFile(TEXT("PMD1:"), GENERIC_READ, 0, NULL, OPEN_EXISTING,0, NULL);
            
      // get the current power state
      DeviceIoControl(hDevice, PMD_GET_POWERSTATE, NULL, 0, &dwPowerState,sizeof(dwPowerState), NULL, NULL);

      if ((dwPowerState==POWER_USERIDLE)||(dwPowerState==POWER_SYSTEMIDLE))
      {
         counter++;
         if (counter==death)
         {
            dwPowerState = POWER_OFF;
            DeviceIoControl(hDevice, PMD_SET_POWERSTATE, &dwPowerState,sizeof(dwPowerState), NULL, 0, NULL, NULL);
         }
      }else
         counter=0;
      
      CloseHandle(hDevice);
   }
}

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/