Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

Here is how to use NI-DAQmx with Visual Basic 3.0 or 16-Bit 4.0

NI-DAQmx works very well with VB6 with the help of nidaqmx.tlb.

 

Here is an interesting twist. One can use  NI-DAQmx (nicaiu.dll) with 16-bit VB (VB3 and VB4).

 

Here are the declarations to use with CALL32.DLL for those who still use VB3 or 16-Bit VB4.

 

Option Explicit

Type NumDblL
  L1 As Long
  L2 As Long
End Type

Declare Function DAQmxResetDevice Lib "call32.DLL" Alias "call32" (ByVal DevName As String, ByVal id As Long) As Long
Declare Function DAQmxCreateTask Lib "call32.DLL" Alias "call32" (ByVal taskname As String, taskHandle As Long, ByVal id As Long) As Long
Declare Function DAQmxStartTask Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal id As Long) As Long
Declare Function DAQmxStopTask Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal id As Long) As Long
Declare Function DAQmxClearTask Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal id As Long) As Long
Declare Function DAQmxGetTaskNumChans Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, dataval As Long, ByVal id As Long) As Long
Declare Function DAQmxCreateAIVoltageChan Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal physicalChannel As String, ByVal myName As String, ByVal DAQmxValCfgDefault As Long, ByVal MinVoltsLoLong As Long, ByVal MinVoltsHiLong As Long, ByVal MaxVoltsLoLong As Long, ByVal MaxVoltsHiLong As Long, ByVal DAQmxValVoltageUnits As Long, ByVal reserved As Any, ByVal id As Long) As Long
Declare Function DAQmxReadAnalogF64 Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal numSampsPerChan As Long, ByVal TimeoutLoLong As Long, ByVal TimeoutHiLong As Long, ByVal fillMode As Long, readArray As Double, ByVal arraySizeInSamps As Long, sampsPerChanRead As Long, ByVal reserved As Long, ByVal id As Long) As Long
Declare Function DAQmxGetErrorString Lib "call32.DLL" Alias "call32" (ByVal errorCode As Long, ByVal errorString As String, ByVal bufferSize As Long, ByVal id As Long) As Long
Declare Function DAQmxCreateDIChan Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal lines As String, ByVal myName As String, ByVal linegrouping As Long, ByVal id As Long) As Long
Declare Function DAQmxReadDigitalLines Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal numSampsPerChan As Long, ByVal TimeoutLoLong As Long, ByVal TimeoutHiLong As Long, ByVal fillMode As Long, readArray As Any, ByVal arraySizeInDIBytes As Long, sampsPerChanRead As Long, numBytesPerSamp As Long, ByVal reserved As Long, ByVal id As Long) As Long
Declare Function DAQmxGetSysNIDAQMajorVersion Lib "call32.DLL" Alias "call32" (dataval As Long, ByVal id As Long) As Long
Declare Function DAQmxCreateDOChan Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal lines As String, ByVal nameToAssignToLines As String, ByVal linegrouping As Long, ByVal id As Long) As Long
Declare Function DAQmxWriteDigitalLines Lib "call32.DLL" Alias "call32" (ByVal taskHandle As Long, ByVal numSampsPerChan As Long, ByVal autoStart As Long, ByVal TimeoutLoLong As Long, ByVal TimeoutHiLong As Long, ByVal DAQmxValGroup As Long, writeArray As Any, sampsPerChanWritten As Long, ByVal reserved As Long, ByVal id As Long) As Long

Dim id_DAQmxCreateTask&
Dim id_DAQmxStartTask&
Dim id_DAQmxStopTask&
Dim id_DAQmxClearTask&
Dim id_DAQmxGetTaskNumChans&
Dim id_DAQmxCreateAIVoltageChan&
Dim id_DAQmxReadAnalogF64&
Dim id_DAQmxGetErrorString&
Dim id_DAQmxCreateDIChan&
Dim id_DAQmxReadDigitalLines&
Dim id_DAQmxGetSysNIDAQMajorVersion&
Dim id_DAQmxCreateDOChan&
Dim id_DAQmxWriteDigitalLines&
Dim id_DAQmxResetDevice&

 

Sub InitNIDAXmx ()
Dim junk As Long
 

    id_DAQmxGetSysNIDAQMajorVersion& = Declare32("DAQmxGetSysNIDAQMajorVersion", "nicaiu.dll", "p")
    id_DAQmxResetDevice& = Declare32("DAQmxResetDevice", "nicaiu.dll", "p")
    id_DAQmxCreateTask& = Declare32("DAQmxCreateTask", "nicaiu.dll", "pp")
    id_DAQmxStartTask& = Declare32("DAQmxStartTask", "nicaiu.dll", "i")
    id_DAQmxStopTask& = Declare32("DAQmxStopTask", "nicaiu.dll", "i")
    id_DAQmxClearTask& = Declare32("DAQmxClearTask", "nicaiu.dll", "i")
    id_DAQmxGetTaskNumChans& = Declare32("DAQmxGetTaskNumChans", "nicaiu.dll", "ip")
    id_DAQmxGetErrorString& = Declare32("DAQmxGetErrorString", "nicaiu.dll", "ipi")
    id_DAQmxCreateAIVoltageChan& = Declare32("DAQmxCreateAIVoltageChan", "nicaiu.dll", "ippiiiiiip")
    id_DAQmxReadAnalogF64& = Declare32("DAQmxReadAnalogF64", "nicaiu.dll", "iiiiipipi")
    id_DAQmxCreateDIChan& = Declare32("DAQmxCreateDIChan", "nicaiu.dll", "ippi")
    id_DAQmxReadDigitalLines& = Declare32("DAQmxReadDigitalLines", "nicaiu.dll", "iiiiipippp")
    id_DAQmxCreateDOChan& = Declare32("DAQmxCreateDOChan", "nicaiu.dll", "ippi")
    id_DAQmxWriteDigitalLines& = Declare32("DAQmxWriteDigitalLines", "nicaiu.dll", "iiiiiippp")

    DAQmxErrChk DAQmxGetSysNIDAQMajorVersion(junk, id_DAQmxGetSysNIDAQMajorVersion&)

    Debug.Print "NI-DAQmx Major Version =" & Str$(junk) 'This will print 9 for version 9.0.2f

 

End Sub

 

Sub DblToLL (D#, LoLong As Long, HiLong As Long)
Dim DblLong As NumDblL
  StrD.S = MKD$(D#)
  LSet DblLong = StrD
  LoLong = DblLong.L1
  HiLong = DblLong.L2
End Sub

 

DblToLL sub splits a 64-bit (float64 in C) Double into two Long integers (this is required for using CALL32.DLL 'i' types for values like Timeout, MinVoltage, MaxVoltage)

 

Works really well for me.

 

I will be adding AO and all COUNTER functions soon.

 

Enjoy!

 

0 Kudos
Message 1 of 3
(7,309 Views)

Here is the complete module (attached) for VB3 or 16-Bit VB4 for using AI, AO, DI, DO boards/ports with NI-DAQmx. You will need CAL32.DLL (free) and nicaiu.dll that is part of NI-DAQmx installation. Trickiest part was to convert ByVal Double (float64) to two Long integers (packed next to each other on stack). VB's Lset command saved the day. Sub InitNIDAXmx must be called first before using any other Sub/Function in this module.

 

0 Kudos
Message 2 of 3
(7,267 Views)

Hey VBFan,

 

This would also be a great addition to the Community page. Thanks for posting! 

Regards,


h_baker
National Instruments
Applications Engineer
Message 3 of 3
(7,239 Views)