Solved

WaitCommEvent failing

Posted on 2001-08-24
4
1,642 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Check Delphi Seattle IOS app without MAC ? 1 92
Convert Jpg /PNG To GIF 5 120
Delphi : could not find program, '...exe' 2 149
Working with hours 3 46
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now