Advertisement
AbstractBeliefs

Untitled

Jan 3rd, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <signal.h>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include "Serial.h"
  7.  
  8. //#define DEBUG true
  9.  
  10. // Set up the handler for CTRL+C interruption signal
  11. volatile int ctrlc_pressed = 0;
  12. void ctrlc_handler(int sig){
  13.     ctrlc_pressed = 1;
  14.     std::cout << "CTRL+C hit, exiting" << std::endl;
  15. }
  16.  
  17. int main(int argc, char *argv[]){
  18.     signal(SIGINT, ctrlc_handler);      // Register the signal handler
  19.  
  20.     SYSTEM_POWER_STATUS power_status;   // The variable for power
  21.     MEMORYSTATUSEX      memory_status;  // And memory
  22.     memory_status.dwLength = sizeof(memory_status); // ??? - As per docs.
  23.     CSerial serial;             // New serial object
  24.  
  25.     serial.Open(_T("COM1"));    // Open port COM1
  26.     serial.Setup(
  27.         CSerial::EBaud9600,     // 9600 baud
  28.         CSerial::EData8,        // 8-bit bytes
  29.         CSerial::EParNone,      // No parity
  30.         CSerial::EStop1);       // 1 start/stop bit
  31.     serial.SetupHandshaking(CSerial::EHandshakeOff); // No handshaking
  32.  
  33.     char values[30];                            // Buffer for info output
  34.     while(!ctrlc_pressed){
  35.         GetSystemPowerStatus(&power_status);    // Get the power and memory stats
  36.         GlobalMemoryStatusEx(&memory_status);
  37.  
  38.         sprintf(                            // Load values to the info buffer
  39.             values,
  40.             "Battery: %d%%\nMemory: %d%%",
  41.             power_status.BatteryLifePercent,
  42.             memory_status.dwMemoryLoad
  43.         );
  44.         std::cout << values << std::endl;   // Print the info buffer
  45.  
  46.         char serialdat[] = {    // Load data for serial, differentiate with MSB
  47.             power_status.BatteryLifePercent | (0 << 7),
  48.             ((char)memory_status.dwMemoryLoad) | (1 << 7),
  49.             '\0'
  50.         };
  51.  
  52.         #ifdef DEBUG
  53.         std::cout << serialdat << std::endl;
  54.         #endif
  55.         serial.Write(serialdat);
  56.  
  57.         Sleep(5000);
  58.     }
  59.     serial.Close();
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement