?
Solved

ask a switch on the serial port

Posted on 2003-03-21
13
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 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