Link to home
Start Free TrialLog in
Avatar of warheat001
warheat001

asked on

Communicating with COM Ports

Hi,

I am not much that familiar with Delphi but I was wondering if any of you could help me out in this problem.

I was wondering if I could be detailed about how I can connect to the COM ports of the computer. For example I have a router connected to a COM Port. How can I connect to that port and read/send data and so on. Or I have a software that communicates with the COM port which is connected to an equipment. How can I connect to that port and read/send data through it.

I know this is going to be a little complex one. But if it isn't a bother, could you kind of help me out with some explanations.

Thanks for you help...
ASKER CERTIFIED SOLUTION
Avatar of monir
monir

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
SOLUTION
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
You don't need a component at all.

Here's a unit I wrote which reads data off a custom trackerball. Writing to the COM port is the same principle.

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

Or, you could just open your port as a normal file.
Filenames like "COM1", "LPT1", etc. work fine with all normal file routines.
function InPort(PortAddr: word): byte;
{$IFDEF VER90}
assembler; stdcall;
asm
        mov dx,PortAddr
        in al,dx
end;
{$ELSE}
begin
Result := Port[PortAddr];
end;
{$ENDIF}

procedure OutPort(PortAddr: word; Databyte: byte);
{$IFDEF VER90}
assembler; stdcall;
asm
   mov al,Databyte
   mov dx,PortAddr
   out dx,al
end;
{$ELSE}
begin
Port[PortAddr] := DataByte;
end;
{$ENDIF}

hope it helped ...
SOLUTION
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