Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmxRegisterEveryNSamplesEvent used correctly?

I have the following code :
 
'Add an analog input channel to the task.
DAQmxErrChk DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai0:7", "channel1", _
        DAQmx_Val_Cfg_Default, "-10", "10", _
        DAQmx_Val_VoltageUnits1_Volts, "")
            
'Configure task for continuous sample acquisition and read in data
DAQmxErrChk DAQmxCfgSampClkTiming(taskHandle, "OnboardClock", frmSettings.txtRate.Text, DAQmx_Val_Rising, _
             DAQmx_Val_AcquisitionType_ContSamps, CLng(frmSetMachine.txtSamplesPerChannel.Text))
'DAQmxErrChk DAQmxCfgSampClkTiming(taskHandle, "OnboardClock", frmSettings.txtRate.Text, DAQmx_Val_Rising, _
             'DAQmx_Val_AcquisitionType_ContSamps, CLng(frmSettings.txtRate.Text) / 10#)
            
'DAQmxCfgAnlgEdgeStartTrig taskHandle, "channel1", DAQmx_Val_Slope1_RisingSlope, #
DAQmxCfgAnlgEdgeStartTrig taskHandle, "channel1", DAQmx_Val_Slope1_RisingSlope, triggerVolume ' 0# ' triggerPressure
   'Initiate for Acquisition Event
DAQmxErrChk DAQmxGetTaskNumChans(taskHandle, numChannels)
etc etc
 
DummySub DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer, CLng(frmSetMachine.txtSamplesPerChannel.Text), 0, AddressOf EveryNSamplesCallback, Null)
 
I get a compile error : Invalid use of AddressOf operator.
 
The callback is:
 
Public Function EveryNSamplesCallback(taskHandle As Long, everyNsamplesEventType As Long, nSamples As Long, callbackData As Long) As Long
 
If I just do:
DummySub DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer, CLng(frmSetMachine.txtSamplesPerChannel.Text), 0, EveryNSamplesCallback, Null)
I get "argument not optional" and its pointing at EveryNSamplesCallback.
 
How do I fix this compile error? Also, is there anything else I need to add in code, or will this event fire automatically once I get past the compile issue?
thanks,
saroj

 
0 Kudos
Message 1 of 4
(9,547 Views)

I got the snippets of code from this previous thread

http://forums.ni.com/ni/board/message?board.id=230&message.id=2862

where someone posted sample code. He uses AddressOf, but I cannot compile his program. My program doesnt compile because it doesn't like the AddressOf

Tom, are you there? Could you please help with this issue?

thanks,

saroj

0 Kudos
Message 2 of 4
(9,539 Views)
Hello Saroj,

Are you trying to configure one AI voltage channel in your program?  I ask this because you have multiple calls to many of the functions such as
DAQmxCfgSampClkTiming.  If you only have one channel or one task you only need to reference that function once.

There seem to be a frew problems with your code, so instead of pointing out some of the errors and risking missing some, I'll point you to some great help material that should get you started.  First, there is a shipping example using the DAQmx Register Every N Samples Event that is installed when you install the NI-DAQmx driver.  If you left everything as default, it is installed in the C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Events\Every N Samples\Cont Acq-Int Clk-Every N Samples Event directory.

Second, it looks like some of your function calls have incorrect parameters, such as the sampleMode parameter requires either DAQmx_Val_FiniteSamps, DAQmx_Val_ContSamps, DAQmx_Val_HWTimedSinglePoint, your program calls DAQmx_Val_AcquisitionType_ContSamps - which unless you have this declared somewhere else should cause an error.  To help you troubleshoot your functions, the DAQmx C Reference Help located under Start >> All Programs >> National Instruments >> NI-DAQ is very useful. 

Between the example program and the DAQmx Help I think you'll be able to get it.  Post again if you have more problems and we'll be glad to help.

Regards,
Micaela N
National Instruments
0 Kudos
Message 3 of 4
(9,523 Views)
Hi,

The callback function must be declared in a .bas module, not in the form code.

The following code comes  from the  nidaqmx\Analog In\Measure Slow Varying Signal\One Sample sample and it works !!
(better than NI original code 🙂 )

good luck

Thierry



In the form code :

' stop task button
Private Sub bstoptask_Click()
    DAQmxErrChk DAQmxStopTask(taskHandle)
    taskIsRunning = False
End Sub

' start measurement
Private Sub startCommandButton_Click()
    Dim sampsPerChanRead As Long
    Dim errcode As Long
   
    On Error GoTo ErrorHandler
       
    If ValidateControlValues Then Exit Sub
    startCommandButton.Enabled = False

    '  Create the DAQmx task.
    DAQmxErrChk DAQmxCreateTask("", taskHandle)
       
    '  Add an analog input channel to the task.
    DAQmxErrChk DAQmxCreateAIVoltageChan(taskHandle, physicalChannelTextBox.Text, "", _
        DAQmx_Val_InputTermCfg_RSE, CDbl(Val(minValueTextBox.Text)), CDbl(Val(maxValueTextBox.Text)), _
        DAQmx_Val_VoltageUnits2_Volts, "")
   
    DAQmxErrChk DAQmxCfgSampClkTiming(taskHandle, "OnboardClock", 100, DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_ContSamps, 1000)
   
    DAQmxErrChk DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer, 100, 0, AddressOf backfonc, Null)
   
    DAQmxErrChk DAQmxStartTask(taskHandle)
    taskIsRunning = True
   
    Exit Sub
       
ErrorHandler:
    If taskIsRunning = True Then
        DAQmxStopTask taskHandle
        DAQmxClearTask taskHandle
        taskIsRunning = False
    End If
       
    startCommandButton.Enabled = True
    MsgBox "Error:" & Err.Number & " " & Err.Description, , "Error"
   
End Sub


In the bas module :

Public total As Long
Public arraydata(0 To 1000) As Double
Public taskHandle As Long
Public taskIsRunning As Boolean


Public Function backfonc(ByVal Hwnd As Long, ByVal lParam As Long, ByVal nSamples As Long, ByVal callbackData As Long) As Long

Dim p1 As Long

    total = total + 1   ( number of calls)
    p1 = DAQmxReadAnalogF64(taskHandle, -1, 10, DAQmx_Val_GroupByScanNumber, arraydata(0), 100, 0, ByVal 0&)
    Mainform.acquisitionDataTextBox = "callback " & total & "  val=" & arraydata(0)

End Function
0 Kudos
Message 4 of 4
(9,473 Views)