Stadi's software page

A magyar változat megtekintéséhez kattints ide

Parallel Port Unit for Delphi

Browsing through the internet you can find plenty of information on how to handle parallel ports from software, but nearly all of these pages are about the old-school port addressing method. That method has some serious limitations: On Windows NT and its successors (2000, XP, Vista, Windows 7), you need a driver to access hardware ports directly. And nowadays many mainboard manufacturers choose to leave out the parallel port from their products. In this case, installing a PCI or PCI Express parallel port card seems a good idea to solve the problem, but they cannot be controlled using the "conventional" method because they have different internal structure and their base address is not among the standard ones'. However, Windows can use these add-in cards flawlessly. So there must be a way to use these cards from a development environment (e.g. Delphi) as well. The solution is simple: we have to use the Windows API. Unfortunately, there is hardly any information available about this topic on the internet. The unit whose details are shown below is the Delphi language translation of the OpenParPort project by Andrey V. Lelikov.

Using the unit

With this unit, your Delphi applications will be able to use parallel ports through the Windows API. The interface is as follows:

type TParPortReg=(TPRData,TPRControl,TPRStatus);

Using the TParPortReg enumerated type, you can easily refer to the data, control and status ports of the parallel port.

function OpenParPort(PortNumber: Integer): THandle;

The OpenParPort function opens the LPTx port where x is PortNumber. You can use the Device Manager to determine what numbers can be used for the ports installed in the computer. The return value is the handle of the port.

procedure WriteParPort(Port: THandle; Reg: TParPortReg; Value: Byte);

The WriteParPort procedure sends Value to the Reg register of Port.

function ReadParPort(Port: THandle; Reg: TParPortReg): Byte;

The ReadParPort function returns the value read from the Reg register of Port.

procedure CloseParPort(Port: THandle);

The CloseParPort can be used to close the Port handle.

Limitations

WriteParPort can only write the TPRData and TPRControl ports.
ReadParPort can only read the TPRStatus port.

Conditions of usage

This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

Test results

This unit was tested on Windows XP Professional x86 SP3 using the integrated LPT port and an NM9835-based PCI card. Please report if you tried this unit out with any other add-in card and/or operating system (especially Vista, Windows 7 or any 64-bit Windows). My e-mail address is stadi at stadi dot hu.

Download

parport.pas - the parport unit

Examples

A demo console application which shows how to use the parport unit is available on a separate page.

However it does not contain the example of listing the available LPT ports of the computer. So take a look at the code snippet below which demonstrates how to load the list of available parallel ports into ComboBox1 when Form1 is shown.

procedure TForm1.FormShow(Sender: TObject);
var
  h: THandle;
  i: Integer;
begin
  ComboBox1.Items.Clear;
  for i:=1 to 20 do
    begin
      h:=OpenParPort(i);
      if h<>INVALID_HANDLE_VALUE then
        begin
          CloseParPort(h);
          ComboBox1.Items.Add('LPT'+IntToStr(i));
        end;
    end;
  if ComboBox1.Items.Count>0 then ComboBox1.ItemIndex:=0;
end;

Credits

This unit is based on the OpenParPort project by Andrey V. Lelikov.

Contact info

Bug reports, advices and "thank you"s :) can be sent to this e-mail address:

stadi at stadi dot hu