Controlling RS232 PINS Status

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.
LVL 4
tomerleiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_nn_Commented:
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
gmayoCommented:
Alternatively, use the parallel port.

Geoff M.
0
tomerleiAuthor Commented:
what do u mean by disabling the hardware flow control?
can you give me an example of how to use all this procedures?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

_nn_Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomerleiAuthor Commented:
dcb_DTRControl       = $00000030;
dcb_RTSControl       = $00003000;

where do i input those values? do i have to include anything else in my "uses" line?
0
_nn_Commented:
The Windows unit should have been enough. Else, just declare these as constants.
0
SteHCommented:
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
tomerleiAuthor Commented:
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
tomerleiAuthor Commented:
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
_nn_Commented:
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
tomerleiAuthor Commented:
CreateFile()? why should i use it?
0
_nn_Commented:
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
gmayoCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.