Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ask a switch on the serial port

Posted on 2003-03-21
13
Medium Priority
?
418 Views
Last Modified: 2012-05-04
SW: Delphi 6
OS: W98SE
My customer has a mech. switchcontact on the serialport. When the switch is pushed, the program has to start an 2nd program. How can i see the status of the switch?
Thanks for your help
0
Comment
Question by:tzigan
  • 7
  • 6
13 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 8179578
Open the com port like this:

Fd := CreateFile('Com1',GENERIC_READ+GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

Check the status of the control lines like this:
if ( GetCommModemStatus(Fd,state) ) then

for the dsr pin:
  Result := (state and MS_DSR_ON)<>0;

or for the cts pin:
  Result := (state and MS_CTS_ON)<>0;

Regards
Paul
0
 

Author Comment

by:tzigan
ID: 8179624
Paul, Which type is Fd?
Which ressources do i have to install to use the written variables?
0
 
LVL 6

Accepted Solution

by:
zebada earned 1600 total points
ID: 8179725
Fd: integer;

state: LongWord;

MS_DSR_ON and MS_CTS_ON are in the Windows unit so just include Windows in your units clause.

Regards
Paul


P.S.
From the windows unit:


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tzigan
ID: 8179770
Thanks for the info. I'll check it in the next days.By problems i'll contact you again.
0
 
LVL 6

Expert Comment

by:zebada
ID: 8179789
Sure :)
0
 

Author Comment

by:tzigan
ID: 8386563
Paul, i tested with the following code.
I can't see any reaction when i close the connector CTS - TxD.
Please correct this short program.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Button2: TButton;
    Memo1: TMemo;
    Shape5: TShape;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var  Form1: TForm1;

implementation

{$R *.dfm}
 
var  Fd: integer;
     state: LongWord;
     i : Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  i:=0;
  memo1.Clear;
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
  end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Fd := CreateFile  ('Com1',GENERIC_READ+GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  for i:=1 to 1000 do begin
    Caption:=inttostr(i);
    if ( GetCommModemStatus(Fd,state) ) then
    case state of
      MS_CTS_ON   : memo1.Lines.add('1');
      MS_DSR_ON   : memo1.Lines.add('2');
      MS_RING_ON  : memo1.Lines.add('3');
      MS_RLSD_ON  : memo1.Lines.add('4')
      else     memo1.Lines.add('0');
      end;
    end;
  end;

end.
0
 
LVL 6

Expert Comment

by:zebada
ID: 8387381
Try closing the connection between CTS and ground (pin 5 I think from memory) - not CTS and TxD
0
 

Author Comment

by:tzigan
ID: 8387415
Thanks, i try it and inform you tomorrow. Great.
0
 
LVL 6

Expert Comment

by:zebada
ID: 8387502
There is also a bit of a problem with the code - it won't work that way - I will post a working example...
0
 

Author Comment

by:tzigan
ID: 8387585
I would be happy to get it.
0
 
LVL 6

Expert Comment

by:zebada
ID: 8387652
To cause the CTS state to change you need to open/close the contact between CTS and RTS.

To cause the DSR state to change you need to open/close the contact between DSR and DTR.

The program sets both the DTR and the RTS pins to a high voltage. That way they can be used to pull the CTS and/or the DSR pins high - causing a state change.

CTS = pin 8
RTS = pin 7
DTR = pin 4
DSR = pin 6

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  stopped: boolean;
  Fd: THandle;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  stopped := true;
  Button1.Caption := 'Start';
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Stopped := true;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  state: DWORD;
  msg: string;
begin
  if ( not stopped ) then
  begin
    stopped := true;
    exit;
  end;

  Button1.Caption := 'Stop';
  try
    Fd := CreateFile('Com1',GENERIC_READ+GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

    state := SETDTR;
    if ( not EscapeCommFunction(Fd,state) ) then
      Raise Exception.Create('Could not set DTR ON, EscapeCommFunction failed');
    state := SETRTS;
    if ( not EscapeCommFunction(Fd,state) ) then
      Raise Exception.Create('Could not set RTS ON, EscapeCommFunction failed');

    try
      stopped := false;
      while ( not stopped ) do
      begin
        Sleep(100);
        if ( not GetCommModemStatus(Fd,state) ) then
          Raise Exception.Create('GetCommModemStatus failed');

        msg := 'CTS state is: ';
        if ( (state and MS_CTS_ON)<>0 ) then
          msg := msg+'ON'
        else
          msg := msg+'OFF';
        msg := msg+' DSR state is: ';
        if ( (state and MS_DSR_ON)<>0 ) then
          msg := msg+'ON'
        else
          msg := msg+'OFF';

        memo1.Lines.add(msg);
        Application.ProcessMessages;
      end;
    finally
      CloseHandle(Fd);
    end;
  finally
    Button1.Caption := 'Start';
  end;
end;

end.


0
 

Author Comment

by:tzigan
ID: 8387792
Great, I test it tonight, Thanks very much. rgds. Thomas
0
 

Author Comment

by:tzigan
ID: 8393256
Hi Zebada, unbelieveble, a perfect code. It works. The mainfailure was the contact i used before. Thanks very much.
How can i give you points?
rgds. Thomas
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

581 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