Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

CompactDaq AI with Visual Basic .NET

Michael,

The 9211 will return the exact same number of samples as every other clocked analog module in the chassis.  If you run the modules at 20 kS/s the 9211 will return 20,000 samples per channel into the buffer every second.  Most of these samples will be repeated samples; however, they will all be samples from that single second.  If you specify the callback to run every 20,000 samples then you will be able to read the samples every second as long as your computer can keep up.  This will be just as useful as calling read every second using software control. 

When you set up a timed analog acquisition and start the task, the DAQmx driver starts buffering samples into memory at the rate you specified (ie 20,000 samples every second per channel if you set the rate to 20kS/s).  This happens independent of your code.  However when you use a call back function the DAQmx driver will "alert" your application when a certain number of samples have been acquired, say every 20,000 samples on each channel.  This in essence will cause the callback function to run every second.   The only issue occurs if the callback function takes longer than 1 second to run (which I think it unlikely in your case).

Hopefully this sheds some light on the issue.  Let me know if you have additional questions relating to the same topic.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 91 of 115
(1,620 Views)
Now I am trying to read SCXI using the same code and I am getting the following error:

3:51    AnalogInCallback
NationalInstruments.DAQmx.DaqException: The specified resource is reserved. The operation could not be completed as specified.

Task Name: mod1task

Status Code: -50103
   at h1.EndReadMultiSample(IAsyncResult asyncResult)
   at NationalInstruments.DAQmx.AnalogMultiChannelReader.EndReadMultiSample(IAsyncResult asyncResult)
   at DEER_DAQ.modCallbacks.AnalogInCallback(IAsyncResult ar)

Will post code below.  It will read the first module, but nothing after that.

