Link to home
Start Free TrialLog in
Avatar of tomerlei
tomerlei

asked on

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.
Avatar of _nn_
_nn_

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())
Alternatively, use the parallel port.

Geoff M.
Avatar of tomerlei

ASKER

what do u mean by disabling the hardware flow control?
can you give me an example of how to use all this procedures?
ASKER CERTIFIED SOLUTION
Avatar of _nn_
_nn_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dcb_DTRControl       = $00000030;
dcb_RTSControl       = $00003000;

where do i input those values? do i have to include anything else in my "uses" line?
The Windows unit should have been enough. Else, just declare these as constants.
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!

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.
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.
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.
CreateFile()? why should i use it?
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.
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.