Parallel Port Control With Delphi - Parallel Port, Delphi, LPTPort, Device Driver
Short Description
Download Parallel Port Control With Delphi - Parallel Port, Delphi, LPTPort, Device Driver...
Description
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
1 de 8
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
Start FREE Trial
Contact Us
Member Login
Enter Your Search Terms
Experts Exchange
Programming
Prog Languages
Pascal
Delphi
Parallel Port Control with Delphi
ID: 2161 Username or Email
Parallel Port Control with Delphi
Password Remember Me
thiagoblimeira Posted on 29/12/09 at 4:22 AM 8 of 8 members found this article helpful.
Sign Up
Forgot Password?
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small printer symbol. This port is known as LPT port or printer port. We can program this port for device control and/or data transfer, in projects of robotics, electronics and even LCD interfacing. In this article, I'll show the basics of parallel port and some programming principles using the LPTPort Component. Parallel Port Concepts The parallel port was initially designed to connect a printer to the computer, however we can program this port for many more applications beyond that. The parallel port can be used for two main reasons: device control and communication. We can program the parallel port for both functions. The parallel ports are easier to program and faster compared to serial ports. However the main disadvantage is that it needs more transmission lines (9 in comparison to the 3 lines used in serial port). Because of this, parallel ports are not used in long distance communications. So let's see the basic differences between the transmission of parallel port and serial port (Figure 1). In serial ports, we have two data pins: one for transmission and the other for reception. To send data in serial port, it has to be sent one bit after another with some extra bits like start bit, stop bit and parity bit to detect errors. But in parallel port, all the 8 bits of a byte will be sent to the port at the same time and an indication that the data is ready for transmission will be sent in another pin. There will be some data pins, some control and some handshaking pins in parallel port. Commonly at the computer's backside we have a DB25 female connector with 25 pins (see Figure 2), the Table 1 presents the respective DB25 pins and functions. ADVERTISEMENT
LPTPort1.jpg (19 KB) (File Type Details)
Figure 1
LPTPort2.jpg (18 KB) (File Type Details)
Table 1
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
2 de 8
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
LPTPort3.jpg (16 KB) (File Type Details)
Figure 2
Parallel's Port Organization Now we are going to see how the parallel port is organized, there are 3 registers associated with the LPT port: the data, status and control registers. The data register takes care of the port's data pins, this means that if want to send a byte to the data pins, we need to address this register, in order to send the byte to the data pins. Normally, the data, status and control registers will have the following addresses (see Table 2), we will need them during the programming.
LPTPort5.jpg (12 KB) (File Type Details)
1. ewangoya 2. jimyX 3. Thommy 4. Geert_Gr... 5. systan 6. developm... 7. epasquier 8. lomo74 9. Mahdi78 10. RezaSadigh 11. cyberkiwi 12. pasolo 13. aikimark 14. BdLm 15. sYk0 16. ebob42 17. cebasso 18. Hypo 19. TheRealL... 20. mdspencer 21. BigRat 22. ThievingSix 23. wd123 24. mikelittlew... 25. mnasman
-269,890 -237,064 -67,601 -62,280 -27,432 -16,523 -13,801 -11,150 -10,900 -10,432 -10,400 -8,000 -7,425 -6,799 -6,600 -6,060 -5,900 -5,882 -5,800 -5,000 -4,900 -4,732 -4,332 -4,000 -3,632
1. kretzsch... 2. rllibby 3. ciuly 4. Geert_Gr... 5. epasquier 6. EddieShip... 7. ziolko 8. Madshi 9. Slick812 10. geobul 11. Ferruccio68 12. mnasman 13. TheRealL... 14. Workshop... 15. MerijnB 16. inthe 17. ewangoya 18. mikelittlew... 19. mokule 20. DragonSl... 21. Johnjces 22. ThievingSix 23. developm... 24. jimyX 25. rfwoolf
1,419,314 1,371,255 1,301,064 1,125,067 819,199 779,204 777,079 662,000 649,163 628,081 627,652 612,687 567,438 558,515 546,427 514,714 442,896 438,196 431,295 404,151 366,172 365,510 363,339 353,380 325,170
Table 2
To know the details of parallel ports available in your computer, follow this procedure: 1. Right click on My Computer, go to "Properties". 2. Select the tab Hardware, click Device manager. 3. You will get a tree structure of devices; in that expand "Ports (COM & LPT)". 4. Double click on the ECP Printer Port (LPT1) or any other LPT port if available (see Figure 3).
LPTPort4.jpg (26 KB) (File Type Details)
Figure 3
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
3 de 8
5. 6.
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
You will get details of LPT port. Make sure that "Use this Port (enable)" is selected. Select tab Resources. In that you will get the address range of port (see Figure 4).
LPTPort6.jpg (27 KB) (File Type Details)
Figure 4
Programming the Parallel Port For security reasons NT/2000 and XP versions of Windows don't allow direct access to hardware, in other words, one cannot direct manipulate the hardware resources, like parallel ports, therefore a device driver is necessary to talk to the system's core (kernel) to get access to computer's physical ports. In the 95/Me and 98 Windows versions there is no restriction and the we have direct access, so there's no need for drivers. Among the difficulties in using dlls, is the fact that you must distribute them with your application, in order for it to work, but we will use a free superb component, that will do all the work, of loading its internal drivers to enable us with direct access to computer's parallel ports. We are going to make a simple application to demonstrate the many uses and advantages of the LPTPort component: LPT port detection, data pin manipulation, writing values to and reading values from the parallel port. You can download the LPTPort component at: www.delphifr.com. Initially open the BDS and create a new application, save it as ParallelControl.dpr, and the main form as uMain.pas. Put on the created form one XPManifest and a LPTPort component, now double click the form and on the event OnFormCreate, type the following code:
1: LPTPort1.SelectPort($378);
With this code we will be selecting the base parallel port address ($378) that is the standard in most computers (888 in decimal notation), we need to use the $ in order for the compiler to recognize that's a hexadecimal value. Pick a GroupBox, and alter its Name property to grbPins, add 5 labels onto the GroupBox and modify its properties like described in Table 3.
LPTPort7.jpg (13 KB) (File Type Details)
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
4 de 8
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
Table 3
These Labels will be used to show the decimal and binary values from the port's data register using the GetState method (see Listing 1). Listing 1: Code for presenting the decimal and binary values from data register 1: procedure TfrmMain.GetState; 2: begin 3: lblDec.Caption := IntToStr(LPTPort1.Inp); 4: lblBin.Caption := DecToBin(LPTPort1.Inp); 5: end;
This routine reads the data register using the Inp function and presents the value using the labels Caption property value. Observe that the LPTPort component has a method for converting decimal into binary values with DecToBin. Now insert a RadioGroup on the form with its Name property altered to rdgRegisters, and in its Items property type these three lines: Data Register, Status Register and Control Register. This way, we are inserting three RadioButtons corresponding to these three lines. Now double click the rdgRegisters and type Listing 2 code. Listing 2: Code for parallel port register selection (rdgRegistersClick) 1: procedure TfrmMain.rdgRegistersClick(Sender: TObject); 2: begin 3: case (rdgRegisters.ItemIndex) of 4: 0: 5: begin 6: SetPinOutCheckBoxState(True); 7: btnSend.Enabled := True; 8: btnRead.Enabled := True; 9: lblBinHeader.Enabled := True; 10: lblBin.Enabled := True; 11: lblDecHeader.Enabled := True; 12: lblDec.Enabled := True; 13: edtSend.Enabled := True; 14: btnSend.Enabled := True; 15: end; // Data Register 16: 1: 17: begin 18: SetPinOutCheckBoxState(False); 19: btnSend.Enabled := False; 20: btnRead.Enabled := True; 21: lblBinHeader.Enabled := False; 22: 23: lblBin.Enabled := False; 24: lblDecHeader.Enabled := False; 25: lblDec.Enabled := False; 26: edtSend.Enabled := False; 27: btnSend.Enabled := False; 28: end; // Status Register 29: (* the status register is read-only*) 30: 2: 31: begin 32: SetPinOutCheckBoxState(False); 33: btnSend.Enabled := True; 34: btnRead.Enabled := True; 35: lblBinHeader.Enabled := False; 36: lblBin.Enabled := False; 37: lblDecHeader.Enabled := False; 38:
After that, we are going to put 8 CheckBoxes onto the grbPins and alter its property accordingly to the Table 4.
LPTPort8.jpg (18 KB) (File Type Details)
Table 4
Theses CheckBoxes will simulate the port's data pins, because these pins are used the most in electronic projects, the
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
5 de 8
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
component can associate the pin state directly using the method below:
1: SetPin(String Name, Boolean State);
This method needs the pin's Name (the string para-meter) and the State Boolean parameter (that represents the pin's state, low or high) for setting the desired state of one of the data pins. Because we are going to shift between registers, we need a routine to disable the CheckBoxes when we are using the status and control registers, in order to do that type the Listing 3 code. Listing 3: Code for enabling or disabling the CheckBoxes representing the data pins 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
procedure TFrmMain.SetPinOutCheckBoxState(State: Boolean); var I : Integer; C : TCheckBox; begin for i := 0 to ComponentCount -1 do begin if Components[i] is TCheckBox then begin C := TCheckBox(Components[i]); if Copy(UpperCase(C.Name), 1, 4)= 'CHKD' then C.Enabled := State; end; end; end;
Do not forget to prototype this method in the uMain.pas! By using the typecasting technique this method will verify all the form's components, and if the component belongs to the TCheckBox class, then we will enable or not, the data pins CheckBoxes, depending on the State parameter's value. Now insert 3 buttons in the form that will be called btnOpen, btnSend and btnRead respectively, modify their Enable property to False; double click btnOpen and on its OnClick event type the code from Listing 4. Listing 4: Code from btnOpenClick event 1: procedure TfrmMain.btnOpenClick(Sender: TObject); 2: begin 3: case btnOpen.Tag of 4: 0 : 5: begin 6: btnOpen.Caption := 'Close'; 7: btnOpen.Tag := 1; 8: LPTPort1.LPTPort := $378; // LPT1 by default 9: if LPTPort1.Open then 10: begin 11: SetPinOutCheckBoxState(True); 12: cbLPTPort.Enabled := True; 13: btnSend.Enabled := True; 14: btnRead.Enabled := True; 15: end; 16: end; 17: 1 : 18: begin 19: btnOpen.Caption := 'Open'; 20: btnOpen.Tag := 0; 21: LPTPort1.Close; 22: 23: SetPinOutCheckBoxState(False); 24: cbLPTPort.Enabled := False; 25: btnSend.Enabled := False; 26: btnRead.Enabled := False; 27: end; 28: end; end;
By using this button's Tag property, which initially is 0, we will define the port's base address and load the dlls needed to access the port's hardware using the method Open from the LPTPort, this button's click will also enable the btnSend and btnRead button, making the CheckBoxes available. If you wish to unload the dlls and have no more hardware Access just click btnOpen again, this will call the Close method. Back to the CheckBoxes, we need a method for verifying the CheckBoxes Checked property state and apply this to the state of the data pins, therefore reflecting the port's state and value. For this prototype the function OnPinOutClick in the uMain.pas and type the code from Listing 5. Listing 5: Code for pin activation by using the CheckBoxes
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
6 de 8
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
procedure TfrmMain.OnPinOutClick(Sender: TObject); var C : TCheckBox; Pin : String; begin if Sender is TCheckBox then begin C := (Sender as TCheckBox); Pin := Format('D%u' , [C.Tag]); case C.State of : LPTPort1.SetPin(Pin, True); cbChecked cbUnChecked : LPTPort1.SetPin(Pin , False); end; GetState; end; end;
How to list the computer's LPT ports It's possible to list all available computers' LPT Ports using the LPTPort, to do that we are going to insert a ComboBox onto the grbPins and change its Name property to cbLPTPort, and on its DropDown event we will use the following code:
1: procedure TfrmMain.cbLPTPortDropDown(Sender: TObject); 2: begin 3: LPTPort1.EnumPorts(cbLPTPort.Items); 4: end;
The method EnumPorts receives a TStrings parameter, and lists all LPT ports available and stores them in the cbLPTPort.Items property. Listing the LPT Port's address It's also possible to determine the parallel's port address, by using the component's Ports property. This property is a list of all the computer's parallel ports, and with this list we can recover both port's Name and Address, using respectively the properties Name and Port from the Ports list, just by using the code below on the cbLPTPort's OnClick event:
1: 2: 3: 4: 5: 6: 7: 8:
procedure TfrmMain.cbLPTPortClick(Sender: TOb-ject); begin if cbLPTPort.ItemIndex = -1 then Exit; LPTPort1.SelectPortByIndex(cbLPTPort.ItemIndex); lblPort.Caption := Format('%s = 0x%.3xH' , [LPTPort1.Ports[cbLPTPort.ItemIndex].Name , LPTPort1.Ports[cbLPTPort.ItemIndex].Port]); end;
Writing values to the LPT port Now we are going to write values to the parallel port, the LPTPort facilitates writing values to the port, we need to use 2 Edits that Will be used respectively to send and store the parallel port's value, name this Edits as edtSend and edtRead, in Figure 5, we have a GUI suggestion for this application, but nothing's better than use your creativity in creating GUI's.
LPTPort9.jpg (20 KB) (File Type Details)
Figure 5
Double click the btnSend button and add the code from Listing 6 on its OnClick event, this code will send the numerical value typed in the edtSend to the component's unit, we verify if the value stored the in edtSend.Text is really a numerical value, otherwise, we show a message using the MessageDlg function. This way, according to the selected RadioButton, we selected register in the switch case code. First we recover the edtSend's Text property value using the Trim function, which removes empty characters, (e.g. Carrier return) from the string's beginning and end. And using the IsNumeric function declared in the component's unit, we verify if the value stored the in edtSend.Text is really a numerical value, otherwise, we show a message using the MessageDlg function. This way, according to the selected RadioButton, we choose the desired port's register (just remembering that the status register, $379 address, is read-only ), after this verification we need convert this string into a integer value and send it to the port using the LPTPort's Out function. In case the data register is the selected one, we update the labels lblDec and lblBin Caption property using the GetState. Note that we can send to the data register values up to 255, because each pin represent a bit, with a total of 8 bits (1 byte), so we have a range from 0 to 255.
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
7 de 8
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
Listing 6: Code for writing values to the LPT port (btnSendClick) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
procedure TfrmMain.btnSendClick(Sender: TObject); var S : String; begin S := Trim(edtSend.Text); if not(IsNumeric(S)) then begin MessageDlg(Format('Value %s incorrect!', [S]), mtWarning, [mbOk], 0); Exit; end; case rdgRegisters.ItemIndex of 0 : LPTPort1.SelectPort($378); //Data Register address 1 : LPTPort1.SelectPort($379); //Status Register address 2 : LPTPort1.SelectPort($37A); //Control Register address end; LPTPort1.out(StrToInt(S)); if rdgRegisters.ItemIndex = 0 then GetState; end;
Reading values from the LPT port Now that we already know how to send values to the parallel port, we are going to move on reading values from the port. In order to do this, first double click the btnRead and type the code listed at Listing 7 in the button's OnClick event. Note that we selected the base address (e.g $378) and depending on the selected register in the switch case command we are altering the LPTPort's InpOffset property value, this property holds an integer value, that selects what register is going to be read by the LPTPort's Inp function. So if we want to read, for example the control register, we have to select the base address ($378 in our case), and modify the InpOffset property to the value 2. In our example we are reading the selected register value and showing it in the edtRead's Text property. Listing 7: Code for reading values from the LPT port (btnReadClick) 1: procedure TfrmMain.btnReadClick(Sender: TObject); 2: begin 3: LPTPort1.SelectPort($378); 4: case rdgRegistros.ItemIndex of 5: 0 : LPTPort1.InpOffset := 0; //Data Register Offset 6: 1 : LPTPort1.InpOffset := 1; //Status Register Offset 7: 2 : LPTPort1.InpOffset := 2; //Control Register Offset 8: end; 9: 10: edtRead.Text := IntToStr(LPTPort1.Inp); 11: 12: end;
Some tips The development of programs that need to access the parallel port can be a hard time those who don't have basic electronics knowledge, because the measurement of the pin's voltage facilitates the visualization of what's going on with the port's pins. There is a parallel port monitor tool that enables through graphic visualization, the pin's state and one can read, write values to the port's registers, called Parallel Port Monitor (see Figure 6), I really recommend using this tool during the development of any programs that make use of the parallel port. This free tool can be downloaded at: http://www.geekhideout.com/download s/parmon.zip.
LPTPort10.jpg (23 KB) (File Type Details)
Figure 6
Conclusions As we can see in this article, it's pretty easy to control the parallel port's registers in Delphi using the LPTPort component, without the need of dlls. This way we can enjoy our good old LPT ports for developing device control, electronics projects, LCD interfacing, etc. That's all for now people, see you next time. NOTE: The I/O port level controlling details here has proven to work well with parallel ports on the PC motherboard and
01/04/2011 14:02
Parallel Port Control with Delphi - Parallel Port, Delphi, LPTPort, Dev...
8 de 8
http://www.experts-exchange.com/Programming/Languages/Pascal/Delp...
expansion cards connected to ISA bus. The programming examples might not work with PCI bus based I/O cards (they can use different hardware and/or I/O addresses, their drivers make they just look like parallel ports to "normal" applications). The programming examples do not work with USB to parallel port adapters (they use entirely different hardware, their drivers make them to look like normal parallel port to operating system "normal" applications).
Zone: Delphi Programming Tags: Parallel Port, Delphi, LPTPort, Device Driver Category: Tutorial Views: 3679
Dzone
Twitter
Facebook
Linked In
Most Helpful Articles: WARNING: 5 Reasons why you should NEVER fi... How Do I Know What to Charge as an IT Cons... Outlook continually prompting for username... Why you shouldn't use PST files Migrate Small Business Server 2003 to Exch...
Digg
Mixx
StumbleUpon
Related Articles: Sending E-mail from a PowerShell Script How to create a PlugIn bootstrap in PHP fr... Deploying Adobe Acrobat Reader X (Ver 10.0.1) Managing Trees and Hierachies in Access What's New With Access 2010 Macros
24/12/10 10:54 PM, ID: 22422
systan: hi can this be used to program a full scree video with lots of led light for displaying images or letters for street advertisement?
Please Login or Register to post a comment.
Shorten URL | Help | About Us | Contact Us | Terms of Use | EE Blog | Internet Rank | Privacy Policy | Site Map
01/04/2011 14:02
View more...
Comments