Solved

Controlling RS232 PINS Status

Posted on 2003-10-26
13
820 Views
Last Modified: 2010-04-03
i need to control an electronical relay that get's logical 0(GND) or 1(5Volts) my idea was to give that data using rs232 which i used a little bit before with motorola's assembly, i need to be able to control the status of only one pin of the connector no need for input reading just output.

this is the pinout for the rs232 as i know it:
Pin 1 Received Line Signal Detector
Pin 2 Received Data
Pin 3 Transmit Data
Pin 4 Data Terminal Ready
Pin 5 Signal Ground
Pin 6 Data Set Ready
Pin 7 Request To Send
Pin 8 Clear To Send
Pin 9 Ring Indicator

i'll appreciate any help you can give me

Thanks in advance,
Daniel Leicht.
0
Comment
Question by:tomerlei
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 16

Expert Comment

by:_nn_
ID: 9622343
You can control 2 pins (DTR and RTS) using the EscapeCommFunction() Win32 API :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/escapecommfunction.asp

Before using them, you should make sure you have disabled hardware flowcontrol (the fRtsControl and fDtrControl fields in the DCB structure passed to SetCommState())
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9622459
Alternatively, use the parallel port.

Geoff M.
0
 
LVL 4

Author Comment

by:tomerlei
ID: 9625108
what do u mean by disabling the hardware flow control?
can you give me an example of how to use all this procedures?
0
 
LVL 16

Accepted Solution

by:
_nn_ earned 125 total points
ID: 9625769
Well, I allowed myself to reply here, because the question was more of a Win32 one than a Delphi one. Actually it's been a long time since I last coded in that language and I'm not too sure what's the best way. But if the definitions below still apply for the latest Delphi environments :

  dcb_DTRControl       = $00000030;
  dcb_RTSControl       = $00003000;

Then I'd use something like :

  GetCommState(hPort, sDCB);
  sDCB.Flags:= sDCB.Flags and not (dcb_DTRControl or dcb_RTSControl);
  SetCommState(hPort, sDCB);

Afterward, you can use any of these :

  EscapeCommFunction(hPort, SETDTR);
  EscapeCommFunction(hPort, CLRDTR);
  EscapeCommFunction(hPort, SETRTS);
  EscapeCommFunction(hPort, CLRRTS);
0
 
LVL 4

Author Comment

by:tomerlei
ID: 9626212
dcb_DTRControl       = $00000030;
dcb_RTSControl       = $00003000;

where do i input those values? do i have to include anything else in my "uses" line?
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9626266
The Windows unit should have been enough. Else, just declare these as constants.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 13

Expert Comment

by:SteH
ID: 9626449
For a relay which needs TTL voltages (0-5 V) you should use the parallel port which uses those levels. The serial ports uses +- ~12 V for low and high. Check your relay whether it can stand those voltages!

0
 
LVL 4

Author Comment

by:tomerlei
ID: 9626542
my relay uses both 12volt and 5volts but i'll be interested to hear about how to controll the data register of the parallel port too. maybe i'll post that on another question later on.
0
 
LVL 4

Author Comment

by:tomerlei
ID: 9626590
btw, the hPort and sDCB how should be defined?
i didn't have much use of API earlier so my knowledge in it is not so good.

