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