用WaitCommEvent 监视串口的事件。

解决方案 »

  1.   

    WaitCommEvent(port->m_hComm, &Event, &port->m_ov);
      

  2.   

    /*******************************************************
    function:
    CommWatchProc
    describ:
    监视串口数据
    ********************************************************/
    UINT CommWatchProc( LPVOID lpData )
    {
      DWORD       dwEvtMask ;
      //NPTTYINFO   npTTYInfo = (NPTTYINFO) lpData ;
      OVERLAPPED  os ;
      int        nLength ;
      //BYTE       abIn[ MAXBLOCK + 1] ;
      CComStatus * pCom = (CComStatus *)lpData;  memset( &os, 0, sizeof( OVERLAPPED ) ) ;  // create I/O event used for overlapped read  os.hEvent = CreateEvent( NULL,    // no security
                               TRUE,    // explicit reset req
                               FALSE,   // initial event reset
                               NULL ) ; // no name
      if (os.hEvent == NULL)
      {
        MessageBox( NULL, "Failed to create event for thread!", "TTY Error!",
                    MB_ICONEXCLAMATION | MB_OK ) ;
        return ( FALSE ) ;
      }  if (!SetCommMask( pCom->m_hCom, EV_RXCHAR | EV_RXFLAG  ))
        return ( FALSE ) ;
      char buf[256];
      while ( pCom->m_bConnected )
      {
        dwEvtMask = 0 ;    WaitCommEvent( pCom->m_hCom, &dwEvtMask, NULL );    if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
        {
    /*         do
             {*/
          if ((nLength = ReadCommBlock( *pCom, (LPSTR) buf, 255 ))!=0)
          {
                   //WriteTTYBlock( hTTYWnd, (LPSTR) abIn, nLength ) ;
            buf[nLength]='\0';
            //ShowMessage(buf);
                   // force a paint               //UpdateWindow( hTTYWnd ) ;
          }
    //         }
    //         while ( nLength > 0 ) ;
        }
      }   // 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()