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
Solved

Controlling RS232 PINS Status

Posted on 2003-10-26
13
853 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 96
code issue 8 154
find a node in VST 2 70
how to make 2 step back? ..\..\ ExtractFilePath (paramstr(0)) 5 50
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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