Serial communication using C++

I am using visual studio 10 but i am unable to send data through that code........... Can anyone tell me problem in that code




#include "stdafx.h"
#include
#include
using namespace std;
bool WriteComPort(char data);

int main ()
{

char again;
do
{
char data;
bool check;
cout<<" Enter data to be send ";
cin>>data;
check=WriteComPort(data);
if(check==0)
cout<<"
Error occur... Data transmit not possible
";
else
cout<<"Data successfully transmitted ";
cout<<" More data transmission require y or n
";
cin>>again;
}
while (again=='y');
return 0;
}


bool WriteComPort( char data)
{
DCB dcb;
DWORD byteswritten;
HANDLE hPort = CreateFile("\\.\COM11",GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (!GetCommState(hPort,&dcb))
return false;
dcb.BaudRate = CBR_9600; //9600 Baud
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop
if (!SetCommState(hPort,&dcb))
return false;
bool retVal = WriteFile(hPort,&data,1,&byteswritten,NULL);
CloseHandle(hPort); //close the handle
return retVal;
}








































Comments

  • check the port existing, the program is working fine.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion