Link to home
Start Free TrialLog in
Avatar of zebada
zebada

asked on

WaitCommEvent failing

I have the following code segents in my app:

  ovlap: OVERLAPPED;

  ...

  ovlap.hEvent := CreateEvent(nil,true,false,PChar('VPW'));

  ...

  SetCommMask(fd,EV_DSR);

  ...

  fd := CreateFile(PChar(Name),GENERIC_READ+GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL+FILE_FLAG_OVERLAPPED,0);
  if ( fd=INVALID_HANDLE_VALUE ) then
    Raise EVpw.CreateFmt('Can''t open %s. %s',[Name,WinMsg(GetLastError)]);

  ...

  if ( not WaitCommEvent(fd,flag,@ovlap) ) then
  begin
    err := GetLastError;
    if ( err<>ERROR_IO_PENDING ) then
      Raise EVpw.CreateFmt('WaitCommEvent(~DSR) failed for %s. %s',[Name,WinMsg(err)]);
    state := WaitForSingleObject(ovlap.hEvent,10);

  ...

  The call to WaitCommEvent returns FALSE and
  err is set to 87="The parameter is incorrect".

Can anyone tell me why and how to fix it?
Avatar of inthe
inthe

hi,
i cant see why your getting that error but if you want i have an example of waiting for an event on the comport using WaitComEvent(),WaitForSingleObject etc..
Avatar of zebada

ASKER

Hi inthe,

An example would be great.

I have found that I don't get the error on the first few times. Only after I have called WaitCommEvent maybe three or four times. If I put a SetCommMask before WaitCommEvent then I don't get any errors - because SetCommMask will clear any pending waits. I think it might be something to do with the fact that I might be trying to wait on an event that I am already waiting on, if you know what I mean.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zebada

ASKER

Thanks Barry,

By looking at the code I figured out where I was going wrong.

Thanks.