How Do I Use the Parallel Port in LabVIEW?Primary Software: LabVIEW Development Systems>>LabVIEW Full Development SystemPrimary Software Version: 8.6.1 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 can LabVIEW do this and what aspects should I consider? 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. The parallel port on most computers uses the DB-25 connector pictured below in Figure 1. Table 1 shows the pin functionality. Figure 1 Note: The pin numbering is from 1-13 on the top row and 14 - 25 on the bottom going from right to left.![]() 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; see Figure 2 below.
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. The first step is to identify the base address for the parallel port. The base address can generally be found in the Windows Device Manager under the Resources for the parallel port. For Windows 9x:
On Windows NT and 2000 machines, additional AccessHW drivers are required. Refer to the Developer Zone Example: Port and Memory Utilities for Windows. Also, make sure you use the In Port.vi and Out Port.vi that come with the AccessHW driver and not the ones from the vi.lib.Refer to KnowledgeBase 0TL70ATL: Using the Parallel Port as an Input/Output Channel for more information about regarding parallel ports and LabWindows/CVI. 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. ![]()
On Windows NT and 2000 machines, additional AccessHW drivers are required. Refer to the Developer Zone Example: Port and Memory Utilities for Windows. Also, make sure you use the In Port.vi and Out Port.vi that come with the AccessHW driver and not the ones from the vi.lib.Data Lines to be used as inputs 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. ![]()
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:
On Windows NT and 2000 machines, additional AccessHW drivers are required. Refer to the Developer Zone Example: Port and Memory Utilities for Windows. Also, make sure you use the In Port.vi and Out Port.vi that come with the AccessHW driver and not the ones from the vi.lib.
serialDevices="COM1; COM2; COM3; COM4; COM5; COM6; COM7; COM8; COM9; COM10; LPT1; LPT2;LPT3; LPT4;"Windows Vista Considerations: For information concerning what behavior can be expected when using the LabVIEW In Port and Out Port VIs with Windows Vista 32-bit and 64-bit please refer to KnowledgeBase 416C2PZH: Using the LabVIEW In Port and Out Port VIs with Windows Vista. Real-Time Considerations: For information concerning how to interface with the parallel port on a Real Time Operating System please refer to KnowledgeBase 261AI14U: Does LabVIEW Real-Time (RT) Support In Port.vi and Out Port.vi for Register-Level Programming? Note: The reason LV RT can't use VISA VIs is because the default serial driver only handles Serial ports, thus the Parallel port isn't seen by the RT OS driver, and hence the VISA layer cant access the port. Related Links: Developer Zone Tutorial: IEEE 1284 - Updating the PC Parallel Port NI LabVIEW: The Software that Powers Virtual Instrumentation Developer Zone Example: Using VISA to Access the Parallel Port in LabVIEW KnowledgeBase Support KnowledgeBase 2Q1FC3K8: Accessing Hardware Registers and Physical Memory in Windows NT/2000/XP with LabVIEW KnowledgeBase 0TL70ATL: Using the Parallel Port as an Input/Output Channel Developer Zone Example: Port and Memory Utilities for Windows KnowledgeBase 21O7DELI: Error -37 When Using the Serial Compatibility VIs KnowledgeBase 3O7F94LZ: Error BFFF003E Occurs after a VISA Read/Write KnowledgeBase 0PFEJ3A1: Serial Port VIs Do Not Work Properly and Hang the System KnowledgeBase 416C2PZH: Using the LabVIEW In Port and Out Port VIs with Windows Vista KnowledgeBase 261AI14U: Does LabVIEW Real-Time (RT) Support In Port.vi and Out Port.vi for Register-Level Programming? Attachments: Using the Parallel Port in LabVIEW pdf
Report Date: 10/12/2006 Last Updated: 04/06/2012 Document ID: 42BIT2C5 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||







