📄 com232.cpp
字号:
}
// get rid of event handle
CloseHandle( os.hEvent ) ;
// clear information in structure (kind of a "we're done flag")
// THREADID( npTTYInfo ) = 0 ;
// HTHREAD( npTTYInfo ) = NULL ;
return( TRUE ) ;
} // end of CommWatchProc()
//---------------------------------------------------------------------------
// int NEAR ReadCommBlock( HWND hWnd, LPSTR lpszBlock, int nMaxLength )
//
// Description:
// Reads a block from the COM port and stuffs it into
// the provided buffer.
//
// Parameters:
// HWND hWnd
// handle to TTY window
//
// LPSTR lpszBlock
// block used for storage
//
// int nMaxLength
// max length of block to read
//
// Win-32 Porting Issues:
// - ReadComm() has been replaced by ReadFile() in Win-32.
// - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------
int ReadCommBlock(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength )
{
BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
char szError[ 10 ] ;
/*NPTTYINFO npTTYInfo ;
if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
return ( FALSE ) ;
*/
// only try to read number of bytes in queue
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
dwLength = min( (DWORD) nMaxLength, ComStat.cbInQue ) ;
if (dwLength > 0)
{
fReadStat = ReadFile( comDev.m_hCom, lpszBlock,
dwLength, &dwLength, &(comDev.m_rdos) ) ;
/* if(WaitForSingleObject(comDev.m_rdos.hEvent,5000) == WAIT_OBJECT_0)
{
GetOverlappedResult( comDev.m_hCom ,
&(comDev.m_rdos), &dwLength, TRUE );
//lpszBlo[255] = '0';
//AfxMessageBox(buf);
}*/
if (!fReadStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
OutputDebugString("\n\rIO Pending");
// We have to wait for read to complete.
// This function will timeout according to the
// CommTimeOuts.ReadTotalTimeoutConstant variable
// Every time it times out, check for port errors
while(!GetOverlappedResult( comDev.m_hCom ,
&(comDev.m_rdos), &dwLength, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
// normal result if not finished
continue;
else
{
// an error occurred, try to recover
wsprintf( szError, "<CE-%u>", dwError ) ;
//WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
break;
}
}
}
else
{
// some other error occurred
dwLength = 0 ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
}
}
}
return ( dwLength ) ;
} // end of ReadCommBlock()
//-------------------------------------------------------
//
//ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut)
//
//---------------------------------------------------------
//---------------------------------------------------------------------------
int ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut)
{
LPSTR lpOffset=lpszBlock;
int nReadCount = 0;
char chBuf;
//time_t beginTime,endTime;
if(!comDev.m_hCom)
return 0;
if(dwTimeOut <= 0)
return 0;
MSG msg;
//time(&beginTime);
DWORD dwLastTick,dwNowTick,dwGoneTime;
dwGoneTime = 0;
dwLastTick = GetTickCount();
dwNowTick = dwLastTick;
// double diftime;
do
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
if(ReadCommBlock(comDev,&chBuf,1) > 0)
{
//TRACE("----get a char----\n");
*lpOffset = chBuf;
lpOffset ++;
nReadCount ++;
}
/*time(&endTime);
diftime = difftime(endTime,beginTime);*/
dwNowTick = GetTickCount();
if(dwNowTick < dwLastTick)
{
dwLastTick = dwNowTick;
}
dwGoneTime = dwNowTick - dwLastTick;
//TRACE("gon time = %lu\n",dwGoneTime);
}while((nReadCount < nMaxLength) && (dwGoneTime < dwTimeOut));//((diftime * 1000.0) < dwTimeOut));
return (nReadCount);
}//end ReadCommBlockEx
// BOOL NEAR WriteCommBlock( HWND hWnd, BYTE *pByte )
//
// Description:
// Writes a block of data to the COM port specified in the associated
// TTY info structure.
//
// Parameters:
// HWND hWnd
// handle to TTY window
//
// BYTE *pByte
// pointer to data to write to port
//
// Win-32 Porting Issues:
// - WriteComm() has been replaced by WriteFile() in Win-32.
// - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------
BOOL WriteCommBlock( CComStatus& comDev, unsigned char * lpByte , DWORD dwBytesToWrite)
{
BOOL fWriteStat ;
DWORD dwBytesWritten ;
// NPTTYINFO npTTYInfo ;
DWORD dwErrorFlags;
DWORD dwError;
DWORD dwBytesSent=0;
COMSTAT ComStat;
char szError[ 128 ] ;
/*
if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
return ( FALSE ) ;
*/
fWriteStat = WriteFile( comDev.m_hCom , lpByte, dwBytesToWrite,
&dwBytesWritten, &( comDev.m_wtos) ) ;
// Note that normally the code will not execute the following
// because the driver caches write operations. Small I/O requests
// (up to several thousand bytes) will normally be accepted
// immediately and WriteFile will return true even though an
// overlapped operation was specified
if (!fWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
// We should wait for the completion of the write operation
// so we know if it worked or not
// This is only one way to do this. It might be beneficial to
// place the write operation in a separate thread
// so that blocking on completion will not negatively
// affect the responsiveness of the UI
// If the write takes too long to complete, this
// function will timeout according to the
// CommTimeOuts.WriteTotalTimeoutMultiplier variable.
// This code logs the timeout but does not retry
// the write.
while(!GetOverlappedResult( comDev.m_hCom,
&(comDev.m_wtos), &dwBytesWritten, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
{
// normal result if not finished
dwBytesSent += dwBytesWritten;
continue;
}
else
{
// an error occurred, try to recover
wsprintf( szError, "<CE-%u>", dwError ) ;
//WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
break;
}
}
dwBytesSent += dwBytesWritten;
if( dwBytesSent != dwBytesToWrite )
wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes sent", dwBytesSent);
else
wsprintf(szError,"\n%ld bytes written", dwBytesSent);
OutputDebugString(szError);
}
else
{
// some other error occurred
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
return ( FALSE );
}
}
return ( TRUE ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -