本帖最后由 cuibo1123 于 2009-09-28 17:33:51 编辑

解决方案 »

  1.   

    这个不太清楚   应该有响应的事件的  
    IRP_MJ_READ    但是不知道应用层是什么  
      

  2.   

    可以在驱动里面做,用IRP派发实现
      

  3.   

    应用层下也是可以的,WaitCommEvent等待串口事件
    BOOL WINAPI WaitCommEvent(
      __in   HANDLE hFile,
      __out  LPDWORD lpEvtMask,
      __in   LPOVERLAPPED lpOverlapped
    );
    Value Meaning 
    EV_BREAK
    0x0040
     A break was detected on input.
     
    EV_CTS
    0x0008
     The CTS (clear-to-send) signal changed state.
     
    EV_DSR
    0x0010
     The DSR (data-set-ready) signal changed state.
     
    EV_ERR
    0x0080
     A line-status error occurred. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
     
    EV_RING
    0x0100
     A ring indicator was detected.
     
    EV_RLSD
    0x0020
     The RLSD (receive-line-signal-detect) signal changed state.
     
    EV_RXCHAR
    0x0001
     A character was received and placed in the input buffer.
     
    EV_RXFLAG
    0x0002
     The event character was received and placed in the input buffer. The event character is specified in the device's DCB structure, which is applied to a serial port by using the SetCommState function.
     
    EV_TXEMPTY
    0x0004
     The last character in the output buffer was sent.
     Example
    DWORD WINAPI Com1ThreadProcess(HWND hWnd//主窗口句柄) 

     DWORD wEven; 
     char str[10]; //读入数据 
     SetCommMask(hComm1, EV_RXCHAR | EV_TXEMPTY); 
     while (TRUE) 
     { 
      WaitCommEvent(hComm1, &wEven, NULL); 
      if(wEven = 0) 
      { 
       CloseHandle(hCommThread1); 
       hCommThread1 = NULL; 
       ExitThread(0); 
      } 
      else 
      { 
       switch (wEven) 
       { 
        case EV_TXEMPTY: 
         if (wTxPos < wTxLen) 
         { 
          //在串口1写入数据 
          DWORD wCount; //写入的字节数 
          WriteFile(hComm1, com1Data.TxBuf[wTxPos], 1, &wCount, NULL); 
          com1Data.wTxPos++; 
         } 
         break; 
        case EV_RXCHAR: 
         if (com1Data.wRxPos < com1Data.wRxLen) 
         { 
          //读取串口数据, 处理收到的数据 
          DWORD wCount; //读取的字节数 
          ReadFile(hComm1, com1Data.RxBuf[wRxPos], 1, &wCount, NULL); 
          com1Data.wRxPos++; 
          if(com1Data.wRxPos== com1Data.wRxLen); 
           ::PostMessage(hWnd, COM_SENDCHAR, 0, 1); 
         } 
         break; 
        } 
       } 
      } 
     } 
     return TRUE; 
      

  4.   

    WaitCommEvent 怎么用??是阻塞等待串口数据?  和socket wait那个东西一样吗?
      

  5.   

    方法很多了
    1,开两个线程,一个readfile.....,一个writefile.........
    2,同样不过可以使用overlapped模式进行异步操作..............
    3.开一个线程,waitcommevent.......然后操作............
    4.使用readfileex,writefileex,这个时候不需要线程.................不过要指定回调函数...................
      

  6.   

    这是操作系统实现的原因,最好还是自己开线程来进行读写数据。另外,我不建议使用WaitCommEvent来等待数据到达的事件。用overlapped来处理是比较合适的。