Serial Port Communication in VC++ using MFC

<p>Try for below...
<pre>
serial transmit

#include "stdafx.h"
#include "iostream.h"

unsigned char Byte2Write ,ByteReceived;

class Cserial:public CWnd
{
public:
BOOL Configure_port(void);
BOOL Open_port(void);
BOOL Write_port(void);
BOOL Read_port(void);
BOOL Close_port(void);
Cserial(){}

HANDLE hComm;
DCB m_dcb;
// COMMTIMEOUTS m_CommTimeouts;
BOOL m_bPortReady;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
DWORD dwBytesRead;
// virtual ~Cserial();
} ;

// funtion definitions

BOOL Cserial::Open_port(void)
{
CString portname ="com1" ;
portname = "//./"+ portname;

hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if(hComm==INVALID_HANDLE_VALUE)
{cout<< "unable to open the port"<< endl ;
return false;}
else
return true;
}
//*****************************************************

BOOL Cserial::Configure_port(void)
{
m_dcb.BaudRate= 9600 ;
m_dcb.ByteSize = 8;
m_dcb.fParity =0 ;
m_dcb.Parity = NOPARITY ;
m_dcb.StopBits = ONESTOPBIT ;

m_dcb.fBinary=TRUE;
m_dcb.fDsrSensitivity=false;
m_dcb.fOutX=false;
m_dcb.fInX=false;
m_dcb.fNull=false;
m_dcb.fAbortOnError=TRUE;
m_dcb.fOutxCtsFlow=FALSE;
m_dcb.fOutxDsrFlow=false;
m_dcb.fDtrControl=DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity=false;
m_dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow=false;
m_dcb.fOutxCtsFlow=false;

/* m_bPortReady = SetCommState(hComm, &m_dcb);
if(m_bPortReady ==0){
MessageBox("SetCommState Error","Error",MB_OK+MB_ICONERROR);
CloseHandle(hComm);
return false;}*/
return true;
}
//**************************************************

BOOL Cserial::Write_port(void)
{
iBytesWritten=0;
if(WriteFile(hComm,&Byte2Write,1,&iBytesWritten,NULL)==0)
return false;
else return true;
}
//***************************************************

BOOL Cserial::Read_port(void)
{
BYTE rx;

DWORD dwBytesTransferred=0;

if (ReadFile (hComm, &rx, 1, &dwBytesTransferred, 0)){
if (dwBytesTransferred == 1){
ByteReceived=rx;
return true;}}

return false;
}

//**************************************************

BOOL Cserial::Close_port(void)
{
CloseHandle(hComm);
return true;
}
//*************************************************
// main function

void main(void)
{


Cserial ser_port ;

if (!ser_port.Open_port())
{cout << "Port 1 opening failed" <<endl;
goto out ;}

else if (!ser_port.Configure_port())
{cout<< "Port 1 configuration failed"<<endl ;
goto out ;}

else if (!ser_port.Configure_port())
{
cout<< "Port 1 configuration failed"<<endl ;
goto out;
}
else
{
while(1)
{
cout<<"\nEnter a character:";
cin>>Byte2Write;
if(ser_port.Write_port())
{
cout <<" Data_written is: "<< Byte2Write<< endl ;
if(Byte2Write=='z')
goto out;
}
else
{
cout <<"Port 1 writing failed"<<endl ;
goto out ;
}
};

}



/* if (ser_port.Read_port())
cout<< "Data read is : "<<ByteReceived <<endl ;
else
{cout<< "Port 1 reading failed"<<endl ;
goto out ;
}

if (!ser_port.Close_port())
cout<< "Port can not be closed" << endl ;
else
cout<< "Port is closed :: good bye " << endl ; */
out:
cout<< "Exit"<< endl ;
}














serial receive


#include "stdafx.h"
#include "iostream.h"

unsigned char Byte2Write ='A',ByteReceived=0 ;

class Cserial:public CWnd
{
public:
BOOL Configure_port(void);
BOOL Open_port(void);
BOOL Write_port(void);
BOOL Read_port(void);
BOOL Close_port(void);
Cserial(){}

HANDLE hComm;
DCB m_dcb;
// COMMTIMEOUTS m_CommTimeouts;
BOOL m_bPortReady;
BOOL bWriteRC;
BOOL bReadRC;
DWORD iBytesWritten;
DWORD iBytesRead;
DWORD dwBytesRead;
// virtual ~Cserial();
} ;

// funtion definitions

BOOL Cserial::Open_port(void)
{
CString portname ="com1" ;
portname = "//./"+ portname;

hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if(hComm==INVALID_HANDLE_VALUE)
{cout<< "unable to open the port"<< endl ;
return false;}
else
return true;
}
//*****************************************************

BOOL Cserial::Configure_port(void)
{
m_dcb.BaudRate= 9600 ;
m_dcb.ByteSize = 8;
m_dcb.fParity =0 ;
m_dcb.Parity = NOPARITY ;
m_dcb.StopBits = ONESTOPBIT ;

m_dcb.fBinary=TRUE;
m_dcb.fDsrSensitivity=false;
m_dcb.fOutX=false;
m_dcb.fInX=false;
m_dcb.fNull=false;
m_dcb.fAbortOnError=TRUE;
m_dcb.fOutxCtsFlow=FALSE;
m_dcb.fOutxDsrFlow=false;
m_dcb.fDtrControl=DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity=false;
m_dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow=false;
m_dcb.fOutxCtsFlow=false;

/* m_bPortReady = SetCommState(hComm, &m_dcb);
if(m_bPortReady ==0){
MessageBox("SetCommState Error","Error",MB_OK+MB_ICONERROR);
CloseHandle(hComm);
return false;}*/
return true;
}
//**************************************************

BOOL Cserial::Write_port(void)
{
iBytesWritten=0;
if(WriteFile(hComm,&Byte2Write,1,&iBytesWritten,NULL)==0)
return false;
else return true;
}
//***************************************************

BOOL Cserial::Read_port(void)
{
BYTE rx;

DWORD dwBytesTransferred=0;

if (ReadFile (hComm, &rx, 1, &dwBytesTransferred, 0)){
if (dwBytesTransferred == 1){
ByteReceived=rx;
return true;}}

return false;
}

//**************************************************

BOOL Cserial::Close_port(void)
{
CloseHandle(hComm);
return true;
}
//*************************************************
// main function

void main(void)
{

// int i =0 ;
Cserial ser_port ;

if (!ser_port.Open_port())
{cout << "Port 1 opening failed" <<endl;
goto out ;
}

else if (!ser_port.Configure_port())
{
cout<< "Port 1 configuration failed"<<endl ;
goto out ;
}

else if (!ser_port.Configure_port())
{
cout<< "Port 1 configuration failed"<<endl ;
goto out;
}
/* else
{
if(ser_port.Write_port())
cout <<" Data_written is: "<< Byte2Write<< endl ;
else
{
cout <<"Port 1 writing failed"<<endl ;
goto out ;
}
} */


while(1)
{
if (ser_port.Read_port())
{
cout<< "Data read is : "<<ByteReceived <<endl ;
if(ByteReceived=='z')
goto out;
}
else
{
cout<< "Port 1 reading failed"<<endl ;
goto out ;
}
};

/* if (!ser_port.Close_port())
cout<< "Port can not be closed" << endl ;
else
cout<< "Port is closed :: good bye " << endl ; */
out:
ser_port.Close_port();
cout<< "Exit"<< endl ;
}
 
Top