//---------------------------------------------------------------
// Write com
// Ret : 0 : OK
// -1 : com not open
// -2 : time out
//---------------------------------------------------------------
int CSerialComunication::iWriteCom(char * psData, size_t iLen)
{
size_t i;
DWORD dwTimer, tmpdw;
LPCVOID lpBuffer;
带超时功能的读函数,供参考。
//---------------------------------------------------------------
// Read one charator from opened com port
// Ret : 0 : OK
// -1 : com not open
// -2 : time out
//---------------------------------------------------------------
int CSerialComunication::iGetChar(char * cC)
{
DWORD dwTimer, tmpdw;
COMSTAT com_stat;
if( hHandle == NULL ) return( -1 );
dwTimer = GetTickCount( );
do {
ClearCommError( hHandle, &tmpdw, &com_stat );
if( ( GetTickCount( ) - dwTimer )/1000 >= dwTimeWait ) return(-2);
} while( com_stat.cbInQue < 1 ); // Waiting a Charator to read untill time out.