Sunday, December 20, 2009

How to program pararel port with delphi


I believe this topic has been write so many times in internet and you can find so many articles about it, but i want to share my experience programming pararel port with delphi. I started to do this project when i was doing some vision system project in printing company. When i got this project i started to learn about programming pararel port with delphi. One good source that i´ve found is http://et.nmsu.edu/~etti/fall96/computer/printer/printer.html This is very good source. ( but it use C language ).
The easiest port to control and access in the computer is pararel port ( LPT1, LPT2, LPT3 ). You can use this port to control your home appliance, electricity , robotic and also you can use it to make data acquisition. This port come in with female DB25 connector ( ussualy ) pic like image below :


Pin # Pin Name Pin Description and Function
1 /STROBE Strobe
2 D0 Data Bit 0
3 D1 Data Bit 1
4 D2 Data Bit 2
5 D3 Data Bit 3
6 D4 Data Bit 4
7 D5 Data Bit 5
8 D6 Data Bit 6
9 D7 Data Bit 7
10 /ACK Acknowledge
11 BUSY Busy
12 PE Paper End
13 SEL Select
14 /AUTOFD Autofeed
15 /ERROR Error
16 /INIT Initialize
17 /SELIN Select In
18 GND Strobe Ground
19 GND Data bit 1 and 2 Ground
20 GND Data bit 3 and 4 Ground
21 GND Data bit 5 and 6 Ground
22 GND Data bit 7 and 8 Ground
23 GND Busy and Fault Ground
24 GND Paper out, Select, and Acknowledge Ground
25 GND AutoFeed, Select input and Initialize Ground

Today computer ussualy come with 1 pararel port and 1 address, but last time it can came with 3 pararel port and 3 address.The address of pararel port listed like below :
       Printer         Data Port     Status          Control 
           LPT1                    0x03bc                0x03bd                0x03be        
               LPT2                   0x0378                0x0379                 0x037a        
               LPT3                   0x0278                0x0279                 0x027a
My experience pararel port address is using $378 for data port and $379 for status and $37A for control port.
in order to make my program easier i use io.dll. an io.dll is small dll program that you can download it from internet. it is free and easy to use ( even i use it on win-xp operating system ). This program is small procedure / function to connecting your delphi program and your pararel port. Please put this io.dll in one folder with your application program.

The electronic things
we can use simple electronic circuit like schematic below to test our program. if you dont have electronic experience you can ask your friend who know about electronic circuit to assembly this schematic.
Programming
Write simple program in delphi like code below :

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
input1:integer;

implementation

function PortIn(Port:Word):Byte;stdcall;external'io.dll';
procedure PortOut(Port:Word;Data:Byte); stdcall; external'io.dll';

{$R *.dfm}
// this procedure to light on LED on port 378
procedure TForm1.Button1Click(Sender: TObject);
begin
PortOut($378,$01); //this will light on 1 LED on pin number 2, you can change this value
end;
//this procedure is to read any value from port 379
procedure TForm1.Button2Click(Sender: TObject);
begin
input1:= PortIn($379) xor $80;
Edit1.Text:= IntToStr(input1);
end;

end.

you can make some experiment with this circuit and programming with any programming language, in this story i use delphi.

1 comment:

  1. pin 1 bukannya di posisi pin 13, dan seterusnya?

    ReplyDelete