Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

CompactDaq AI with Visual Basic .NET

Michael,

I would imagine this would be the easiest way to accomplish this.  This would allow you to use the object passed to the callback function to read the data and to BeginReadMultiSample again.
Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 81 of 115
(2,171 Views)
It's not so straightforward.  If I have 12 tasks, then I need 12 readers and 12 arrays to store the data.  How do I handle selection of which reader and array I am using for any given call to the callback function?

    Private Sub AnalogInCallback(ByVal ar As IAsyncResult)
        Try
            If taskRunning = True Then
                Task1Data = reader(0).EndReadMultiSample(ar)
                reader(0).BeginReadMultiSample(numSamples, myAsyncCallback, ?????)
            End If
        Catch exception As DaqException
            MessageBox.Show(exception.Message)
            Tasks(0).Dispose()
            taskRunning = False
        End Try
    End Sub
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 82 of 115
(2,157 Views)
Is this feasible?

    Private Sub AnalogInCallback(ByVal ar As IAsyncResult)
        Try
            If taskRunning = True Then
                for x = 1 to numTasks
                    DataArray(x) = reader(x).EndReadMultiSample(ar)
                    reader(x).BeginReadMultiSample(numSamples, myAsyncCallback, Nothing)
                next x
            End If
        Catch exception As DaqException
            MessageBox.Show(exception.Message)
            Tasks(0).Dispose()
            taskRunning = False
        End Try
    End Sub

Message Edited by SCXI and MS 2k3-VB.NET on 01-25-2007 02:37 PM

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 83 of 115
(2,150 Views)
Michael,

I don't think that approach would work.  Just because samples on one task are ready it does not mean all the tasks will be.  I believe you can define a second parameter to be passed into the Callback function of type Object.  You will then need to cast the object into a reader object and pass the reader in when calling DAQmxReadMultiSample method.

I would expect it to look something like this:

Private Sub AnalogInCallback(ByVal ar As IAsyncResult, callback_reader As Object)
    Try
       temp_reader = CType(callback_reader, DAQmx.AnalogMultiChannelReader)
 
           If taskRunning = True Then
                    DataArray = temp_reader.EndReadMultiSample(ar)
                    temp_reader.BeginReadMultiSample(numSamples, myAsyncCallback, temp_reader)
            End If
        Catch exception As DaqException
            MessageBox.Show(exception.Message)
            Tasks(0).Dispose()
            taskRunning = False
        End Try
    End Sub


I must warn you that I have not had the chance to try this out.  I have been away from my desk today and haven't had access to MS Visual Studio.  I should be able to try out some options and give you a more complete answer tomorrow.  I just wanted to give you an idea of where I was heading.  Let me know if you discover anything more.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 84 of 115
(2,141 Views)
Michael,

I have finally had the chance to set up and example program and am successfully able to determine which task is calling the callback method.
Instead of passing the task object, I decided to pass an integer to describe which task was calling the callback.   Below is the code of both where I register the callback and the callback itself:

'Registering the Callbacks
for x = 0 to numTasks
     reader(x).BeginReadMultiSample(Convert.ToInt32(samplesPerChannel.Value), analogCallback, x)
next x

'Callback function used by all tasks
Private Sub AnalogInCallback(ByVal ar As IAsyncResult)
    Dim tasknumber As Integer
    tasknumber = CType(ar.AsyncState, Integer) 'cast the user object passed when registering the callback to an integer
    Try
        If taskRunning = True Then
            data(tasknumber) = reader(tasknumber).EndReadMultiSample(ar)
            reader(tasknumber).BeginReadMultiSample(Conver.ToInt32(samplesPerChannel.Value), analogCallback, tasknumber)
        End If
    Catch ex As DaqException
       MessageBox.Show(ex.Message)
       taskRunning = False
       for x = 0 to numTasks
          task(tasknumber).Dispose()
       next x
    End Try
End Sub
   
I hope this gets you up and running.  Let me know if you have any questions about this approach.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 85 of 115
(2,126 Views)
This is MUCH simpler than my 12 callbacks.  I'll let you know if it works.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 86 of 115
(2,124 Views)
When I am taking data in this manner, how can I know how long the data takes from read to store in buffer to send to application?  I need to be sure that the temperatures I am seeing at this second are the temperatures that happened this second?  Is it a function of the rate and number of samples taken?  If I have voltages and thermocouples on the same task, I assume that the task will wait to be returned until all values are in the buffer.  If my sample rate is 25,000 and I call for 20 samples, shouldn't it return data to the callback function in this amount of time (number of samples * number of channels / sample rate)

Thanks.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 87 of 115
(2,067 Views)
I finally have the computer hooked up and all of the chassis installed.  I hooked up one thermocouple to each module, but those crazy 9211s are not giving me data fast enough.  The AI modules allow the program to run at 10 Hz when I request 10-20 samples per channel.  As soon as I add a thermocouple channel to the list, my loop times go way up.  Shouldn't the modules return the data from the buffer even if they aren't able to read fast enough?

My current sample rate is 25 kS/S, trying to read 2,10,20 samples per channel.  One task, one chassis, 1-3 modules.  Here is some data from just the AI module:
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 88 of 115
(2,062 Views)
Michael,

Are you acquiring the same number of samples from each channel?  Or are you trying to read 2 from one channel, 10 from another, and 20 from another?  When you say you cannot read data fast enough are you getting an error or is it some other behavior that is making it seem that data is not be read fast enough?

When data is acquired and moved into the application there is some delay, however, the amount of time it takes for this transfer to take place with be dependent on the system and is not a simple formula.  The DAQmx read will not return until all the samples you have requested have been acquired and moved into the buffer.

I am not sure if any of this is helpful but I am a little unsure as to what the issue is now.  Could you provide me some psuedocode of what you are trying to accomplish with the analog channels and the thermocouple channels?

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 89 of 115
(2,052 Views)
I can read fast enough right now using the callback...

When I read finite samples each second, I know that the temperature I read that second is the actual temperature THAT second.  With this callback deal, I don't know when the data is stored in the buffer.  If I am asking for x samples per channel, and y sample rate > than 9211s, can I assume that I will get 12 samples in the buffer per second?
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 90 of 115
(2,049 Views)