Solved

WaitCommEvent failing

Posted on 2001-08-24
4
1,660 Views
Last Modified: 2007-11-27
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?
0
Comment
Question by:zebada
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6424502
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..
0
 
LVL 6

Author Comment

by:zebada
ID: 6425060
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.
0
 
LVL 17

Accepted Solution

by:
inthe earned 100 total points
ID: 6425382
hi zebada,
here is the example,i founbd it a while back when i wanted some port stuff without using components.
hope it is useful for you ;-)
Regards Barry



type
TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    bContinue   :  boolean;
    hcomm       :  THandle;
    CommTimeouts   : TCommTimeouts;
    overlappedRead : TOverlapped;
    overlappedWait : TOverlapped;
    dwEventMask    : DWord;
    inputbuffer    : array [0..2047] of byte;
    function CheckForCharacters:boolean;
    procedure SetupSerial;
  public
  end;
var
  Form1: TForm1;
implementation
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  dwDummy   : dword;
  bWait          : boolean;
  bStartWait   : boolean;
begin
  SetupSerial;
  bContinue:=true;
  bStartWait:=true;
  bwait:=false;
  while bContinue do // Now continue looping every Second
   begin
    if bStartWait then
     begin
       bStartWait:=false;
       bWait:=WaitCommEvent(hcomm,dwEventMask,@overlappedWait);
     end;
    if not bWait then
     begin
     // showmessage(syserrormessage(getlasterror));
        If WAIT_OBJECT_0 = WaitForSingleObject(overlappedWait.hEvent,1000) then
            if GetOverlappedResult(hComm,overlappedWait,dwdummy,false) then
              if ((dwEventMask and EV_RxChar)=EV_RxChar) then
               begin
                 // Character(s) arrived- now do a readfile operation
                 CheckForCharacters;
                 bStartWait:=true;
               end;
     end else
          begin
            if GetOverlappedResult(hComm,overlappedWait,dwdummy,false) then
              if ((dwEventMask and EV_RxChar)=EV_RxChar) then
               begin
                 // Character(s) already there- now do a readfile operation
                 CheckForCharacters;
                 bStartWait:=true;
               end;
          end;
      application.processmessages;
   end; // while bContinue
  //Close the commport
  CloseHandle (hComm );
  CloseHandle (overlappedWait.hEvent);
  CloseHandle (overlappedRead.hEvent);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 bContinue:=false;
end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  bContinue:=false;
end;

procedure TForm1.SetupSerial;
var dcb            :  TDCB;
begin
  Fillchar(overlappedread,SizeOf(overlappedread),0);
  Fillchar(overlappedwait,SizeOf(overlappedwait),0);
  // Create manual-reset events in nonsignaled state
  overlappedread.hevent:=CreateEvent(nil,True,True,nil);
  overlappedwait.hevent:=CreateEvent(nil,True,True,nil);
  hComm := CreateFile( PChar('COM1'),
                                     GENERIC_READ+GENERIC_WRITE,
                                     0, {no Filesharing}
                                    nil, {security always 0}
                                   OPEN_EXISTING,
                                   FILE_FLAG_OVERLAPPED, 0);
  GetCommState( hComm, dcb );
  dcb.Flags:=1 + DTR_Control_Enable shl 4 +
                 RTS_CONTROL_HANDSHAKE shl 12; // Handshake Protocol
  dcb.Baudrate:=CBR_9600;
  dcb.ByteSize:=8;        // 8 Databits
  dcb.Parity:=NOPARITY;   // No Parity
  dcb.StopBits:=ONESTOPBIT;
  SetCommState(hcomm,dcb);
  GetCommTimeouts( hComm, commtimeouts );
  commtimeouts.ReadIntervalTimeout:= 20;
  commtimeouts.ReadTotalTimeoutMultiplier:=0;
  commtimeouts.ReadTotalTimeoutConstant:= 250;
  SetCommTimeOuts(hcomm,commtimeouts);
  GetCommMask(hcomm,dwEventMask);
  dwEventMask:=dwEventMask or EV_RXCHAR;
  SetCommMask(hcomm,dwEventMask);
  SetupComm(hcomm,2048,2048);
end;

function TForm1.CheckForCharacters: boolean;
var
  dwDummy : DWord;
  dwBytesRead: DWord;
  s:string;
begin
 ReadFile(hcomm,inputbuffer,sizeof(inputbuffer),dwDummy,@overlappedRead);
 if GetOverlappedResult(hComm,overlappedRead,dwBytesRead,true) then
  begin
   setLength(s,dwBytesRead);
   move(inputbuffer,s[1],dwBytesRead);
   memo1.text:=memo1.text+s;
   result:=true;
  end else result:=false;

end;
0
 
LVL 6

Author Comment

by:zebada
ID: 6440268
Thanks Barry,

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

Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question