Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
14 rating:
 1.78 out of 5     Rate this Document

Programming with DAQmx Base in VB 6.0, VB.NET and C#

Primary Software: Driver Software>>NI-DAQmx Base
Primary Software Version: 1.5
Primary Software Fixed Version: N/A
Secondary Software: Driver Software>>NI-DAQmx Base

Problem:
Can I program a DAQmx Base device in VB 6.0, VB.NET, or C#?

Solution:
Please be aware that many USB products which were originally supported only in NI-DAQmx Base now are supported by NI-DAQmx 7.5. Please refer to the Notes: section below for the list of supported devices.

Currently, DAQmx Base is officially supported in only LabVIEW and C. If you do need to program with DAQmx Base in VB 6.0, VB.NET, or C#, however, you can call the DAQmx Base DLL functions in these IDEs. There is one important issue to be aware of: the DAQmx Base C DLL functions are exported using the cdecl calling convention, but the default calling convention in these IDEs is stdcall, thus extra effort is required. (Please find more information about these two calling convention in the Notes: section below.)

Visual Basic 6.0:
The DAQmx Base DLL cannot be called directly from VB 6.0 since it only supports calling C DLL functions which are exported using the stdcall calling convention. The only way around this is to create a wrapper C DLL that calls these functions, with the wrapper functions exported using the stdcall calling convention. You need only wrap the functions you require for your application. A wrapper C DLL for DAQmx Base 1.5 and a simple example demonstrating how to use it are attached. Before running the example, please follow the instructions in the README.txt located in the DAQmxBaseInVB6.zip file.

C# / Visual Basic.NET:
C# and Visual Basic.NET by default call C DLL functions using the stdcall calling convention, but they also support cdecl calling convention. Additional parameters for DllImport attribute need to be configured in order to allow the DAQmx Base C DLL functions to be called using the cdecl calling convention. Please refer to the following example. Note that the example is not a complete program but only demonstrates how to call DAQmx Base C DLL functions.

[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

Notes:

1. Many new USB products that were originally supported in NI-DAQmx Base now are supported by NI-DAQmx.
A conversion utility is available at ni.com/downloads.

· NI USB-6008 (does not have simulated device capability)
· NI USB-6009 (does not have simulated device capability)
· NI USB-6501 (does not have simulated device capability)
· NI USB-9201
· NI USB-9211A
· NI USB-9215A
· NI USB-9221
· NI USB-9233
· NI USB-9421
· NI USB-9472
· NI USB-9481

The SCXI-1600, DAQPad-6015 and 6016 were already supported. The USB-9211 and USB-9215 will be supported under NI-DAQmx Base only.

2. In the cdecl calling convention, the calling function is responsible for cleaning up the stack. Functions can have a variable number of arguments in cdecl.

In the stdcall calling convention, the called function is responsible for cleaning up the stack. Functions with a variable number of arguments do not work in stdcall. If you use the stdcall calling convention to call a function that is exported using the cdecl calling convention, both the caller and the callee assume the other will for pop arguments off of the stack; the code will run, but it will consume stack space.

National Instruments recommends you use the stdcall calling convention for all functions exported from a DLL, except functions with a variable number of arguments. Visual Basic and other non-C Windows programs expect DLL functions to be exported with stdcall. 



Related Links:
KnowledgeBase 317742FQ: Programming NI-DAQ in Text-Based Languages

Attachments:


DAQmxBaseVB6AnalogDigitalExample.zipDAQmxBaseInVB6.zip


Report Date: 07/14/2005
Last Updated: 09/05/2008
Document ID: 3NDDQLDD

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit