Using the Parallel Port in LabVIEW Primary Software: LabVIEW Development Systems>>Full Development SystemPrimary Software Version: 7.0 Primary Software Fixed Version: N/A Secondary Software: N/A
Problem: I would like to use LabVIEW to access the parallel port on my computer for digital input and output tasks. How do I do this? Solution: Occasionally it is convenient to use the Parallel Port for simple digital I/O. This document addresses how this might be done using VISA and register level programming. This document also addresses common mistakes, error messages, and problems encountered. This document does not deal with handshaking or PC to PC transfers, and only addresses specifics of the IEEE-1284 specification and communication when necessary. Those who want to learn more about the IEEE-1284 specification should consult the Developer Zone Tutorial: IEEE 1284 - Updating the PC Parallel Port which addresses this in much greater detail.
Introduction Normally the parallel port is used for output to a printer or other device. It sends data 8-bits or one byte at a time in parallel. The other lines available on the DB-25 connector are a combination of status lines, control lines, and ground lines. The status and control lines are used for handshaking, commands, and feedback when we are talking to a printer. We will have to concern ourselves with some of these lines in order to use the parallel port for our own purposes. In a Microsoft Windows environment, it is possible to get limited functionality from the parallel port using the same API that is used for serial communication. This means that in a Windows environment we can output data to the parallel port using the same VISA VIs that we would use for normal serial communication. However, the Windows API does not have built in support for input operations. Even though in some cases the hardware supports it, the software does not. This does not mean it is impossible to use the parallel port for input in LabVIEW, however, it does mean that it is not possible to use VISA VIs for input.
Note: The pin numbering is from 1-13 on the top row and 14 - 25 on the bottom going from right to left.
Table 1. Pin Functionality Diagram. Method 1
Using the parallel port for digital output is really rather trivial if working with the Windows API. The main trick is to tie pins 11(Busy) and 12 (Paper Error) to ground. Otherwise, the hardware driver will think the printer it is talking to is busy or experiencing an error and will not output any data. The port will maintain the last value written to it until another value is written or until the computer is powered down. Remember that in LabVIEW all serial communication needs to be sent as a string. Generally we will want to send 8-bit numbers to the port. This will require flattening the data to string so that the binary representation of the data does not change. We can use the Type Cast VI for the purpose.
![]() Figure 2. Using the Type Cast function in LabVIEW. If you output more than one byte at a time the driver will send them to the port in sequence and toggle the Strobe line (line 1) off and on for each byte. The timing involved varies from one computer to the next, but there are some standards in place. For more information about the timing characteristics, refer to the Developer Zone Tutorial: IEEE 1284 - Updating the PC Parallel Port
Refer to this Developer Zone Example: Using VISA to Access the Parallel Port in LabVIEW for a LabVIEW program illustrating how to write to a Parallel Port.
Common Errors
Method 2 An alternative to the VISA VIs is to write data directly to the parallel port's hardware registers. In LabVIEW we can access the hardware registers using the In Port.vi and Out Port.vi located in the Advanced - Port I/O palette. Because we are not using the higher level drivers, we do not have to concern ourselves with grounding any of the status lines.
Note: Accessing hardware registers on Windows NT or 2000 requires kernel level drivers. Refer to KnowledgeBase 2Q1FC3K8: Accessing Hardware Registers and Physical Memory in Windows NT/2000/XP with LabVIEW for more information.
For Windows NT/2000:
![]() Figure 3. Parallel Port (LPT1) Properties in the Windows Device Manager The base address is generally hex address 278, 378, or 3BC. There are several registers associated with the parallel port but for purposes of simple output we only need to concern ourselves with the Data register. This is the first register in the I/O range, and so is located at the base address. The 8 bits in this first register map directly to the data lines (2-9). All we need to do is use the Out Port.vi to write the desired values to the port as shown in Figure 4.
![]() Figure 4. Using Out Port.vi. Common Errors
Method 3 As mentioned earlier, it is impractical to use VISA VIs to do input through the parallel port. But there are other issues which arise for input. For one thing it cannot generally be guaranteed that the 8 data lines can be made bidirectional. This issue is covered in more detail in the next section. For now lets assume we cannot use the 8 data lines for input. Fortunately, there are other lines available to us. The next register after the data register, base + 1, is the status register. Five of the bits in the status register map to lines on the 25-pin connector (Busy, nAck, PaperEnd, Select, nError). These map to bits 7,6,5,4,3 of the status register respectively as shown in Figure 5. The status lines are already configured for input. If we were communicating with a printer, these are the lines the printer would use for handshaking and feedback. The Busy line must be inverted because it uses inverse logic. If five or fewer lines are all that is necessary, we can simply use the In Port.vi to read in the status register. If we need more, however, we will have to start using control lines.
The control register is at address base+2. Much like the status register, several bits in this register map to lines on our connector (nStrobe, nAutoLF, nInit, nSelectIn). These map to lines 0,1,2,3 of the control register. These lines are normally output, but they can be configured for bidirectional signals. To turn them into inputs we simply have to set them all to logic high. If a line is set high and is externally grounded by a signal, the ground wins out and the bit reads low. We also have to be aware that the Strobe, nAutoLF, and nSelectIn lines are inverse logic. If we set the inverse lines low, we actually are setting them high at the connector which is exactly what we want. If we want to read one full byte, we can read the upper nibble of the status register and the lower nibble from the control register. This requires some binary manipulation in LabVIEW as shown in Figure 6, and we still have to be careful of the bits that are inverse logic.
![]() Figure 5. Register Map. Note: Number constants for digital logic are represented in binary. ![]() Figure 6. Using the control and status lines for input. Common Errors
Some parallel ports can be configured to use the data lines as inputs. It depends a great deal on the way the manufacturer designed the parallel port. With some models the data lines can be read the same way we read the control lines, by driving them to high logic so they will take on the value of an external signal. However, most parallel ports require that you set the direction bit for input. This is bit 5 in the Control register (base+2). If the port is capable of it, setting the direction bit high has the effect of making the lines tri-state so it can be driven externally. Sometimes it is also necessary to toggle bit 6 high or low. However, it should be noted that some manufacturers actually lock these bits so that software cannot change them. An example is shown below in Figure 7.
![]() Figure 7. Using data lines for input. To test whether your data lines can be used for input, try the following:
If the reads DON'T match the writes, your port is probably bidirectional. Setting C5 disabled the data outputs and you're reading the open inputs of the data-port buffer. If the reads DO match the writes, your port isn't bidirectional. The data outputs are still enabled, you're reading back what you wrote, and you won't be able to read external signals. If it is possible to use your data lines for input, then you just need to set control register bit 5 high and read from the value of the data lines at the base address. Common Errors
Related Links: Developer Zone Tutorial: IEEE 1284 - Updating the PC Parallel Port Attachments:
Report Date: 10/12/2006 Last Updated: 05/16/2008 Document ID: 42BIT2C5 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||







