我用异步方式创建了一个串口,
使用SetCommMask设置了一个EV_RXCHAR事件,
然后再定义一个TOverlapped结构,它的hEvent := CreateEvent(nil, true, true, nil)
此时,利用WaitCommEvent(hcom, dwEvtMask, @lpol)来截获事件。但运行时却失败,现象是这样的,
第一次执行WaitCommEvent就返回ERROR_IO_PENDING,(没有输入的,只是线程开始了)
之后执行WaitCommEvent就总是返回ERROR_INVALID_PARAMETER(87)了,
而且不管是不是有输入都一样,敬请各位大侠帮忙看看,先谢了!

解决方案 »

  1.   

    噢,忘了一点,
    我是在win2000下调试的
      

  2.   

    WaitCommEvent(hcom, dwEvtMask, @lpol)
    @lpol是一个什么变量,改为lpol试试
      

  3.   

    lpol是TOverlapped,也就是_OVERLAPPED结构
    @lpol就是指向该变量的指针,
    WaitCommEvent函数的第三个参数要求lpoverlapped,是一个指针,
    如果改为lpol就编译不过去了
    (我是用DELPHI写的,不过用的是API,应该一样的)
      

  4.   

    创建部分:
    var
      hcom: Thandle;
      lpol: TOverlapped;
      ComTimeout: _COMMTIMEOUTS;
      dwLastError: DWORD;
      ReadThread: TComm;   //接收线程
    ......
    hcom := CreateFile('com1', GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, 0);  
      if hcom=INVALID_HANDLE_VALUE then 
        Application.Terminate;
      ComTimeout.ReadIntervalTimeout := 100;
      ComTimeout.ReadTotalTimeoutMultiplier := 100;
      ComTimeout.ReadTotalTimeoutConstant := 100;
      ComTimeout.WriteTotalTimeoutMultiplier := 100;
      ComTimeout.WriteTotalTimeoutConstant := 100;
      SetCommTimeouts(hcom, ComTimeout);  SetupComm(hcom, 4096, 4096);  
      GetCommState(hcom, lpdcb);    
      lpdcb.BaudRate := 2400;
      lpdcb.StopBits := ONESTOPBIT;
      lpdcb.ByteSize := 8;
      lpdcb.Parity := NOPARITY;   
      if not SetCommState(hcom, lpdcb) then 
        Application.Terminate;
      SetCommMask(hcom, EV_RXCHAR);
      ReadThread := Tcomm.Create(False);
    ......
    procedure TComm.Execute;//线程主体
    var
      dwEvtMask,LError: Dword;
      Wait: Boolean;
      Clear: Boolean;
      Coms: TComStat;
      cbNum: Integer;
      ReadNumber, lpErrors, dwTransfer: Cardinal;
    Begin
      FillChar(lpol, Sizeof(lpol), 0 );
      lpol.hEvent := CreateEvent(nil, true, true, nil);   
      While True do Begin
        dwEvtMask := 0;
        Wait := WaitCommEvent(hcom, dwEvtMask, @lpol); 
        LError := GetLastError();
        case LError of
          ERROR_IO_PENDING: begin
            GetOverlappedResult(hcom, lpol, dwTransfer, True);
            Wait := True;
          ERROR_INVALID_PARAMETER
            Continue;
          else 
            Break;    
        end;
        if Wait Then Begin
          Clear := ClearCommError(hcom, lpErrors, @Coms);
          if Clear Then Begin
            cbNum := Coms.cbInQue;
            if not ReadFile(hCom, Read_Buffer, cbNum, ReadNumber, @lpol) then 
              if GetLastError = ERROR_IO_PENDING then
                WaitForSingleObject(lpol.hEvent, INFINITE);  
            Synchronize(ShowData);
          end;
        end;
      end;
      CloseHandle(lpol.hEvent);
    end;
    ......
      

  5.   

    http://www.delphibbs.com/delphibbs/dispq.asp?lid=1054436俺照着CB代码译过来的,我也不玩过COM看看吧。