在VC 6.0中用Win32API实现串行通信(4)
2008-04-09 04:10:08来源:互联网 阅读 ()
{
BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
DWORD dwError;
char szError[ 10 ] ;
// 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 (!fReadStat)
{
if (GetLastError() == ERROR_IO_PENDING)
{
OutputDebugString("\n\rIO Pending");
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 ) ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
break;
}
}
}
else
{
// some other error occurred
dwLength = 0 ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
}
}
}
return ( dwLength ) ;
} // end of ReadCommBlock()
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 ;
}
dwNowTick = GetTickCount();
if(dwNowTick < dwLastTick)
{
dwLastTick = dwNowTick;
}
dwGoneTime = dwNowTick - dwLastTick;
//TRACE("gon time = %lu\n",dwGoneTime);
}while((nReadCount < nMaxLength) && (dwGoneTime < dwTimeOut));
return (nReadCount);
}//end ReadCommBlockEx
BOOL WriteCommBlock( CComStatus& comDev, LPSTR lpByte , DWORD dwBytesToWrite)
{
BOOL fWriteStat ;
DWORD dwBytesWritten ;
DWORD dwErrorFlags;
DWORD dwError;
DWORD dwBytesSent=0;
COMSTAT ComStat;
char szError[ 128 ] ;
fWriteStat = WriteFile( comDev.m_hCom , lpByte, dwBytesToWrite,&dwBytesWritten, &( comDev.m_wtos) ) ;
if (!fWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
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 ) ;
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
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 ) ;
return ( FALSE );
}
}
return ( TRUE ) ;
} // end of WriteCommBlock()
四、小结
以上给出了用Win32 API设计串行通信的基本思路,在实际应用中,我们可以利用Win32 API设计出满足各种需要的串行通信程序。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
