Sending output of the program to serial port (rs232 - COM3)

I wish to send the output of my program to serial port (I am using a USB to serial converter on my laptop). Whenever the 'out' variable is '1', i wish to send series of bytes '11111111' to the serial port. else '00000000'. Choose any baud rate that pleases you.

Kindly help me out. I am an electronics engineer and am very bad at this stuff. Need urgent help as I need this code for my semester project in 2 days' time :(



#include <windows.h>
#include <iostream>

#define UNKNOWN 0xFFFFFFFF

using namespace std;

int main() {
while(1){
SYSTEM_POWER_STATUS status;

GetSystemPowerStatus( &status );
int life = status.BatteryLifePercent;
int secs = status.BatteryLifeTime;
int charging = status.ACLineStatus;
int out;

cout << life << "% -> ";
switch (status.BatteryFlag) {
case 1: cout << "High";
break;
case 2: cout << "Low";
break;
case 4: cout << "Critical";
break;
case 8: cout << "Charging";
break;
case 128: cout << "No system battery";
break;
case 256: cout << "Unknown status";
break;
}

if (secs == UNKNOWN) {
cout << endl << "Amount of time remaining is unknown" << endl;
}
else cout << endl << secs << " seconds remaining" << endl;

switch (status.ACLineStatus) {
case 0: cout << "The system is getting discharged" <<endl;
break;
case 1: cout << "The system is under charging" <<endl;
break;
case 255: cout << "Charging status is unknown" <<endl;
break;
}
if (charging==0)
{
if ( life <=100 && life >90)
out = 0;
else if ( life >10 && life <=90)
out = 0;
else
out = 1;
}
else
{
if ( life <=100 && life >90)
out = 0;
else if ( life >10 && life <=90)
out = 1;
else
out = 1;
}

if (out==1)
cout<<"Please charge"<<endl<<endl<<endl<<endl;
else if (out==0)
cout<<"Please don't charge"<<endl<<endl<<endl<<endl;

Sleep( 100 );
}
return 0;
}
VC++ consider port as a file , you have to open the port and then send the data to the port .


1
2
3
4
5
6
7
8
9
10
HANDLE hComm;
hComm = CreateFile( gszPort,  
                    GENERIC_READ | GENERIC_WRITE, 
                    0, 
                    0, 
                    OPEN_EXISTING,
                    FILE_FLAG_OVERLAPPED,
                    0);
if (hComm == INVALID_HANDLE_VALUE)
   // error opening port; abort 


//Writting to the comport
WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)
Topic archived. No new replies allowed.