Any ideas?
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 92 of 115
(1,567 Views)
Create Tasks:
        While ChannelCount < numChannels + 1
            Try
                Do
                    TasksChannelCount(TaskCount) += 1

                    With AIChannels(ChannelCount)
                                Tasks(TaskCount).AIChannels.CreateVoltageChannel(.Device & "/" & .Channel, _
                                                .Name, AITerminalConfiguration.Differential, _
                                                .Min, .Max, AIVoltageUnits.Volts)
                                If Not .Device.Contains("cDAQ") Then
                                    Tasks(TaskCount).AIChannels(.Name).AutoZeroMode = AIAutoZeroMode.None
                                    Tasks(TaskCount).AIChannels(.Name).LowpassEnable = True
                                    Tasks(TaskCount).AIChannels(.Name).LowpassCutoffFrequency = 4
                                End If
                    End With
                    ChannelCount += 1
                    If ChannelCount = numChannels + 1 Then
                        Exit Do
                    End If
                Loop While AIChannels(ChannelCount).Task = AIChannels(ChannelCount - 1).Task
            Catch ex As Exception
                MsgBox("AI Tasks" & vbNewLine & ex.Message)
            End Try

            Try
                If AIChannels(0).Device.Contains("cDAQ") Then
                        Tasks(TaskCount).Control(TaskAction.Unreserve)
                        Tasks(TaskCount).Timing.ConfigureSampleClock("", 14, SampleClockActiveEdge.Rising, _
                                                                    SampleQuantityMode.ContinuousSamples, numSamples)
                        Tasks(TaskCount).Control(TaskAction.Verify)
                        reader(TaskCount) = New AnalogMultiChannelReader(Tasks(TaskCount).Stream)
                        reader(TaskCount).SynchronizeCallbacks = True
                        taskRunning = True
                        reader(TaskCount).BeginReadMultiSample(numSamples, AsyncCallback, TaskCount)
                Else
                    Tasks(TaskCount).Control(TaskAction.Unreserve)
                    Tasks(TaskCount).Timing.ConfigureSampleClock("", SampleRate, SampleClockActiveEdge.Rising, _
                                                                SampleQuantityMode.ContinuousSamples, numSamples)

                    Tasks(TaskCount).Control(TaskAction.Verify)
                    reader(TaskCount) = New AnalogMultiChannelReader(Tasks(TaskCount).Stream)
                    reader(TaskCount).SynchronizeCallbacks = True
                    taskRunning = True
                    reader(TaskCount).BeginReadMultiSample(numSamples, AsyncCallback, TaskCount)
                End If
            Catch ex As Exception
                MsgBox("Task Timing" & vbNewLine & ex.Message)
            End Try
            TaskCount += 1
            TasksChannelCount(TaskCount) = 0
            'Device = AIChannels(ChannelCount).Device
        End While
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 93 of 115
(1,563 Views)
    Public Sub AnalogInCallback(ByVal ar As IAsyncResult)
        Dim TaskNumber, x As Byte
        TaskNumber = CType(ar.AsyncState, Integer) 'cast the user object passed when registering the callback to an integer
        Try
            If taskRunning = True Then
                Select Case TaskNumber
                    Case Is = 0
                        Task1Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 1
                        Task2Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 2
                        Task3Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 3
                        Task4Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 4
                        Task5Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 5
                        Task6Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 6
                        Task7Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 7
                        Task8Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 8
                        Task9Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 9
                        Task10Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 10
                        Task11Data = reader(TaskNumber).EndReadMultiSample(ar)
                    Case Is = 11
                        Task12Data = reader(TaskNumber).EndReadMultiSample(ar)
                End Select
                'DataArray(TaskNumber).Array = reader(TaskNumber).EndReadMultiSample(ar)
                If TaskNumber = 0 And AIChannels(0).Device.Contains("cDAQ") Then
                    reader(TaskNumber).BeginReadMultiSample(numSamples * 10, AsyncCallback, TaskNumber)
                Else
                    reader(TaskNumber).BeginReadMultiSample(numSamples, AsyncCallback, TaskNumber)
                End If
            End If
        Catch ex As DaqException
            Try
                swError = New StreamWriter(Application.StartupPath & "\Errors\" & RunNumber & "_" & "Error Log.txt", True)
                swError.WriteLine(TimeString & vbTab & "AnalogInCallback" & vbNewLine & ex.ToString & vbNewLine & vbNewLine)
                swError.Close()
                reader(TaskNumber).BeginReadMultiSample(numSamples, AsyncCallback, TaskNumber)
            Catch ex1 As Exception
                MessageBox.Show("Unable to start reading.")
            End Try
            MessageBox.Show("Callback Function" & vbNewLine & ex.ToString)
        End Try
    End Sub
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 94 of 115
(1,561 Views)

Hi Michael,

 

There is actually a Knowledge Base entry on this exact error message.

 

Causes of NI-DAQmx Error 50103 "The Specified Resource is Reserved"

 

Please post back if you have further problems after going through the article.  Also, for your convenience and the readability of our forums, I would recommend simply attaching code as a file rather than copying it into separate posts. 

 

Ed W.

Applications Engineer

National Instruments

0 Kudos
Message 95 of 115
(1,548 Views)
I understand what causes the problem from a NI driver standpoint, but this code works on CompactDAQ using 1 task per chassis.  All I did was try to run it on an SCXI chassis with 11 modules and 11 tasks and it no longer works.  I have verified that it is creating a task and putting no more than 8 channels in each task.  The first set of channels works, but the rest don't.  What makes it even more weird is that the first time I ran it today, module 2 was not throwing an error.  There error happens when I can to end a read.  I can see that I am creating the tasks and starting them with the code listed above.

From a log:

TaskCount: 0    TasksChannelCount: 1    Speed
TaskCount: 0    TasksChannelCount: 2    Load
TaskCount: 0    TasksChannelCount: 3    Inlet LFE Delta P
TaskCount: 0    TasksChannelCount: 4    Ambient Pressure
TaskCount: 0    TasksChannelCount: 5    EO BP
TaskCount: 0    TasksChannelCount: 6    LFE Inlet Temp
TaskCount: 0    TasksChannelCount: 7    DPF Delta P
TaskCount: 0    TasksChannelCount: 8    AFR
Reader(0) started.
TaskCount: 1    TasksChannelCount: 1    Boost Before
TaskCount: 1    TasksChannelCount: 2    Boost After
TaskCount: 1    TasksChannelCount: 3    O2 Before DPF
TaskCount: 1    TasksChannelCount: 4    O2 After DPF
TaskCount: 1    TasksChannelCount: 5    Micromotion
TaskCount: 1    TasksChannelCount: 6    BP Valve
Reader(1) started.

14:53:20    EndRead
NationalInstruments.DAQmx.DaqException: The specified resource is reserved. The operation could not be completed as specified.

Task Name: mod1task

Status Code: -50103
   at h1.EndReadMultiSample(IAsyncResult asyncResult)
   at NationalInstruments.DAQmx.AnalogMultiChannelReader.EndReadMultiSample(IAsyncResult asyncResult)
   at DEER_DAQ.modCallbacks.AnalogInCallback(IAsyncResult ar)
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 96 of 115
(1,538 Views)
Little extra information...

Two days in a row now the program read from modules 1 and 2 (i'm not using the other 9 right now) the first time I ran the program, but after that first run it doesn't work anymore.  What does time have to do with anything?  I'm resetting the modules before each run.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 97 of 115
(1,528 Views)

Hi Michael,

 

What do you mean by resetting the devices before each run – are you power cycling the chassis or are you resetting the devices in MAX?  This error can usually be cleared up by right clicking on the devices in MAX and selecting Reset Device…  If this does not fix the issue, try power cycling the chassis.  Although this may not be a feasible long term solution, it will give us a better idea of the problem.

 

Thanks.

 

Ed W.

Applications Engineer

National Instruments

0 Kudos
Message 98 of 115
(1,526 Views)
Michael,

Are you trying to run multiple tasks at the same time?  If all of your modules are in one SCXI chassis, could you use one task?

Regards,
Kyle
0 Kudos
Message 99 of 115
(1,519 Views)
        Try
            For i = 1 To NumberOfModules
                frmD.txtMessage.Text = frmD.txtMessage.Text & vbNewLine & "Resetting Module " & i
                dvc = DaqSystem.Local.LoadDevice("SC1Mod" & i.ToString) ' Set dvc = SC1Mod1
                dvc.Reset() ' Reset Device
            Next
        Catch ex As Exception
            If MsgBox("Please ensure that the SXCI chassis is turned on.  Possible reasons for failure " & _
                    "include a blown fuse, being switched off, a bad power supply or being unplugged. " & _
                    "Press OK when SCXI chassis is powered on or cancel to quit.", MsgBoxStyle.OkCancel, _
                    "Warning!") = MsgBoxResult.Ok Then
                ResetDevices()
            End If
        End Try

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 100 of 115
(1,521 Views)