cpubbs论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

LabVIEW+单片机学习套件全套教程资料下载[免费]LabVIEW论坛精华列表贴USB0816数据采集卡《LabVIEW宝典》
LabWindows/CVI论坛精华贴NET0816以太网数据采集卡RC0210远程设备授权系统 关闭关停锁定打开设备 户外分布式数据采集
NET1624低速高精度以太网数据采集卡WIFI0824SD无线WIFI网络数据采集卡脱机运行 SD存储 小尺寸微型 串口采集远程采集 安卓 手持移动采集 纪录仪
查看: 13672|回复: 15

LabVIEW操作并口的方法(NI)

[复制链接]
发表于 2008-10-1 13:43:48 | 显示全部楼层 |阅读模式
12 ratings:
3.5 out of 5    Rate this Document
Using the Parallel Port in LabVIEW
Primary Software: LabVIEW Development Systems>>Full Development System
Primary 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.

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.




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
  • -1073807330 (BFFF003E) - VISA "Could not perform operation because of I/O error"
  • Error 37 using Serial Compatibility VIs

    Search the KnowledgeBase for these error numbers to review troubleshooting ideas.
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 your Desktop, right-click on My Computer and select Properties.
  • Click on the Device Manager tab and find LPT1 under Plug and Play BIOS.
  • After selecting LPT1, click the Properties button.
  • Next select the Resources tab and the address should then appear next to Input/Output Range.

For Windows NT/2000:
  • On your desktop, right-click on My Computer and select Properties.
  • Now select the Hardware tab and click the Device Manager button.
  • LPT1 is found under Ports (COM & LPT).
  • Double click on LPT1.
  • Next, select the Resources tab and the address should appear next to Input/Output Range.




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
  • "Capability not supported" error form In Port.vi or Out Port.vi

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.



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
  • "Capability not supported" error form In Port.vi or Out Port.vi

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:
  • Set bit 5 of the control register high (at base address+2).
  • With nothing connected to the port, write a couple of values to the data port, and read each back after you write it.

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
  • "Capability not supported" error form In Port.vi or Out Port.vi
    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.
  • LabVIEW hangs with Serial Write compatibility VI

    Generally, these problems mean that lines 11 and 12 have not been properly grounded. You should be able to jumper these lines to any of the ground lines (18-25). Make sure you have not accidentally grounded lines 2 and 3 by mistake.

    Serial devices need to be properly listed in the LabVIEW.ini file to use the Serial Compatibility VIs. Because it is now recommended to use VISA, the necessary line is not present in the INI file by default. Generally, the line should appear as:

    serialDevices="COM1; COM2; COM3; COM4; COM5; COM6; COM7; COM8; COM9; COM10; LPT1; LPT2;LPT3; LPT4;"

    LabVIEW assigns port numbers in the order of the list beginning at 0, so this would make COM1 equal to port 0 and LPT1 equal to port 10. 1. DB-25 Connector - Facing Connector on Computer Back.


Related Links: [size=+0]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



Attachments:


- 0.B44.gif - 0.A1A.gif - 0.5484.gif
- 0.646E.gif
- 0.586.gif - 0.452.gif - 0.3760.gif
- 0.6BA.gif
http://sine.ni.com/devzone/cda/epd/p/id/4213
 楼主| 发表于 2008-10-1 13:44:24 | 显示全部楼层
http://digital.ni.com/public.nsf ... 37886257206000551CB
http://sine.ni.com/devzone/cda/epd/p/id/4213
文件名: 203540.llb
要求: View

This example uses VISA to write data to the parallel port. NI-VISA 1.1 was the first version that supported parallel communications. If you have an earlier version, this example will not work. When using the parallel port, pins 2-9 are data pins with pin 9 being the most significant bit. Also, pins 11 (printer busy) and 12 (printer out of paper) must be grounded to transmit data.
 楼主| 发表于 2008-10-1 13:46:00 | 显示全部楼层
53 ratings:      
  3.45 out of 5     Rate this Document

Accessing Hardware Registers and Physical Memory in Windows NT/2000/XP with LabVIEW

Primary Software: LabVIEW Development Systems>>Full Development System
Primary Software Version: N/A
Primary Software Fixed Version: N/A
Secondary Software: LabVIEW Development Systems>>Professional Development System, LabVIEW Development Systems>>Base Package



