Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <tchar.h>
- #include <signal.h>
- #include <cstdlib>
- #include <iostream>
- #include "Serial.h"
- //#define DEBUG true
- // Set up the handler for CTRL+C interruption signal
- volatile int ctrlc_pressed = 0;
- void ctrlc_handler(int sig){
- ctrlc_pressed = 1;
- std::cout << "CTRL+C hit, exiting" << std::endl;
- }
- int main(int argc, char *argv[]){
- signal(SIGINT, ctrlc_handler); // Register the signal handler
- SYSTEM_POWER_STATUS power_status; // The variable for power
- MEMORYSTATUSEX memory_status; // And memory
- memory_status.dwLength = sizeof(memory_status); // ??? - As per docs.
- CSerial serial; // New serial object
- serial.Open(_T("COM1")); // Open port COM1
- serial.Setup(
- CSerial::EBaud9600, // 9600 baud
- CSerial::EData8, // 8-bit bytes
- CSerial::EParNone, // No parity
- CSerial::EStop1); // 1 start/stop bit
- serial.SetupHandshaking(CSerial::EHandshakeOff); // No handshaking
- char values[30]; // Buffer for info output
- while(!ctrlc_pressed){
- GetSystemPowerStatus(&power_status); // Get the power and memory stats
- GlobalMemoryStatusEx(&memory_status);
- sprintf( // Load values to the info buffer
- values,
- "Battery: %d%%\nMemory: %d%%",
- power_status.BatteryLifePercent,
- memory_status.dwMemoryLoad
- );
- std::cout << values << std::endl; // Print the info buffer
- char serialdat[] = { // Load data for serial, differentiate with MSB
- power_status.BatteryLifePercent | (0 << 7),
- ((char)memory_status.dwMemoryLoad) | (1 << 7),
- '\0'
- };
- #ifdef DEBUG
- std::cout << serialdat << std::endl;
- #endif
- serial.Write(serialdat);
- Sleep(5000);
- }
- serial.Close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement