I was trying to implement a serial port communication, the SetCommState function returns error(0).
I have tried with the following code.
COMMPROP commprop = {0};
COMMTIMEOUTS timeouts = {0};
m_hComm = CreateFile(_T("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (0 == GetCommProperties(m_hComm,&commprop)) {
MessageBox(_T("Error in setting params"), _T("Error"),
MB_ICONERROR | MB_OK);
}
if (m_hComm == INVALID_HANDLE_VALUE)
{
MessageBox(_T("Cannot open port"), _T("Error"),
MB_ICONERROR | MB_OK);
}
else
{
MessageBox(_T("Port open"), _T("Info"),
MB_ICONINFORMATION | MB_OK);
}
DWORD size =1;
char readBuf[5];
char sendBuf[10];
DCB dcb;
memset(readBuf,0,5);
memset(sendBuf,0,10);
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength=sizeof(dcb);
GetCommState(m_hComm,&dcb);
dcb.BaudRate=5;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
if (0 == SetCommState(m_hComm,&dcb))
{
AfxMessageBox(_T("error on setting baud rate"));
}
while(size ==0)
{
if(!ReadFile(m_hComm, &readBuf[0], 1, &size, NULL))
{
MessageBox(_T("Read Error"), _T("Error"),
MB_ICONERROR | MB_OK);
}
}
Is it valid to use FILE_FLAG_OVERLAPPED when calling the
CreateFile method? I cannot identify why this issue happens, Please help me to find the trouble with this code.