Problem:
I want to access my hardware directly in Windows NT/2000/XP. How can I communicate directly with hardware I/O registers and physical memory?

Solution:
Accessing hardware registers in Windows NT/2000/XP using LabVIEW

Hardware registers are accessed using the In Port and Out Port VIs in LabVIEW. See the heading for your version of LabVIEW below for more details:

LabVIEW 8

LabVIEW 8 maintains the functionality available in LabVIEW 7.x, but the In Port and Out Port VIs are now found on the Functions palette at Functions» Connectivity» Port I/O.

LabVIEW 7.x

LabVIEW 7.x ships with In Port and Out Port VIs which work in Windows NT/2000/XP. These are found on the Functions palette at All Functions» Advanced» Port I/O. It is not necessary to install the AccessHW libraries to access hardware registers. The new VIs are polymorphic, allowing you to choose between 8, 16, or 32 bit operations, and are not pin compatible with the VIs from previous versions of LabVIEW. Due to this, minor changes will be necessary when converting applications from LabVIEW 6.1 to LabVIEW 7.x which utilize the In Port and Out Port VIs.

LabVIEW 4.0 - 6.1

The In Port and Out Port VIs that ship with LabVIEW 4.0 through 6.1 do not work on Windows NT/2000/XP because they are not kernel mode routines, which are required for direct hardware access. To work around this, download and install the AccessHW library found at the Port and Memory Utilities for Windows link below. This will install replacement In Port and Out Port VIs which will work in Windows NT/2000/XP. The replacement VIs will be found in the installed directory and can then be used instead of normal In Port and Out Port VIs. If converting existing code, you will need to manually replace each existing In Port and Out Port VI with the newly installed replacements.



Accessing memory-mapped devices or other locations in physical memory

