LabVIEW-USB 通信简单教程
- 格式:pdf
- 大小:368.81 KB
- 文档页数:8
串口可以说是我们最容易见到,也最容易接触到的一种总线,台式机上一般都有二个,而现在很多下位机、仪器等很多都还是使用串口通信的。
论坛上很多朋友都经常会使用到串口,并遇到一些问题,这里有必要做一个详细的说明,以方便广大会员朋友方便使用.首先补充一个比较重要的问题,就是在LabVIEW中使用串口的话一定要先安装VISA这个驱动,然后生成的EXE运行时也需要在目标机上安装VISA Runtime Engine,可以在打包时一起打包。
1.串口扩展的问题:先说一下串口的扩展问题,一般的台式机或工控机上都至少有二个串口,一般都是够用的,但是现在市场上已经很难找到带串口的笔记本了,而有时候在外出调试时需要在笔记本中使用到串口的,这时一般是使用USB—RS232的转接线,价格从十几到一百多都有,很多朋友反应在使用价格低的转接线时会出现乱七八糟的问题,而贵一点的线就很少听说有其它问题的,所以大家在使用便宜的USB-RS232转接线时要特别注意线的质量,遇到一些奇怪的问题时先考虑换一根好一点的线。
PCI—RS232扩展卡也同理,便宜的卡也容易出问题,尽量买好一点的,以免因小失大.PCI—RS232一般至少能扩展2个串口,有些BT一点的可以扩展到8—16个,一堆线和接头。
转接线和扩展卡一般是要装驱动的。
2。
串口功能的确认:在使用串口之前,最好先确认一下串口是否正常,特别是使用转换接或扩展卡的.检查的方法很简单,就是将串口的2、3脚短接起来,3脚是发送数据,2脚接收数据,就是这个串口自发自收。
电脑上的串口软件一般是用串口调试助手,很出名的,也好用.如下图所示:图1串口调试助手打开软件,选择已经短接好的串口号,点击“手动发送”,如果串口是好的,2、3脚又短接起来了,马上就可以在上面的接收框里看到接收的数据就是发送的数据。
稍微要注意一下的是有些电脑的COM1和COM2的位置是反过来的,所以要确定好串口调试助手左上角的串口上择的是已经短接的那一个,如果COM1没接收到,可以再先COM2再发一次看一下。
LabVIEW⽤NI-488.2GPIB-USB-HS+建⽴通讯步骤LabVIEW⽤NI-488.2 GPIB-USB-HS+建⽴通讯步骤。
LabVIEW是⼀款⾮常⽅便的图形化上位机编程⼯具,在⾼校实验室和公司使⽤⽐较常见,NI-488.2 GPIB-USB-HS+是将GPIB接⼝转USB接⼝的控制器,本百度经验分享给⼤家如何使⽤LabVIEW通过NI-488.2 GPIB-USB-HS+控制器与仪器建⽴通讯连接。
⼯具/原料LabVIEWNI-VISA⼯具包NI-488.2 GPIB-USB-HS+控制器和驱动程序⽅法/步骤1.安装LabVIEW、NI-VISA和NI-488.2软件。
PC连接GPIB-USB接⼝⾄GPIB设备,安装新NI GPIB硬件设备前必须先安装NI-488.2驱动程序,否则Windows⽆法检测到硬件。
2.安装完成后桌⾯将出现NI MAX图标。
(NI-488.2驱动程序包含Measurement & Automation Explorer (MAX)⼯具,⽀持GPIB控制器。
MAX提供⼀系列⼯具对连接的仪器进⾏搜索、与设备进⾏通信,使得GPIB仪器检测和控制更为⽅便。
)3.单击桌⾯的MAX图标或通过开始>>所有程序>>National Instruments>>Measurement & Automation打开MAX浏览器。
4.确认GPIB设备是否正确连接,展开“我的系统(My System)”下的“设备和接⼝(Devices and Interfaces)”⼦⽬录,并选择⾃⼰的GPIB控制器,然后单击“扫描仪器(Scan for Instruments)”。
5.如GPIB设备可兼容SCPI,仪器扫描结束后名称和地址将出现在“属性”选项开的主窗⼝底部。
6.双击已识别的仪器(位于窗⼝底部)可访问仪器VISA属性(VISA Properties)。
万方数据i班岱里圣燕苤2;QQ!至苤垫翘基蓥!盟翘E堑型五置缍g于这些设各,只需以与GPlB仪器通讯同样的方孟,使用“VTSAOpen”,“VISAClose”,。
VISARend”和“VISAWrite”功能。
UsBTMc设备符台VISAUSBINSTR类函数能够理解的协议。
USBTMC设备相对来说控制较为复杂,因为每个设备可以使用各自的通信协议,而这些通信协议一般都是由设备的生产厂家自定的。
为了使用NI—VISA,岿须先让Windows将NI—VI-SA作为设备的缺省驱动程序使用。
在Windows环境中,可以通过INF文档做到这一点。
INF文件是系统硬件设备配置文件,USB驱动程序通过INF文件中的PID(产品识别号)和VID(厂商识别号)识别USB设备。
N1一VISA3.0中包古的VISADriverDevelopmentWizard(DDW)可以为USB设备创建一个INF文档。
下面简单介绍创建INF文档的过程;(1)在安装了NI—VISA后,启动VISADriverDevel—opmentWizard程序,出现了为PXI/PCI或USB设备创建一个INF文档的向导,选择USB设备,点NEXT,出现VI—SADDW基本设备信息窗口。
(2)进行这一步时.需要清楚USB的PID和VID。
这些数字可以在安装USB设备的时候对其进行确认,并在想要与设备通讯的时候,寻找他的地址。
依据USB的规格,两个数字都是16位16进制数字,并应该由设备制造商提供。
侧如在后面介绍基于USB的虚拟示渡器用到USB接口芯片PDlUSBDl2的PID和VID分别是0x0471和0x0666;这一步设置完成后,点击NEXT,进行最后一步的设置。
(3)USBInstrumentPrefix(USB仪器前缀)只是一个描述符,可以用他来识别本设备所用的相关文档。
在USBInstrumentPrefix中输入相应信息,并在“outputfiledirectory"中选择存放这些文档的目录,然后点击Finish。
串口可以说是我们最容易见到,也最容易接触到的一种总线,台式机上一般都有二个,而现在很多下位机、仪器等很多都还是使用串口通信的。
论坛上很多朋友都经常会使用到串口,并遇到一些问题,这里有必要做一个详细的说明,以方便广大会员朋友方便使用。
首先补充一个比较重要的问题,就是在LabVIEW中使用串口的话一定要先安装VISA这个驱动,然后生成的EXE运行时也需要在目标机上安装VISA Runtime Engine,可以在打包时一起打包。
1.串口扩展的问题:先说一下串口的扩展问题,一般的台式机或工控机上都至少有二个串口,一般都是够用的,但是现在市场上已经很难找到带串口的笔记本了,而有时候在外出调试时需要在笔记本中使用到串口的,这时一般是使用USB-RS232的转接线,价格从十几到一百多都有,很多朋友反应在使用价格低的转接线时会出现乱七八糟的问题,而贵一点的线就很少听说有其它问题的,所以大家在使用便宜的USB-RS232转接线时要特别注意线的质量,遇到一些奇怪的问题时先考虑换一根好一点的线。
PCI-RS232扩展卡也同理,便宜的卡也容易出问题,尽量买好一点的,以免因小失大。
PCI-RS232一般至少能扩展2个串口,有些BT一点的可以扩展到8-16个,一堆线和接头。
转接线和扩展卡一般是要装驱动的。
2.串口功能的确认:在使用串口之前,最好先确认一下串口是否正常,特别是使用转换接或扩展卡的。
检查的方法很简单,就是将串口的2、3脚短接起来,3脚是发送数据,2脚接收数据,就是这个串口自发自收。
电脑上的串口软件一般是用串口调试助手,很出名的,也好用。
如下图所示:图1串口调试助手打开软件,选择已经短接好的串口号,点击“手动发送”,如果串口是好的,2、3脚又短接起来了,马上就可以在上面的接收框里看到接收的数据就是发送的数据。
稍微要注意一下的是有些电脑的COM1和COM2的位置是反过来的,所以要确定好串口调试助手左上角的串口上择的是已经短接的那一个,如果COM1没接收到,可以再先COM2再发一次看一下。
:文件类型技术指南VISA是一个高 级API用来与仪器 控制总线进行通信。
它是平台独立、总线 独立、环境独立的。
也就是说,无论是使 用LabVIEW编 程在一台运行 Windows 2000的机器上与 USB设备进行通 信,还是使用C编程 在一台运行Mac OS X的机器上与 GPIB设备进行通 信,都可以使用同样 的API。
图1:VISA DDW硬件总线窗口您可以使用这个向导 创建供 PXI/PCI、 USB或IEEE 1394设备使用的 INF文件。
由于您 在为USB设备创建 驱动程序,选择 USB并点击下一 步。
VISA DDW基本设备信息 窗口打开,如图2所 示。
图2:VISA DDW基本设备信息 窗口2、在这个步骤 中,您必须知道 USB仪器所使用的 USB厂商ID和产 品ID。
这些数字在 您安装的时候能够识 别USB设备,在您 希望进行通信的时 候,可以对设备进行 寻址。
根据USB规范,所有数字必须是 16位的十六进制数 字,必须由设备制造 商提供。
如果您不知道USB 厂商ID和产品 ID,您可以将设备 插入计算机,让计算 机识别新设备得到这 些ID。
如果找到新 硬件向导打开,选择 取消。
打开控制面板 中的设备管理器,在 列表中找到您的设备,通常它在“其他 设备”中。
可能它会 带有黄色惊叹号标 记,表示这是一个未 知设备。
双击这个设 备打开属性。
选择详 细标签,确保“设备 实例ID”显示在属 性的下拉框中。
这样将会显示类似于图3 的字符串。
“VID_”和 “PID_”右边的 四个字符分别是您的 厂商ID和产品 ID。
写下设备的字 符串,关闭设备管理 器,从计算机上拔下 设备。
或者您还可以 联系您的设备厂商获得这些信息。
1.2. 3. 图3:从设备管理器 中找到厂商ID和产 品ID对于DAQPad- 6020E而言,厂 商ID和产品ID分 别是0x3923和 0x12C0。
如果 您的设备不是NI DAQPad- 6020E,厂商 ID和产品ID对于 您的设备而言是不同 的。
使用LabVIEW进行数据通信实现可靠的数据传输和网络通信LabVIEW是一款强大的工程软件,可以用于实现数据通信以及网络通信。
在大数据时代,准确、可靠的数据传输和网络通信对于各行各业都至关重要。
本文将介绍如何使用LabVIEW来实现可靠的数据传输和网络通信。
一、LabVIEW简介LabVIEW(Laboratory Virtual Instrument Engineering Workbench)是一种图形化的开发环境,由美国国家仪器公司(National Instruments)开发。
它允许用户通过将函数和连接它们的线连接在一起创建程序。
这种可视化编程的方法使得非专业编程背景的人员也可以轻松地进行软件开发。
LabVIEW广泛应用于控制系统、数据采集、实验室仪器控制和数据分析等领域。
二、数据传输1. 数据读取与写入LabVIEW提供了丰富的函数库和工具箱,可以轻松地进行数据的读取和写入操作。
通过使用合适的模块和函数,我们可以连接各种传感器、设备或者数据库来实现数据的采集和存储。
2. 数据处理与分析LabVIEW提供了强大的数据处理和分析功能。
用户可以使用图形化编程的方式,通过拖拽和连接不同的函数块来实现数据的处理和分析。
这使得数据的处理变得更加直观、高效和可靠。
3. 数据传输协议LabVIEW支持多种常用的数据传输协议,包括TCP/IP、UDP、HTTP等。
用户可以根据具体需求选择合适的协议来进行数据传输。
同时,LabVIEW还支持自定义的数据传输协议,可以根据实际情况进行定制。
三、网络通信1. 建立网络连接LabVIEW提供了丰富的网络通信工具,可以方便地建立网络连接。
用户可以使用TCP/IP或者UDP协议来建立客户端和服务器之间的通信。
LabVIEW还支持Web服务,可以通过Web浏览器与LabVIEW程序进行交互。
2. 实时监控与控制LabVIEW可以实时监控和控制网络上的各种设备。
用户可以通过连接传感器和执行器等设备,实时地获取数据并进行控制。
LabVIEW与PSoC3基于USB的通信方式
王程程;王安腾;王阡智;赵洪亮
【期刊名称】《电子技术应用》
【年(卷),期】2015(041)004
【摘要】针对LabVIEW无法直接支持非NI公司的USB接口数据采集卡的问题,介绍了上位机LabVIEW驱动USB设备的方法.同时使用PSoC3作为下位机数据采集仪器,研究了将PSoC3构建为USB设备的方式.系统地阐述了上位机LabVIEW与下位机PSoC3通过USB实现通信的各种方式,并提出了一种新方法:即将PSoC3构建为LabVIEW中已有的驱动类型的USB设备,例如声卡、串口类型等.
【总页数】4页(P81-83,90)
【作者】王程程;王安腾;王阡智;赵洪亮
【作者单位】山东科技大学电子通信与物理学院,山东青岛266590;山东科技大学电子通信与物理学院,山东青岛266590;山东科技大学电子通信与物理学院,山东青岛266590;山东科技大学电子通信与物理学院,山东青岛266590
【正文语种】中文
【中图分类】TP274.2
【相关文献】
1.基于LabVIEW NI-VISA实现PC与C8051F320的USB数据通信 [J], 段晋军;位恒曦;常晓明
2.基于USB串口的LabVIEW与MSP430通信实现 [J], 张杰;张轶;王晓刚;刘志辉
3.基于LabVIEW和C51的USB通信系统 [J], 陈龙燕
4.基于LabVIEW USB高速通信海量数据存储系统的设计 [J], 刘达
5.基于LabVIEW和USB伽玛能谱仪通信的实现 [J], 丁玉飞; 黄乡生; 管小明; 朱志甫
因版权原因,仅展示原文概要,查看原文内容请购买。
USB Instrument Control Tutorial OverviewThis tutorial is meant as a starting point for using NI-VISA to communicate with a USB device.It is not intended as a starting point for learning about USB architecture or the various protocols used in USB communication.After reading this tutorial,you should be able to install a USB device and use NI-VISA to communicate with that device,as long as you understand the device communication protocol.B and VISA BackgroundVISA is a high-level API used to communicate with instrumentation buses.It isplatform independent,bus independent,and environment independent.In otherwords,the same API is used regardless of whether a program is created tocommunicate with a USB device with LabVIEW on a machine running Windows2000or with a GPIB device with C on a machine running Mac OS X.USB is a message-based communication bus.This means a PC and a USB device communicate by sending commands and data over the bus as text or binary data.Each USB device has its own command set.You can use NI-VISA Read and Write functions to send these commands to an instrument and read the response from an instrument.Check with your instrument manufacturer for a list of valid commands for your instrument.Starting with version3.0,NI-VISA supports USB communication.Two classes of VISA resources are supported:USB INSTR and USB RAW.USB devices that conform to the USB Test and Measurement Class(USBTMC)protocol use the USB INSTR resource BTMC devices conform to a protocol that the VISA USB INSTR resource class can understand.No configuration is necessary to communicate with a USBTMC device.To communicate with a USBTMC instrument, refer to section3.For more information about the USBTMC specification,refer to the USB Implementers Forum Web page linked below.USB RAW instruments are any USB instrument other than those instruments that specifically conform to the USBTMC specification.If you are using a USB RAW device,follow the instructions in section2to configure NI-VISA to control your device.Contact your instrument manufacturer for details about the communication protocol and the command set your instrument uses.For specific information about the NI-VISA API,refer to the NI-VISA Help.This document is included with NI-VISA and is available through the link at the end of this tutorial.2.Configuring NI-VISA to Control Your USB DeviceThis section walks through the steps for configuring a USB RAW device to be controlled by NI-VISA3.0on a Windows-based computer.If you are using a USBTMC-compatible device,connect your device and skip to section2.3. At this point,NI-VISA already should be installed on your computer,and your USB device should not be connected.Furthermore,you should not have a driver for your USB device installed.There are three steps to configure your USB device to use NI-VISA:1.Create the INF file using the Driver Development Wizard.2.Install the INF file and the USB device using the INF file.3.Test the device with NI-VISA Interactive Control.For the purposes of this tutorial,an NI DAQPad-6020E is used as an example USB device and is installed on a Windows XP system.Because this tutorial is intended to explain the configuration of a generic USB device,details specific to the DAQPad-6020E are not discussed.Remember that NI-DAQ is the only supported driver for a DAQPad-6020E.2.1.Create the INF File Using the Driver Development WizardTo use NI-VISA,you must first tell Windows to use NI-VISA as default driver for the device.In the Windows environment,you can do this with an INF file.NI-VISA3.0and higher includes the VISA Driver Development Wizard (DDW)to create an INF file for your USB device.1.To open the DDW,select Start»Programs»National Instruments»VISA»VISA Driver Developer Wizard.Figure 1shows the open screen.Figure1.VISA DDW Hardware Bus WindowYou can use this wizard to create an INF file for a PXI/PCI,USB,or IEEE1394device.Because you are creating the driver for a USB device,click USB and Next.The VISA DDW Basic Device Information window opens as shown in Figure2.Figure2.VISA DDW Basic Device Information Window2.For this step,you must know the USB vendor ID and product ID for your USB instrument.These numbers identify your USB device when you install it and address your device when you want to communicate with it.According to the USB specification,both numbers are16-bit hexadecimal numbers and should be provided by the device manufacturer. If you do not know the USB vendor ID and product ID,you can get them by plugging the device into the computer and allowing the computer to recognize the new device.Cancel out of the Found New Hardware Wizard if it starts.Open the Device Manager from the Control Panel and find your device on the list,usually under"Other Devices."It may show a yellow exclamation mark indicating it is an unknown device.Double-click the device to open the properties. Select the Details tab and ensure that"Device Instance Id"shows in the attribute dropdown box.A string of characters will be displayed similar to Figure3.The four characters to the right of"VID_"and"PID_"are your vendor ID and product ID,respectively.Write down the characters for your device,close the Device Manager,and unplug the device from the computer.Alternatively,you can contact your device vendor to obtain this information.Figure3.Finding the Vendor ID and Product ID from the Device ManagerFor the DAQPad-6020E,the vendor ID and product ID are0x3923and0x12C0,respectively.If your device is not a NI DAQPad-6020E,the vendor ID and product ID are different for your device.Note:Before proceeding with the Driver Development Wizard,make sure the device has been removed from the computer.Enter the vendor ID,product ID,manufacturer name,and model name for your device in their respective fields.Click Next.The Output Files Properties window is displayed as shown in Figure4.Figure4.VISA DDW Output Files Properties Window3.The USB Instrument Prefix is simply a descriptor you use to identify the files used for this device.Enter a USB instrument prefix,select the desired directory in which to place these files,and click Next.The next window will provide you installation options.The default selection is to install the setup information for the operating system and is usually the best option.Once you select an option,click Finish to exit the wizard.The INF file is created in the directory you specified in the output file directory field in the previous window.2.2.Install the INF files and the USB device.The installation of the INF files is different for each version of Windows.When the DDW creates an INF file, installation instructions are included in a header at the top of the INF file.Because INF files are ASCII text files,they can be read in any text editor such as Notepad.For detailed information about installing your INF file,open your INF file in a text editor and follow the instructions at the top of the file.This tutorial assumes you are using Windows XP. 1.Copy the INF file to the INF folder.On Windows XP,this folder is usually at C:\WINDOWS\INF.This foldermay be hidden,so you may need to change your folder options to view hidden files.2.Right-click on the INF file in C:\WINDOWS\INF and click Install.This process creates a PNF file for yourdevice.You are now ready to install your USB device.3.Connect your USB device.Because USB is hot pluggable,Windows should be able to detect your USB device,andthe Add New Hardware Wizard should open automatically as soon as you connect it to the USB port.Follow the onscreen instructions for the wizard.When you are prompted to select a driver for this device,browse to the INF folder and select the INF file you created using the DDW.Note:In some cases,Windows may already have a default driver associated with your USB device.If this is the case, Windows will look to install that driver first.Once you've plugged in your USB device and Windows has installed the default driver,right-click on My Computer and select Properties.In the Properties window go to the Hardware tab and click on Device Manager.Once Device Manager is open,expand the"Human Interface Devices"tree.Then find out which instance of"USB Human Interface Device"(shown in Figure5)corresponds to your USB device byright-clicking and selecting Properties and checking the Details tab for the matching VID and PID.Figure5.Finding the Correct Instance of USB Human Interface Device for Your USB DeviceOnce you've found the"USB Human Interface Device"that has the matching VID and PID of your USB Device, right-click on it and go to Update Driver as shown in Figure6.Figure6.Update the Driver for Your USB DeviceOn the first screen,select"No,not at this time"then click Next.On the second screen,select"Install from a list or specific location(Advanced)"then click Next.On the third screen,select"Don't search.I will choose the driver to install."The fourth screen will look similar to Figure7where the highlighted driver is the driver you've created.Once you've selected your driver click next.When the driver has finished installing click Finished.Figure7.Select the VISA Driver for Your USB Device2.3.Test Communication with VISA Interactive Control.1.Open Measurement&Automation Explorer(MAX).Select Tools»Refresh to refresh the view.Your USB device should be listed as a USB Device under Devices and Interfaces as shown in Figure8.Your USB device is now installed and configured to use NI-VISA.If you select your USB device,the device information is displayed in the USB Settings ing this window, you can access information such as the manufacturer ID,model code,and serial number for your device.B Device Shown in MAX2.To communicate with your device using VISA,use the VISA instrument descriptor for your device.The instrument descriptor format for a USB INSTR device is USB[board]::manufacturer ID::model code::serial number[::USB interface number]::INSTR.The instrument descriptor format for a USB RAW device is USB[board]::manufacturer ID:: model code::serial number[::USB interface number]::RAW.According to the USBTMC specification,all USBTMC devices must have a serial number.Some USB RAW devices may not have serial numbers.If your device does not have a serial number,NI-VISA automatically assigns a VISA specific serial number for that device.The format for the serial number is NI-VISA-#,where#is an automatically generated number.Some USB devices have multiple interfaces.This is similar to the way a PCI device can have multiple functions.If your device only supports one interface,you do not need to include the USB interface number.The DAQPad-6020E uses the RAW class,and the manufacturer code and model code are0x3923and0x12C0, respectively.For the DAQPad-6020E,the instrument descriptor is USB0::0x3923::0x12C0::00B50DAE::RAW.To test communication with this device,open MAX.Select Tools»NI-VISA»VISA Interactive Control.A window similar to that shown in Figure9should open.Figure9.VISA Interactive Control3.The VISA Interactive Control(VISAIC)is a utility program used to communicate easily with any VISA resource. After your USB device is configured to use VISA,it should be listed in the USB branch.Double-click on your device to open a VISA session to the device.The window shown in Figure10should open.Figure10.VISA Interactive Control Open VISA SessionWhen you open a VISA Session with VISAIC,the Template tab and the Property Node(Set)tab are automatically selected.To read a property,select the Property Node(Get)tab,and choose the desired property and click Execute.The current value of the property specified in Attribute Name is displayed in the Current Value indicator.In Figure10, the Resource Name property was read.For more information about using VISAIC,refer to Developer Zone:VISA Interactive Control(VISAIC).For information about the NI-VISA API,review the NI-VISA Help.Both are available through the links at the end of this tutorial.For a list of valid commands for your USB instrument,contact your instrument’s manufacturer.ing NI-VISA to Communicate with Your USB DeviceThis section explains how to communicate with your USB device using NI-VISA3.0and above.Recall that there are two classes of USB devices.The method of communication depends on the class of your device.3.1USB INSTR Class(USBTMC)Devices that conform to the USB Test and Measurement Class(USBTMC)use the NI-VISA USB INSTR class.These devices use488.2style communication.For these devices,you can simply use the VISA Open,VISA Close,VISARead,and VISA Write functions in the same way you would if you were communicating with GPIB instruments.Figure11illustrates a LabVIEW VI that communicates with a USBTMC device.In this example,a VISA session is opened to a USB device.A command is written to the device,and the response is read back.In this example,the specific command being sent is the ID query for the device.Check with your device manufacturer for your device command set.After all communication is complete,the VISA session is closed.BTMC LabVIEW Example Block Diagram3.2USB RAW ClassCommunicating with the USB RAW class is more complicated because each device may use its own communication protocol.Contact your device vendor for details about the communication protocol for your device.USB communicates using four types of pipes or endpoints:control,bulk,interrupt,and isochronous.Each type of pipe transfers a different type of information.Furthermore,any number of endpoints can be of any endpoint type.Think of an endpoint as a communication socket.For specific details about USB architecture,review the USB specification linked below.NI-VISA supports three types of USB pipes:control,bulk,and interrupt.When NI-VISA detects your USB instrument, it automatically scans your instrument for the lowest available endpoint for each type.When VISA detects the lowest available endpoint,it assigns that value to the appropriate VISA attribute.The bulk in endpoint and bulk out endpoint are stored in the VI_ATTR_USB_BULK_IN_PIPE attribute and theVI_ATTR_USB_BULK_OUT_PIPE attribute,respectively.The interrupt in endpoint is stored in theVI_ATTR_USB_INTR_IN_PIPE attribute.A value of-1indicates that a USB device does not support that type of pipe. For the control pipe,only endpoint zero is supported.If you are using the C API,use the viSetAttribute function to change endpoints.In LabVIEW,use a Write VISA Property node.NI-VISA includes four functions to transfer data through USB pipes.Before you can communicate with your device using these functions,you need to set up the communication protocol using the VISA USB attributes.The following list describes the available functions.•Use VISA USB Control In and VISA USB Control Out to transfer data using the control pipe.•To transfer data using a bulk pipe,use VISA Read and VISA Write.If you are using LabVIEW,VISA includes an additional function to use the interrupt pipe:VISA Get USB Interrupt Data.In the C API,you can do this by accessing the VI_ATTR_USB_RECV_INTR_SIZE andVI_ATTR_USB_RECV_INTR_DATA attributes of the VI_EVENT_USB_INTR event object.See the NI-VISA Help for more information about VISA Events.B on Linux®and Mac4.1LinuxNI-VISA relies on a Linux kernel feature for its USB support.This feature is called usbfs,and on older Linux kernels was referred to as usbdevfs.For NI-VISA to support USB devices,this feature must be present and mounted(like a virtual file system).This is supported in most major Linux distributions such as Red Hat,SuSE,and Mandrake.You may use the mount command to display what file systems are currently mounted to see if your system currently supports this feature.Also,the VISA user must have write access to the file that represents the USB device,which is typically somewhere in a subdirectory within/proc/bus/usb.If this is not the case,the USB device is not accessible by VISA(it will not be found using viFindRsrc,and viOpen will fail).The default configuration on most systems is that the root user has write access;however,no other user has this access.There are several options for providing a nonroot user access to a USB device.•Use the hotplug package.This package is installed by default on most distributions including Red Hat,SuSE,and Mandrake.The hotplug package allows the user to run scripts for a specific USB device based on characteristics such as Vendor ID(VID)and Product ID(PID).If the hotplug package exists,the NI-VISA Installer by default will install scripts to give all users write access to all USB TMC devices and a framework for USB RAW devices.To add write permissions for a specific USB RAW device,run the included script:<VXIPNPPATH>/linux/NIvisa/USB/AddUsbRawPermissions.shFor more information about the hotplug package,refer to the following Web site:/.•usbfs(formerly known as usbdevfs)may be mounted with the option devmode=0666.This gives all users read and write access to all USB devices.•The root user may add write permissions to the file that represents the USB device,which is typically somewhere in a subdirectory within/proc/bus/usb.Unfortunately,these permissions are lost if the device is unplugged.Therefore,this approach is not recommended.4.2Mac OS XAs long as no other driver on the system claims the USB device,you can use NI-VISA to access it.No special setup is required.Related Links:Return to the Instrument Control Fundamentals HomepageProduct Reference:NI-VISA HelpDeveloper Zone:VISA Interactive Control(VISAIC)Developer Zone:Using the VISA Driver Development Wizard and NI-VISA to Register-Level Program a PXI/PCI Device under WindowsUSB Implementers Forum:USB2.0Specification:/developers/docsUSB Implementers Forum:Approved Class Specification Documents:/developers/devclass_docs Developer Zone:USB RAW Control Communication in LabVIEWDeveloper Zone:USB RAW Bulk Communication with LabVIEWDeveloper Zone:VISA USB RAW Mode Get Interrupt Data ExampleLinux®is the registered trademark of Linus Torvalds in the U.S.and other countries.。
0 引言虚拟仪器构建的系统具有性能高、扩展强、开发时间段和无缝集成等特点,被广泛应用于各个领域。
LabVIEW 是目前应用最广、功能最强的图形化虚拟仪器开发工具,它可将计算机、模块化的仪器等硬件与高效灵活的软件结合起来,提高系统的灵活性和工作效率,已经成为用于各个领域的标准化开发软件[1]。
任务方所提出研制的系统需将下位机FPGA 中大量数据通过USB 实时高速(12Mb/S)上传存储。
而如此大的数据量,需要将这些数据高效的存储,虽然LabVIEW 可以较好的完成这个问题,但如何在完成高速的数据通信的情况下,要保证快速、准确、可靠地存储这些海量数据,是当前研究的热点。
1 方案比较LabVIEW 中配有多种文件存储函数,如文献[1]选用数据记录文件函数,文献[2]选用写入电子表格函数,这些是目前常使用的文件存储软件设计,但此类函数存在读写速度较慢,文件取地址麻烦,且随着存储时间的增长,导致后台缓存紧张等缺点,因此不适于高速数据存储。
直接使用存储函数无法实现数据传输与存储的同时执行,不能满足任务方提出的要求。
所以在设计中引入生产者/消费者模式,提高数据处理存储与通信效率,提高了程序可读性、扩展性和复用性。
但该方案速度仍无法满足任务书要求。
所以针对以上问题,本文采用3组生产者/消费者分部执行,将数据通信、数据处理校验、数据存储分为三个执行循环结合“生产者/消费者”执行模式,同时在数据存储部分采用Datalog 文件进行存储能够进一步加快数据存储速度[2]。
2 系统软件构成系统软件框架由3组“生产者/消费者”执行组成,各执行循环内分别执行通信模块、数据存储模块、数据处理校验模块三个模块。
2.1 “生产者/消费者”框架结构设计与实现在传统的生产者/消费者模式基础上进一步提高运行communication with the FPGA, and requires data transmission rate, high accuracy and strong anti-interference ability. At present, the traditional file storage method is slow, and the real-time performance is poor. For these problems, a high-speed data mass storage method based on LabVIEW and FPGA USB communication is presented. The design method establishes a program framework by adopting three sets of “producer-consumer” modes, and synchronizes data transmission with data processing verification and data storage, that is, storage, data display, data verification, and background data determination in data processing. Cache cleanup and other synchronous execution, while using the Datalog file data storage format to further accelerate the continuous execution of data storage operations. The actual product results show that the high-speed data communication transmission storage at the same time, the program structure is less time-consuming than the conventional method, and as the communication time increases, the storage time is not increased.Keywords: high speed data storage;USB communication; LabVIEW; Data于USB 单次接收数据量为1024字节,而一帧数据有3680字节,所以将数据分为4页进行通信传输,单次通信取出1页数据放入数组后再进行一次写入与接收操作,直至所累计接收4页数据组为一帧(3680字节)并在帧末加上帧计数与校验字符,再将4帧数据(14736字节)组合完成一组数据作为生产者数据发送至其他消费者循环执行中。
串口可以说是我们最容易见到,也最容易接触到的一种总线,台式机上一般都有二个,而现在很多下位机、仪器等很多都还是使用串口通信的。
论坛上很多朋友都经常会使用到串口,并遇到一些问题,这里有必要做一个详细的说明,以方便广大会员朋友方便使用。
首先补充一个比较重要的问题,就是在LabVIEW中使用串口的话一定要先安装VISA这个驱动,然后生成的EXE运行时也需要在目标机上安装VISA Runtime Engine,可以在打包时一起打包。
1.串口扩展的问题:先说一下串口的扩展问题,一般的台式机或工控机上都至少有二个串口,一般都是够用的,但是现在市场上已经很难找到带串口的笔记本了,而有时候在外出调试时需要在笔记本中使用到串口的,这时一般是使用USB-RS232的转接线,价格从十几到一百多都有,很多朋友反应在使用价格低的转接线时会出现乱七八糟的问题,而贵一点的线就很少听说有其它问题的,所以大家在使用便宜的USB-RS232转接线时要特别注意线的质量,遇到一些奇怪的问题时先考虑换一根好一点的线。
PCI-RS232扩展卡也同理,便宜的卡也容易出问题,尽量买好一点的,以免因小失大。
PCI-RS232一般至少能扩展2个串口,有些BT一点的可以扩展到8-16个,一堆线和接头。
转接线和扩展卡一般是要装驱动的。
2.串口功能的确认:在使用串口之前,最好先确认一下串口是否正常,特别是使用转换接或扩展卡的。
检查的方法很简单,就是将串口的2、3脚短接起来,3脚是发送数据,2脚接收数据,就是这个串口自发自收。
电脑上的串口软件一般是用串口调试助手,很出名的,也好用。
如下图所示:图1串口调试助手打开软件,选择已经短接好的串口号,点击“手动发送”,如果串口是好的,2、3脚又短接起来了,马上就可以在上面的接收框里看到接收的数据就是发送的数据。
稍微要注意一下的是有些电脑的COM1和COM2的位置是反过来的,所以要确定好串口调试助手左上角的串口上择的是已经短接的那一个,如果COM1没接收到,可以再先COM2再发一次看一下。
:文件类型技术指南VISA是一个高 级API用来与仪器 控制总线进行通信。
它是平台独立、总线 独立、环境独立的。
也就是说,无论是使 用LabVIEW编 程在一台运行 Windows 2000的机器上与 USB设备进行通 信,还是使用C编程 在一台运行Mac OS X的机器上与 GPIB设备进行通 信,都可以使用同样 的API。
图1:VISA DDW硬件总线窗口您可以使用这个向导 创建供 PXI/PCI、 USB或IEEE 1394设备使用的 INF文件。
由于您 在为USB设备创建 驱动程序,选择 USB并点击下一 步。
VISA DDW基本设备信息 窗口打开,如图2所 示。
图2:VISA DDW基本设备信息 窗口2、在这个步骤 中,您必须知道 USB仪器所使用的 USB厂商ID和产 品ID。
这些数字在 您安装的时候能够识 别USB设备,在您 希望进行通信的时 候,可以对设备进行 寻址。
根据USB规范,所有数字必须是 16位的十六进制数 字,必须由设备制造 商提供。
如果您不知道USB 厂商ID和产品 ID,您可以将设备 插入计算机,让计算 机识别新设备得到这 些ID。
如果找到新 硬件向导打开,选择 取消。
打开控制面板 中的设备管理器,在 列表中找到您的设备,通常它在“其他 设备”中。
可能它会 带有黄色惊叹号标 记,表示这是一个未 知设备。
双击这个设 备打开属性。
选择详 细标签,确保“设备 实例ID”显示在属 性的下拉框中。
这样将会显示类似于图3 的字符串。
“VID_”和 “PID_”右边的 四个字符分别是您的 厂商ID和产品 ID。
写下设备的字 符串,关闭设备管理 器,从计算机上拔下 设备。
或者您还可以 联系您的设备厂商获得这些信息。
1.2. 3. 图3:从设备管理器 中找到厂商ID和产 品ID对于DAQPad- 6020E而言,厂 商ID和产品ID分 别是0x3923和 0x12C0。
如果 您的设备不是NI DAQPad- 6020E,厂商 ID和产品ID对于 您的设备而言是不同 的。
注意:在进入驱动程 序开发向导之前,请 确保设备已经从计算 机上拔下。
在对应的框中输入设 备的厂商ID、产品 ID、制造商名称和 型号名称。
点击下一步。
输出文 件属性窗口如图4所 示。
图4:VISA DDW输出文件属性 窗口3、USB仪器前缀 只是用来识别这个设 备所使用文件的描述 符。
输入USB仪器 前缀,选择需要存放 文件的目标文件夹, 点击下一步。
下一个 窗口将提供您安装选 项。
默认的选择是为操作系统安装设置信 息,也通常是最佳选 项。
在您选择了一个 选项之后,点击完成 退出向导。
INF文 件已经在您在前一个 窗口中输入的输出文 件位置中指定的文件 夹中生成了。
2.2. 安装 INF文件和USB 设备INF文件的安装对 于每个版本的 Windows而言 都是不同的。
在 DDW创建了INF 文件之后,安装指令 就已经包括在INF 文件的头部了。
由于 INF文件是 ASCII文本文 件,它们可以在记事本等任何文本编辑器 中查看。
要获得关于 安装INF文件的具 体信息,可以在文本 编辑器中打开INF 文件,按照文件头部 的指令完成。
本教程 假设您使用 Windows XP操作系统。
将INF文件复制到 INF文件夹中。
在 Windows XP中,这个文件夹 通常位于C: \WINDOWS\INF 中。
这个文件夹可能 是隐藏的,因此可能 需要修改文件夹选项 以便查看隐藏文件。
右键点击C: \WINDOWS\INF 中的INF文件,单 击安装。
这个过程将 为您的设备建立一个 PNF文件。
现在您 已经可以安装您的 USB设备了。
连接USB设备。
由 于USB是可以热插 拔的, Windows将能 够检测到您的USB 设备,在您连接到 USB端口之后,就 会自动打开添加新硬 件向导。
按照屏幕指 令完成向导。
在提示您为这个设备选择驱 动程序时,浏览 INF文件夹,选择 您使用DDW创建的 INF文件。
注意:在部分情况 下,Windows 可能已经为您的 USB设备设置了默 认的关联驱动程序。
在这个情况下, Windows将会 首先安装那个驱动程 序。
如果在您插入 USB设备, Windows安装了默认驱动程序,右 键点击我的电脑,选 择属性。
在属性窗口 中进入硬件标签,点 击设备管理器。
在设 备管理器打开之后, 展开“人体工程接口 设备”树。
找到 “USB人体工程接 口设备”(如图5所示)中对应USB设 备的实例,右键点击 并选择属性,检查对 应VID和PID的 细节标签。
图5:为您的USB 设备找到正确的 USB人体工程设备 实例在您找到了具有您 USB设备对应 VID和PID的 “USB人体工程接 口设备”之后,右键 点击这个设备,进入 如图6所示的更新驱 动程序窗口。
图6:更新USB设 备的驱动程序在第一个窗口中,选 择“不,不是这 次”,单击下一步。
在下一个窗口中,选 择“从列表或指定位 置安装(高级)”, 单击下一步。
在第三 个窗口中,选择“不 搜索。
我将选择要安 装的驱动程序。
”第四个屏幕将和图7相 似,在其中选择您已 经创建的驱动程序。
在选择了驱动程序之 后,单击下一步。
在 驱动程序完成安装之 后,点击完成。
图7:为您的USB 设备选择VISA驱 动程序2.3. 使用 VISA交互式控制 测试通信1、打开测量与自 动化浏览器 (MAX)。
选择工 具》刷新更新视图。
您的USB设备应该 已经作为USB设备 列在如图8所示的设 备与接口中。
您的 USB设备现在已经 完成安装,并配置为用于NI-VISA 使用。
如果您选择了USB 设备,设备信息将被 显示在USB设置窗 口中。
使用这个窗 口,您可以访问例如 制造商ID、型号代 码和设备序列号的信 息。
[+] 放大图片图8:在MAX中显 示的USB设备2、要使用VISA 与您的设备通信,使 用您设备的VISA 仪器描述符。
USB INSTR 设备的仪器描述符格 式是USB [board]:: 制造商ID::型号 代码::序列号 [::USB接口编 号]:: INSTR。
USB RAW设备的仪器描述符格式是 USB [board]:: 制造商ID::型号 代码::序列号 [::USB接口编 号]::RAW。
根据USBTMC规 范,所有 USBTMC设备必 须带有序列号。
部分 USB RAW设备 可能没有序列号。
如 果您的设备没有序列 号,NI-VISA 将会自动为该设备分 配一个VISA专用 的序列号。
序列号的格式是NI- VISA-#,其中 #是自动生成的数 字。
部分USB设备具有 多个接口。
这与一个 PCI设备具有多种 功能相似。
如果您的 设备仅仅支持一个接 口,您可能就不需要 包含USB的接口编 号。
DAQPad- 6020E使用 RAW类别,制造商 代码和型号代码分别 是0x3923和 0x12C0。
对于 DAQPad- 6020E而言,仪 器描述符是 USB0:: 0x3923:: 0x12C0:: 00B50DAE:: RAW。
要测试与这个设备的 通信,打开MAX。
选择工具》NI- VISA》VISA 交互式控制。
如图9 所示的窗口将被打 开。
图9:VISA交互 式控制3、VISA交互式 控制 (VISAIC)是 用来简单地与任何 VISA资源进行通 信的使用程序。
在您 的USB设备被配置 为使用VISA之 后,它就会被列出在 USB分支中。
双击 您的设备打开与设备通信的VISA会 话。
如图10所示的 窗口将被打开。
[+] 放大图片图10:VISA交 互式控制开发式 VISA会话在您使用 VISAIC打开 VISA会话之后, 就会自动选择模板标 签和属性节点(设 置)标签。
要读取一 个属性,选择属性节 点(获取)标签,选 择需要的属性并点击 执行。
属性目前在属性名中所指定的数值 会显示在当前值指示 器中。
在图10中, 资源名属性已经被读 取。
要获取更多关于 VISAIC的信 息,请参阅开发者园 地:VISA交互式 控制 (VISAIC)。
要获取更多关于 NI-VISA API的信息,请参 考NI-VISA帮 助。
两者都可以在本 教程最后的链接中获得。
要得到您USB 仪器中有效指令的列 表,请联系您的仪器 制造商。
本小节介绍了如何使 用NI-VISA 3.0以及更高版本 与USB设备进行通 信。
回忆一下共有两 种类别的USB设 备。
通信的方法依赖 于所使用设备的类 别。
3.1 USB INSTR类别 (USBTMC)符 合USB测试和测量 类别 (USBTMC)的 设备使用NI- VISA USB INSTR类别。
这 些设备使用 488.2格式通 信。
对于这些设备而 言,您简单地使用 VISA打开、VISA关闭、 VISA读取和 VISA写函数,与 使用GPIB仪器通 信相似的方式进行。
图11展示了能够与 USBTMC设备通 信的 LabVIEW VI。
在这个实例 中,打开了一个 VISA会话与 USB设备相连。
一 条指令被写入设备 中,并读回系统响 应。
在这个实例中, 发送到设备的特定指令是设备的ID查 询。
可以与设备制造 商联系,得到设备指 令集的列表。
在完成 所有通信之后, VISA会话被关 闭。
图11: USBTMC LabVEW实例程 序框图3.2 USB RAW类别与 USB RAW类别 通信更加复杂,因为 每个设备可能使用它 们自己的通信协议。
联系您的设备厂商获 取设备通信协议的详 细资料。
USB通信使用四 个类型的管道或中止 点:控制、集合、中 断和等时。
每个类别 的管道都会传送不同 类型的信息。
此外, 任何数量的中止点都 可以是任何中止点类 型。
中止点可以看作是通信套接字。
对于 关于USB体系结构 的具体细节,可以查 看下面链接中的 USB规范。
NI-VISA支 持三种类型的USB 管道:控制、集合和 中断。
在NI- VISA检测完您的 USB仪器之后,它 将自动扫描您的仪 器,以便得到每种类 型最低可能的中止 点。
在VISA探测完最 低可能的中止点之 后,它将该值分配到 对应的VISA属性 中。
中止点中的集合 和集合末中止点分别 被存储在 VI_ATTR_USB_BULK_IN_PIPE 属性和VI_ATTR_USB_BULK_OUT_PIPE 属性中。
中止点中的 终端被存储在 VI_ATTR_USB_INTR_IN_PIPE 属性中。
数值-1表 示USB设备不支持 该类型的通道。
对于 控制通道而言,只支持中止点零。
如果您 正在使用C API,可以使用 viSetAttribute 函数改变中止点。