Re: Serial communication Threading (Thread is blocking)
the thread is blocked on ReadFile code is like this can i avoid this
blocking behaviour.
CString ReadCharbyChar()
{
CString FromBoard;
DWORD dwEventMask;
DWORD dwSize=0;
if(!SetCommMask(hCom, EV_RXCHAR)) /* Setting Event Type */
{
MessageBox(NULL,"Can not set the event","THREAD",MB_OK);
return FromBoard;
}
if(WaitCommEvent(hCom, &dwEventMask, NULL)) /* Waiting For Event to
Occur */
{
if(dwEventMask & EV_RXCHAR)
{
char szBuf;
DWORD dwIncommingReadSize;
do
{
if(ReadFile(hCom, &szBuf, 1, &dwIncommingReadSize, NULL) != 0)
{
if(dwIncommingReadSize > 0)
{
dwSize += dwIncommingReadSize;
FromBoard += szBuf;
}
}
else
{
CloseComHandle();
MessageBox(NULL,"ReadFile Failed","THREAD",MB_OK);
unsigned long error = ::GetLastError();
break;
}
if(szBuf == 'n')
break;
} while(dwIncommingReadSize > 0);
}
}
else
{
MessageBox(NULL,"Event never happened","THREAD",MB_OK);
return FromBoard;
}
return FromBoard;
}
and the com port hanle is global so i can close the port after killing
the thread.
Is this right or wrong?