LabVIEW does not have native support for physical memory access. A library called AccessHW is available which provides this functionality by installing VIs which call LabWindows/CVI functions to allow direct memory access. The AccessHW library can be found at the Port and Memory Utilities for Windows link found below. The AccessHW installation will install the necessary LabWindows/CVI components, and does not require you to own LabWindows/CVI.
发表于 2008-10-1 21:50:59 | 显示全部楼层
好东西!我来顶一下!
发表于 2008-10-1 23:54:10 | 显示全部楼层
OMG
这下有的看了
发表于 2008-10-4 12:10:33 | 显示全部楼层
印象中应该有两个Port I/O Vi可以直接控制并口的`
 楼主| 发表于 2008-10-4 14:55:09 | 显示全部楼层
Using the Parallel Port as an Input/Output Channel

Primary Software: LabWindows/CVI Development Systems>>Full Development System
Primary Software Version: 5.0.1
Primary Software Fixed Version: N/A
Secondary Software: N/A



Problem:
How can I use a parallel printer port as an input or output channel?

Solution:
There are 3 locations in the I/O port address space at which printer cards can be configured: 278, 378, and 3BC. (The 3BC address was previously used with now obsolete combination monochrome display/printer cards.) Often the 278 and 378 ports are marked LPT1 and LPT2, which turns out not to be completely accurate in all cases. Typical BASE ADDRESSES are:
LPT1 0x378 (378h)
LPT2 0x278 (278h)

When configuring your system during a startup routine, DOS scans downward through the 3 locations, starting at 3BC, to determine if those addresses are occupied in your system. The first occupied port is then named LPT1, and so on. Each time it makes an assignment, it stores the I/O port address in a table which starts at memory address 408 (hex), occupying 2 bytes per LPT number and ordered by LPT number. When the operating system writes to a particular printer, the data is indexed into the table, the correct I/O port address is determined, and the data is passed to that port.

If you are developing an application for general use, then the best application architecture will include determining what is located at which of these addresses. (When developing a custom setup for your own purposes, it may be sufficient to hard code the port address.

To write to the printer port, simply output the appropriate data to the appropriate port address. If writing to an actual printer, it will generally be necessary to strobe that value. Also, your application may need some indication that the data on the pins is valid. This is done this by changing bit 0 of the control register to zero and then bringing it back to 1. (Bit 0 of the control register is located at Base Address + 2.). Don't tamper with the other bits in this register. Dropping this bit in the control register asserts the Strobe signal on pin 1 of the connector. Most printers require that the strobe last for at least 0.5 microseconds.

In LabWindows CVI 4.0.1 you can use a " data = inp(address); " function call to read one of the registers and you can use an " outp(address, bytevalue); " function to write to one of the registers. Where bytevalue is a value between 0 and 255 that sets the desired bit pattern.

The following information defines common base addresses for port registers.  Note that the register bits are listed as well as the corresponding DB25 connector pin that that bit is connected to. All the lines listed as Outputs are open collector type on most newer computers, which means that with care they can be used as inputs.


          BASE ADDRESS     (PDR) Data register (Read or Write)     Bit 0  Pin 2  Data Output     Bit 1  Pin 3  Data Output     Bit 2  Pin 4  Data Output     Bit 3  Pin 5  Data Output     Bit 4  Pin 6  Data Output     Bit 5  Pin 7  Data Output     Bit 6  Pin 8  Data Output     Bit 7  Pin 9  Data Output     BASE ADDRESS + 1     (PSR) Status register (Read Only)     Bit 0         Not used     Bit 1         Not used     Bit 2         Not used     Bit 3  Pin 15 ERROR Input     Bit 4  Pin 13 SELECT Input     Bit 5  Pin 12 PAPER END Input     Bit 6  Pin 10 ACKNOWLEDGE Input     Bit 7  Pin 11 BUSY Input     BASE ADDRESS + 2     (PCR) Control register (Read or Write)     Bit 0  Pin 1  DATA STROBE Output     Bit 1  Pin 14 AUTO LINE FEED Output     Bit 2  Pin 16 INITIALIZE PRINTER Output     Bit 3  Pin 17 SELECT INPUT Output     Bit 4         0 = disable interrupt, 1 = enable interrupt     Bit 5     Bit 6     Bit 7
http://digital.ni.com/public.nsf ... 4778c4?OpenDocument
 楼主| 发表于 2008-10-4 14:58:51 | 显示全部楼层
如果计算机无并口,则可以购买并口转换卡或者模块,市面上有提供的usb转并口的模块,可以虚拟出LP1---LPT3的端口号,映射出物理地址
LPT1 0x378 (378h)
LPT2 0x278 (278h)
然后像使用正常的并口一样轻松编程。
但一般的usb转打印机线这个不是真正的并口,而是usb printing support,无法映射物理地址,没有办法用output函数或者input函数底层操作编程,需要dll调用或者usb的编程,麻烦。对于高手来说,或者可以参考:usbprint.sys 。
 楼主| 发表于 2008-10-4 14:59:42 | 显示全部楼层

推荐一个真正的usb转并口模块!

USB转LPT(真并口,全功能专业版)模块

http://www.lzwest.com/list.asp?ProdId=0016
 楼主| 发表于 2008-10-4 15:13:49 | 显示全部楼层
市场上卖的通常是USB转打印口线,而非转并口,看CH341的说明就知道为什么区分打印口和并口了。
你要的是USB转并口,CH341可以转并口,不过,目前只能调用CH341的并口驱动DLL实现EPP输入输出,
而不是通过读写并口地址,因为USB转串口、转并口、转打印口都没有I/O地址,都是API调用,应用程序要改
 楼主| 发表于 2008-10-4 15:23:30 | 显示全部楼层
换个方法【才可能】成功的!

在电脑上插【USB接口】就会被操作系统识别成【USB设备】,然后操作系统会用【USB驱动】来支持这个【USB设备】;

问题是:要求【操作系统】使用【并口驱动】来支持【[USB接口转成并口]转接[试验箱]】,直接来是肯定不成的;
这个唯一的解决方法是:使用【自己编写】的【特殊USB驱动程序】来支持【[USB接口转成并口]转接[试验箱]】

自己是很难【自己写个】符合要求的驱动程序的!!
发表于 2008-10-6 09:23:56 | 显示全部楼层
原帖由 站立的鱼 于 2008-10-4 12:10 发表
印象中应该有两个Port I/O Vi可以直接控制并口的`

恩恩,赞成
发表于 2008-10-6 20:27:58 | 显示全部楼层
基本上看不懂,唉,的好好学习呀
发表于 2009-2-24 18:43:36 | 显示全部楼层
请问可以用并口11和21进行闭合和断开吗(我知通可以读出11和21的闭合或断开)
发表于 2009-3-2 09:36:18 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|cpubbs论坛. ( 粤ICP备09171248号 )

GMT+8, 2024-5-18 19:37 , Processed in 0.676510 second(s), 8 queries , Gzip On, File On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表