Chinaunix首页 | 论坛 | 博客
  • 博客访问: 430714
  • 博文数量: 56
  • 博客积分: 2262
  • 博客等级: 大尉
  • 技术积分: 711
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-08 20:04
文章分类

全部博文(56)

文章存档

2013年(1)

2012年(9)

2011年(10)

2010年(7)

2009年(7)

2008年(22)

我的朋友

分类: WINDOWS

2008-08-20 19:27:16

 

CCESeries *ceSeries = (CCESeries*)lparam;
    
    DWORD    evtMask;
    BYTE * readBuf = NULL;//读取的字节

    DWORD actualReadLen=0;//实际读取的字节数

    DWORD willReadLen;
    
    DWORD dwReadErrors;
    COMSTAT    cmState;
    
    // 清空缓冲,并检查串口是否打开。

    ASSERT(ceSeries->m_hComm !=INVALID_HANDLE_VALUE);
    
    
    //清空串口

    PurgeComm(ceSeries->m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR );
    
    SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
    while (TRUE)
    {     
        if (WaitCommEvent(ceSeries->m_hComm,&evtMask,0))
        {            
            SetCommMask (ceSeries->m_hComm, EV_RXCHAR | EV_CTS | EV_DSR );
            //表示串口收到字符        

            if (evtMask & EV_RXCHAR)
            {
                
                ClearCommError(ceSeries->m_hComm,&dwReadErrors,&cmState);
                willReadLen = cmState.cbInQue ;
                if (willReadLen <= 0)
                {
                    continue;
                }
                
                readBuf = new BYTE[willReadLen];
                ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
                
                //如果读取的数据大于0,

                if (actualReadLen>0)
                {
                    //触发读取回调函数

                    ceSeries->m_OnSeriesRead(ceSeries->m_pPortOwner,readBuf,actualReadLen);
                }
            }
        }
        //如果收到读线程退出信号,则退出线程

        if (WaitForSingleObject(ceSeries->m_hReadCloseEvent,500) == WAIT_OBJECT_0)
        {
            break;
        }
    }
    return 0;

阅读(3304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~