Control LPT port under windows XP using Delphi

Another way of controlling the LPT port under Windows 2000 and XP is using the Delphi language. In this case, library, inpout32.dll, is used, which allows controlling LPT port registers.

And now how to do this from the beginning. Start Borland Delphi 7.0 and make a simple form where you can enter Data to be sent to the port, Port Address, buttons for writing to and reading from a port.

If you are familiar with building forms this should be ease task.

Okay, now let’s start programming; first of all wee need to include inpout32.dll in the project. For this, Delphi has several ways, but let’s stay to the easiest one when the library is in the same directory, where the project is. The header in section uses we have to place function prototypes Out32 and Inp32 with special compiler directive external, saying where to find this function.

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

 function Inp32(PortAdr: word): byte; stdcall; external 'inpout32.dll';
 function Out32(PortAdr: word; Data: byte): byte; stdcall; external 'inpout32.dll';

Then let’s go to the button methods. For this, double click on the button in the form, and you will be directed to the editor. First o all, let’s work with wiring to port function. Place data to the variables Port and Data from text fields, convert them from text to numerical data and then call function Out32() with these parameters.

procedure TForm1.Button1Click(Sender: TObject);
var
    Port: word;
    Data: Byte;
begin
    Data:= StrToInt(Edit1.Text);
    Port:= StrToInt(Edit2.Text);
    Out32(Port, Data);
end;

The same way we have to deal with reading. Port Address is taken from the Port Address text field. This is where data will be read from. Then call function Inp32() with port number and output received data to the window.

procedure TForm1.Button2Click(Sender: TObject);
var
    Port: word;
    Data: Byte;
begin
    Port:= StrToInt(Edit3.Text);
    Data:= Inp32(Port);
    MessageDlg('Value: '+ IntToStr(Data), mtInformation, [mbOK], 0);
end;

Compile the program and start. Try to write to the DATA register of LPT port number 0. For this port, the address will be 888(or hexadecimal $378). Press the button, and if you have some tester connected to LPT, you will see the results. The same way you can work with reading.

Source: www.pcports.ru

3 Comments:

  1. Thanks for this program too much
    and i have qusetion ?
    the qusetion is i want turn of the LED by LPT and and Turn on Again . ”Led is simple light”
    Please Help me.
    and thanks again for this program.
    Souhill

  2. Hello friend!

    well, it works but i have a question:
    i’m having problems with the interval between the bytes.
    I’m controlling a CNC driver and i’m trying to use my PC as a pulse generator.
    The minimum interval that i can get is 15.0 miliseconds. This have something to deal with the dll? (some delay)
    When i use MSDos (booted with command.com) i can reach to 0.2 miliseconds

  3. Richard!

    This not dll trouble,this is MSWin-sux!-(don’t let more than 10 millisec time(interrupt). U must use some external circuit with(PIC) or some other microcontroller.

    Sorry about my english

    Roland

Leave a Reply