WaitCommEvent要设成一个循环。给你个例子:DWORD FAR PASCAL CommWatchProc( LPSTR lpData )
{
   DWORD       dwEvtMask ;
   NPTTYINFO   npTTYInfo = (NPTTYINFO) lpData ;
   OVERLAPPED  os ;
int        nLength ;
   BYTE       abIn[ MAXBLOCK + 1] ;   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( COMDEV( npTTYInfo ), EV_RXCHAR ))
      return ( FALSE ) ;   while ( CONNECTED( npTTYInfo ) )
   {
      dwEvtMask = 0 ;      WaitCommEvent( COMDEV( npTTYInfo ), &dwEvtMask, NULL );      if ((dwEvtMask & EV_RXCHAR) == EV_RXCHAR)
      {
         do
         {
            if (nLength = ReadCommBlock( hTTYWnd, (LPSTR) abIn, MAXBLOCK ))
            {
               WriteTTYBlock( hTTYWnd, (LPSTR) abIn, nLength ) ;               // 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()

解决方案 »

  1.   

    完整的例子你到MDSN上去找。补充:typedef struct tagTTYINFO
    {
       HANDLE  idComDev ;
       BYTE    bPort, abScreen[ MAXROWS * MAXCOLS ] ;
       BOOL    fConnected, fXonXoff, fLocalEcho, fNewLine, fAutoWrap,
               fUseCNReceive, fDisplayErrors;
       BYTE    bByteSize, bFlowCtrl, bParity, bStopBits ;
       DWORD   dwBaudRate ;
       WORD    wCursorState ;
       HFONT   hTTYFont ;
       LOGFONT lfTTYFont ;
       DWORD   rgbFGColor ;
       int     xSize, ySize, xScroll, yScroll, xOffset, yOffset,
               nColumn, nRow, xChar, yChar ;
       HANDLE      hPostEvent, hWatchThread, hWatchEvent ;
       HWND        hTermWnd ;
       DWORD       dwThreadID ;
       OVERLAPPED  osWrite, osRead ;} TTYINFO, NEAR *NPTTYINFO ;// macros ( for easier readability )#define GETHINST( x )  ((HINSTANCE) GetWindowLong( x, GWL_HINSTANCE ))
    #define GETNPTTYINFO( x ) ((NPTTYINFO) GetWindowLong( x, GWL_NPTTYINFO ))
    #define SETNPTTYINFO( x, y ) SetWindowLong( x, GWL_NPTTYINFO, (LONG) y )#define COMDEV( x ) (x -> idComDev)
    #define PORT( x )   (x -> bPort)
    #define SCREEN( x ) (x -> abScreen)
    #define CONNECTED( x ) (x -> fConnected)
    #define XONXOFF( x ) (x -> fXonXoff)
    #define LOCALECHO( x ) (x -> fLocalEcho)
    #define NEWLINE( x ) (x -> fNewLine)
    #define AUTOWRAP( x ) (x -> fAutoWrap)
    #define BYTESIZE( x ) (x -> bByteSize)
    #define FLOWCTRL( x ) (x -> bFlowCtrl)
    #define PARITY( x ) (x -> bParity)
    #define STOPBITS( x ) (x -> bStopBits)
    #define BAUDRATE( x ) (x -> dwBaudRate)
    #define CURSORSTATE( x ) (x -> wCursorState)
    #define HTTYFONT( x ) (x -> hTTYFont)
    #define LFTTYFONT( x ) (x -> lfTTYFont)
    #define FGCOLOR( x ) (x -> rgbFGColor)
    #define XSIZE( x ) (x -> xSize)
    #define YSIZE( x ) (x -> ySize)
    #define XSCROLL( x ) (x -> xScroll)
    #define YSCROLL( x ) (x -> yScroll)
    #define XOFFSET( x ) (x -> xOffset)
    #define YOFFSET( x ) (x -> yOffset)
    #define COLUMN( x ) (x -> nColumn)
    #define ROW( x ) (x -> nRow)
    #define XCHAR( x ) (x -> xChar)
    #define YCHAR( x ) (x -> yChar )
    #define USECNRECEIVE( x ) (x -> fUseCNReceive)
    #define DISPLAYERRORS( x ) (x -> fDisplayErrors)#define POSTEVENT( x ) (x -> hPostEvent)
    #define TERMWND( x ) (x -> hTermWnd)
    #define HTHREAD( x ) (x -> hWatchThread)
    #define THREADID( x ) (x -> dwThreadID)
    #define WRITE_OS( x ) (x -> osWrite)
    #define READ_OS( x ) (x -> osRead)#define SET_PROP( x, y, z )  SetProp( x, MAKEINTATOM( y ), z )
    #define GET_PROP( x, y )     GetProp( x, MAKEINTATOM( y ) )
    #define REMOVE_PROP( x, y )  RemoveProp( x, MAKEINTATOM( y ) )
      

  2.   

    我在MSDN找不到,请告无在MSDN何处找?
      

  3.   

    菜单 GO ----->URL-----> jump to this url 输入mk:@MSITStore:D:\Microsoft%20Visual%20Studio\MSDN\2001JAN\1033\vcsample.chm::/html/vcsmpserialsampleforcommunicationsdemonstration.htm
      

  4.   

    或者http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample98/html/vcsmpserialsampleforcommunicationsdemonstration.asp