Solved

WaitCommEvent failing

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Strange message in Delphi 10 Seattle 1 464
loading dll from external path 1 197
Delphi XE10, MySQL Query 4 109
Simple Delphi Question 9 77
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

20 Experts available now in Live!

Get 1:1 Help Now