Hi
I have written a code to read from the COMM port. Below given is the code for the thread which reads the comm port and send the data to the object which process it. But there is a small problem in it. Some times the code doesn't read the complete content received at the comm port
for exampl
The data expected is
*** BSC sous le LIC nominal **
Personnalisation du BSC [2
But the data which i get is
*** BSC souersonnalisation du BSC [2
Please help me as this is very important
UINT CSerialPort::CommThread(LPVOID pParam
// Cast the void pointer passed to the thread back t
// a pointer of CSerialPort clas
CSerialPort* port = (CSerialPort*)pParam
COMSTAT comStat
DWORD dwRead,dwReadResult,dwResult,dwErrors
DWORD dwEvtMask,dwErrorFlags
COMSTAT ComStat ;
BOOL bResult
char lpszBlock[5000]
int index
char temp [40]
OVERLAPPED osWaiter
ZeroMemory (&osWaiter, sizeof(OVERLAPPED))
osWaiter.hEvent = CreateEvent ( NULL , TRUE , FALSE , NULL ); // auto reset - not signale
if (!SetCommMask(port->m_hComm,EV_RXCHAR)
return ( FALSE )
while(!port->m_bCloseSerialPort
dwEvtMask = 0
bResult=::WaitCommEvent(port->m_hComm,&dwEvtMask,&osWaiter );
//ClearCommError(port->m_hComm,&dwErrorFlags,&ComStat)
//strcpy(lpszBlock,"")
if ( !bResult )
dwErrors = GetLastError ()
if ( dwErrors == ERROR_IO_PENDING )
switch ( WaitForSingleObject(osWaiter.hEvent,INFINITE ) )
case WAIT_OBJECT_0:
// Wait Comm Event complete
if(GetOverlappedResult ( port->m_hComm , &osWaiter, &dwReadResult , FALSE )
bResult = TRUE
break
case WAIT_TIMEOUT
AfxMessageBox("WAIT_TIMEOUT")
break
default
AfxMessageBox("Default")
break
if ( !bResult )
ClearCommError(port->m_hComm, &dwErrors, &comStat)
PurgeComm ( port->m_hComm , PURGE_TXCLEAR | PURGE_RXCLEAR );
}
else
if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR
ClearCommError(port->m_hComm, &dwErrors, &comStat)
bResult=ReadFile(port->m_hComm,lpszBlock,comStat.cbInQue,&dwReadResult,&port->m_ovRead)
if (!bResult
switch (dwErrors = GetLastError())
case ERROR_IO_PENDING:
// asynchronous I/O is still in progress
switch ( WaitForSingleObject ( port->m_ovRead.hEvent , 6000 ) )
case WAIT_OBJECT_0
// Async I/O complete
GetOverlappedResult ( port->m_hComm , &port->m_ovRead, &dwReadResult , FALSE )
break
case WAIT_TIMEOUT
default
ClearCommError(port->m_hComm, &dwErrors, &comStat)
dwReadResult = 0
break
} // wait for IO to complet
break
default
dwReadResult = 0
break
} // switch ReadFile Erro
if(dwReadResult>0)
{
lpszBlock[dwReadResult]='\0';
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM)lpszBlock, (LPARAM) port->m_nPortNr);
}
}
}while(dwReadResult>0);
if(index!=0)
{
lpszBlock[index]='\0';
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM)lpszBlock, (LPARAM) port->m_nPortNr);
}
}
}
AfxMessageBox("Port is closed");
EscapeCommFunction(port->m_hComm,CLRDTR);
PurgeComm(port->m_hComm, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(port->m_hComm) ;
return( TRUE ) ;
}