在VB 6.0、VB.NET和C#下开发基于DAQmx Base驱动的程序



主要软件:
主要软件版本: 1.5
主要软件修正版本: N/A
次要软件: Driver Software>>NI-DAQmx Base

问题: 我可以在VB 6.0、VB.NET和C#下开发基于DAQmx Base驱动的程序吗?

解答: 首先,值得注意的是,NI很多原来只由NI-DAQmx Base支持的基于USB总线产品现在已经可以由NI-DAQmx 7.5支持。您可以通过本文末尾的注解获得NI-DAQmx 7.5所支持的设备的列表。

虽然目前NI官方对DAQmx Base的支持只限于LabVIEW和C,但是如果您需要在VB 6.0、VB.NET或者C#开发的话,仍然可以通过在这些集成开发环境(IDE)下调用NI-DAQmx Base的动态链接库(DLL)的方式来实现。有一点必须留意:所有DAQmx Base C DLL都是使用cdecl调用模式的,但是在这些IDE下缺省的调用模式是stdcall,因此您必须做一些额外的设置。(您可以通过本文末尾的注解来获取更详细的关于这两种调用模式的信息)

Visual Basic 6.0:
因为在VB 6.0只能通过stdcall调用模式调用C DLL中的函数,所以不能直接调用DAQmx Base的DLL。您必须创建一个使用stdcall调用模式的C DLL包容器(wrapper),并通过这个包容器来调用这些函数。在这个包容器中,您只需要包含您的应用中所用到的函数。附件中有一个关于DAQmx Base 1.5的C DLL包容器的简单的例子,在使用这个例子之前,请阅读DAQmxBaseInVB6.zip文件中的README.txt。


C# / Visual Basic.NET:
在缺省方式下,C#和Visual Basic.NET使用stdcall模式调用C DLL函数的,但同时它们也支持cdecl调用模式;因此,您只需要对DllImport属性作相应的配置就可以通过cdecl模式调用DAQmx Base C DLL。请参考以下的例子。注意:这个例子并不是一个完整的应用程序,它只是演示了如何调用DAQmx Bass C DLL。

[C#]
using System;
using System.Runtime.InteropServices;
using TaskHandle = System.UInt32;

public class LibWrap
{
// C# doesn't support varargs so all arguments must be explicitly defined.
// CallingConvention.Cdecl must be used since the stack is
// cleaned up by the caller.
[DllImport("nidaqmxbase.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern int DAQmxBaseCreateTask (string taskName, out TaskHandle taskHandle);

[DllImport("nidaqmxbase.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern int DAQmxBaseCreateAIThrmcplChan (TaskHandle taskHandle, string
physicalChannel, string nameToAssignToChannel, double minVal, double maxVal, int units,
int thermocoupleType, int cjcSource, double cjcVal, string cjcChannel);
}

public class App
{
public static void Main()
{
TaskHandle taskHandle;
LibWrap.DAQmxBaseCreateTask("", out taskHandle) );
LibWrap.DAQmxBaseCreateAIThrmcplChan(taskHandle, "/Dev1/ai0", "", 0, 10, DAQmx_Val_DegC,
DAQmx_Val_K_Type_TC, DAQmx_Val_BuiltIn, 0, "") );
}
}

[Visual Basic]
Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports TaskHandle = System.UInt32

Public Class LibWrap
' Visual Basic does not support varargs, so all arguments must be
' explicitly defined. CallingConvention.Cdecl must be used since the stack
' is cleaned up by the caller.
<DllImport("nidaqmxbase.dll", CallingConvention := CallingConvention.Cdecl)> _
Public Shared Function DAQmxBaseCreateTask(ByVal taskName As String, ByRef taskHandle _
As TaskHandle) As Integer
End Function

<DllImport("nidaqmxbase.dll", CallingConvention := CallingConvention.Cdecl)> _
Public Shared Function DAQmxBaseCreateAIVoltageChan(ByVal taskHandle As TaskHandle, _
ByVal physicalChannel As String, ByVal nameToAssignToChannel As String, _
ByVal terminalConfig As Integer, ByVal minVal As Double, ByVal maxVal As Double, _
ByVal units As Integer, ByVal customScaleName As String) As Integer
End Function
End Class 'LibWrap

Public Class App
Public Shared Sub Main()
Dim taskHandle As TaskHandle = Convert.ToUInt32(0)
LibWrap.DAQmxBaseCreateTask("", taskHandle)
LibWrap.DAQmxBaseCreateAIVoltageChan(taskHandle, "/Dev1/ai0", "", _
DAQmx_Val_Cfg_Default, 0, 10, DAQmx_Val_Volts, Nothing)
End Sub 'Main
End Class 'App


注解:

1.NI很多原来只由NI-DAQmx Base支持的基于USB总线产品现在已经可以由NI-DAQmx 7.5支持。
您可以在ni.com/downloads下载从NI-DAQmx Base到NI-DAQ的转换工具。

· NI USB-6008(不支持虚拟设备功能)
· NI USB-6009(不支持虚拟设备功能)
· NI USB-6501(不支持虚拟设备功能)
· NI USB-9201
· NI USB-9211A
· NI USB-9215A
· NI USB-9221
· NI USB-9233
· NI USB-9421
· NI USB-9472
· NI USB-9481

NI-DAQmx本身支持SCXI-1600、DAQPad-6015和6016。USB-9211和USB-9215只由NI-DAQmx Base支持。

2.在cdecl调用模式下,调用函数会负责清空堆栈,并且函数包含多个参数。

在stdcall调用模式下,被调用函数负责清空堆栈,并且函数不能包含多个函数。如果您使用stdcall调用模式来调用一个用cdecl调用模式输出的函数,调用函数和被调用函数都不会清空堆栈,因此当程序运行的时候,会不断消耗堆栈控件。

美国国家仪器建议您使用stdcall调用模式来调用DLL中的函数,除非这些函数包含多个参数。Visual Basic和其他非C语言的Windows程序只能以stdcall方式调用DLL函数。

相关链接: KnowledgeBase 317742FQ: Programming NI-DAQ in Text-Based Languages

附件:


DAQmxBaseVB6AnalogDigitalExample.zipDAQmxBaseInVB6.zip


报告日期: 05/16/2006
最近更新: 05/19/2006
文档编号: 3NDDQLDD