thanks in advance,
Daniel Leicht.
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9626627
hPort should be a THandle you'll get by opening the port with the CreateFile() API.
sDCB is a TDCB (or is it _DCB ? I don't remember exactly) structure.
0
 
LVL 4

Author Comment

by:tomerlei
ID: 9627082
CreateFile()? why should i use it?
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9627131
How do you open the COM port ?

Maybe some (related) source code will help. This is just something I found by googling "EscapeCommFunction Delphi", look at the TComm.Open method :
http://homepages.borland.com/efg2lab/Library/UseNet/1999/1104b.txt

If you can get a Win32 handle by some other mean, then fine. But CreateFile() is the Win32 API normally used to get it.
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9627161
CreateFile is a misnomer. It opens existing files as well as comm ports etc. Here is some code to open and read from comm port. Just put the stuff that nn has suggested in the appropriate places.


unit TBall;

// Purpose: Handles trackerball interface via a comm port

{
$Log$
}

interface

uses
      Dialogs,
      Windows;

procedure TBallDone;
procedure TBallInit(comportid : string);
procedure TBallTenMSTimer;

implementation

uses
      Display,
      Globals;

var
      CommPort : THandle;                  // Handle for the comm port
      Byte1, Byte2, Byte3 : byte;      // Most recently received bytes
      ByteCount : integer;            // Number of bytes received

{---------------------------------------------------------------------------------------------------
TBallDone
---------
Purpose:        Closes the communications port
Inputs:         None
Returns:        Nothing
Preconditions:  None
Postconditions: Comm port closed if open
Actions:        If the handle is non-zero, then close the handle and zero the handle.
---------------------------------------------------------------------------------------------------}
procedure TBallDone;
begin
      if CommPort <> 0 then begin
            CloseHandle(CommPort);
            CommPort := 0;
      end;
end;

{---------------------------------------------------------------------------------------------------
TBallInit
---------
Purpose:        Initialises the communications port
Inputs:         comportid - Identity (e.g. COM2: ) of the communications port
Returns:        Nothing
Preconditions:  None
Postconditions: Comm port opened or error shown
Actions:        Open the comm port. If an error occurs then show the error. Otherwise, initialise
                the port to the appropriate speed etc.
---------------------------------------------------------------------------------------------------}
procedure TBallInit(comportid : string);
var
      dcb : TDCB;
      t : TCommTimeouts;
begin
      if CommPort <> 0 then TBallDone;
      CommPort := CreateFile(PChar(comportid), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0);
      if CommPort <> 0 then begin
            SetupComm(CommPort, 256, 256);

            dcb.DCBlength := SizeOf(dcb);
            GetCommState(CommPort, dcb);

            dcb.BaudRate := CBR_1200;
            dcb.Parity := NOPARITY;
            dcb.ByteSize := 8;
            dcb.StopBits := ONESTOPBIT;
            SetCommState(CommPort, dcb);

            t.ReadIntervalTimeout := MAXDWORD;
            t.ReadTotalTimeoutConstant := 0;
            t.ReadTotalTimeoutMultiplier := 0;
            t.WriteTotalTimeoutMultiplier := 0;
            t.WriteTotalTimeoutConstant := 0;
            SetCommTimeouts(CommPort, t);
      end else begin
            ShowMessage('Failed to open hardware port ' + comportid + ' for trackerball. Hardware will not be available in this session.');
      end;
end;

{---------------------------------------------------------------------------------------------------
TBallTenMSTimer
---------------
Purpose:        Checks the port for activity
Inputs:         None
Returns:        Nothing
Preconditions:  Comm port opened
Postconditions: Mouse cursor moved if necessary
Actions:        Read any bytes from the comm port if open. If any are received, process them. Three
                bytes are received per movement, although not necessarily at the same time. Certain
                bit patterns in the 3 bytes denote the X and Y distance, and whether the byte is the
                first of the 3 (ie to synchronise with a triplet).
---------------------------------------------------------------------------------------------------}
procedure TBallTenMSTimer;
      procedure ProcessMouse;
      var
            {lb, rb : boolean;}
            xm, ym, xdiff : integer;
            xc, yc : shortint;
            p : TPoint;
      begin
            {$R-}
            {lb := (Byte1 and 32) = 32;      // Not used at present
            rb := (Byte1 and 16) = 16;}
            xc := ((Byte1 and 3) shl 6) + (Byte2 and 63);
            yc := ((Byte1 and 12) shl 4) + (Byte3 and 63);
                        GetCursorPos(p);
            xm := p.X + xc div 1;
            ym := p.Y - yc div 1;
            if MouseScreenBoundary then DisplayForm.MouseMoveTest(xm, ym, xdiff);
            SetCursorPos(xm, ym);
            {$R+}
      end;
var
      buf : byte;
      numread : cardinal;
begin
      if CommPort <> 0 then begin
            numread := 1;
            while (ReadFile(CommPort, buf, 1, numread, nil) = true) and (numread = 1) do begin
                  if (buf and 64) = 64 then begin
                        bytecount := 0;
                        Byte1 := buf;
                  end else if bytecount = 0 then begin
                        bytecount := 1;
                        Byte2 := buf;
                  end else if bytecount = 1 then begin
                        bytecount := 2;
                        Byte3 := buf;
                        ProcessMouse;
                  end;
            end;
      end;
end;
{--------------------------------------------------------------------------------------------------}
end.


Geoff M.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

18 Experts available now in Live!

Get 1:1 Help Now