Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

CompactDaq AI with Visual Basic .NET

I am using the ReadSingleSample function so it should only be reading 1 sample per second per channel.  The loop is not too slow.  I made one version of the program that would time each process and it takes < 15 ms to create a file and <1 ms to write to it.  All the loop is doing is reading and writing the data.  Can you look at the project I have included as a zip file?  There has to be something wrong in either CreateDAQTasks or ReadData in the modFunctions file.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 71 of 115
(1,661 Views)

Hi,

You might want to check out the ContAcqThermocoupleSamples_IntClk example program that ships with DAQmx:

C:\Program Files\National Instruments\MeasurementStudioVS2003\DotNET\Examples\DAQmx\Analog In\Measure Temperature\ContAcqThermocoupleSamples_IntClk\Vb

It uses the beginReadMultiSample function.  I modified the program to read only one sample at a time and set the front panel to perform a 1 sample per second acquisition.  This example program ran on my cDAQ chassis with three 9211 modules (12 channels total) for several minutes.  Throughout that time it conistently returned data to the screen at 1 sample per second.

I would recommend using that example as a guide for your code.

Thanks,
Sal

0 Kudos
Message 72 of 115
(1,652 Views)
Michael,

I have been semi-watching this thread since the beginning when I posted a bit ago.  I have taken a look at your code and had a question.  In particular I am interested in this line of the modFunctions.vb file:

                Tasks(TaskCount).Timing.ConfigureSampleClock("", frm.numSampleRate.Value, SampleClockActiveEdge.Rising, _
                                                             SampleQuantityMode.ContinuousSamples, frm.numSamples.Value)

From your previous posts and from looking at your code I understand that when this is called the parameters have the following values:

frm.numSampleRate.Value = 14
frm.numSamples.Value = 2

Is this correct?  (This seems to be the default values you specified in frmCDAQTest.Designer.vb)

Are you then calling the function ReadData once a second?

If I am correct in these assumptions I may be able to explain your issue.  If frm.numSamplesRate.Value is set to 14, then the cDAQ will sample on every channel at 14 S/s.  The DAQmx driver does not do averaging, therefore every time you call ReadSingleSample, it only returns one of the 14 samples.  The second time you call the ReadSingleSample (in the next second) it actually returns the second sample of the first second.  This will occur until you receive the 14th sample of the 1st second in the 14th second.  It will then begin to read the first sample of the second second.  If you would like to average 14 readings every second you should read 14 values each second and perform the averaging in your application.  The frm.numSamples.Value is used by the driver to configure the size of the buffer that is used to store the values read from the 9211 in memory; it is not used to average points.  Since you are reading at a much slower rate (only once per second) than you are acquiring (14 times a second) eventually this will cause a memory buffer overflow, in turn causing the error you have seen.

Another issue with setting the sampling rate to 14 S/s is that you will get repeated data.  If you are sampling on every channel without CJC or Autozero the maximum sampling rate according to the manual is 3 times a second.  Only 3 unique values will be returned for each second.  This seems to correspond to your data.  Since the sampling rate is 14 S/s the first 14 points in your 93.txt correspond to the first second (not the first 14s as I explained above).  This data for Temp1 is:

168.1, 168.1, 168.1, 168.1, 168.1
167.8, 167.8, 167.8, 167.8, 167.8
167.7, 167.7, 167.7, 167.7

There are three unique values for the first second.  It seems to repeat this pattern for all the data in the table and confirms the specifications provided in the manual.

In addition to reading more often as Sal suggested, increasing frm.numSamples.Value to a higher number will increase the buffer size an may prevent the error that you have encountered.

If I am misinterpreting your application or don't have the parameters correct let me know.  We would love to find a resolution to your issue and are committed to helping you be successful.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 73 of 115
(1,644 Views)
I changed the program from ReadMultiSample to ReadSingleSample because multi wouldn't let me read 1 sample.  I thought that ReadSingleSample would read 1 sample per channel.  Are you saying that this is incorrect?  I just need to get one reading per channel per second.  I average 100 readings with SCXI, but these units are supposed to be much better and averaging isn't completely necessary.  I don't need to read three samples per channel per second until I figure out how to read one sample per channel per second!

I know that the problem must lie somewhere in the line you pasted, or the call to read samples.  The difference between my program and the example program is that it says BeginContinuousSample (or something) and EndContinuousSample.  I am just calling ReadMultiSample.  I am going to try the method that the example program uses tomorrow morning.  I just have to figure out where I will begin and where I will end the read.


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

If you only want to read one sample a second you must set

frm.numSampleRate.Value = 1

If you set the sample rate to 14 the driver will take 14 measurements a second and stick them into the DAQmx buffer whether you read them or not.  This operation is done completely independent of the DAQmxReadSingleSample.  The ReadSingleSample simply takes the next value out of the DAQmx buffer and returns it into user memory, it doesn't control when the actual measurement takes place.  This is controlled by the DAQmx driver when you are running in continuous mode (which you will have to do to get the 1 sample a second rate).

Let me know if you have additional questions.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 75 of 115
(1,630 Views)
Well....

I actually created the callback deal and it seems to be working.  I will post code and data from parallel SCXI and cDAQ systems in an hour or so.


Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 76 of 115
(1,622 Views)
I guess I should have done it the recommended way all along.  The callback deal fixed everything.  Thanks for your help.  Attached is the chart for comparison of one channel from cDAQ vs. SCXI.

    ' Read data reads the voltages or temperatures from each SCXI module.  The frequency
    ' is 1 Hz, and it will read and average 100 readings.
    Public Sub ReadData()
        Dim g, h, i, Count As Int16

        Try
            g = h = i = 0

            For Count = 0 To NumberOfModules - 1
                'Task1Data = reader(Count).ReadSingleSample()
                'For h = i To i + TasksChannelCount(Count) - 1
                'sum(h) = Task1Data(h, i)
                'Next
                For h = i To i + TasksChannelCount(Count) - 1
                    sum(h) = 0
                Next

                For h = i To i + TasksChannelCount(Count) - 1
                    For g = 0 To frm.numSamples.Value - 1
                        sum(h) = sum(h) + Task1Data(h - i, g)
                    Next
                Next

                i = h
            Next

            For h = 0 To numChannels
                With AIChannels(h)
                    average(h) = sum(h) / frm.numSamples.Value
                    value(h) = average(h) ^ 4 * .Quartic + _
                               average(h) ^ 3 * .Cubic + _
                               average(h) ^ 2 * .Quadratic + _
                               average(h) * .Linear + _
                               .Offset
                End With
            Next
        Catch ex As Exception
            MsgBox("ReadData" & vbNewLine & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub AnalogInCallback(ByVal ar As IAsyncResult)
        Try
            If taskRunning = True Then
                Task1Data = reader(0).EndReadMultiSample(ar)
                reader(0).BeginReadMultiSample(frm.numSamples.Value, myAsyncCallback, Nothing)
            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 77 of 115
(1,617 Views)
This new method poses a new problem.  Will I have to have an async call back for each task?  When using SCXI systems, I could have up to 12 AI tasks.  I guess I need 12 data arrays and 12 analogincallback functions now?
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 78 of 115
(1,610 Views)
Michael,

You can write a single callback to handle all tasks, but you will need to register the function for each task.  When you start the reading with the BeginReadMultiSample method you can specify an optional user defined state object as the third parameter.  This parameter will be passed to the callback function when it is called and can be used to determine which task should be read.  For more information on this functionality you should refer to the AnalogMultiChannelReader.BeginReadMultiSample Method documentation in the DAQmx .NET reference manual.

Let me know if you have any further questions.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 79 of 115
(1,594 Views)
Do you mean I should pass the task with the call of this function?  It doesn't specify what exactly to pass...

state
A user-provided object that distinguishes this asynchronous read request from other requests. Use this parameter to provide information to the callback. Specify a null reference (Nothing in Visual Basic) if you do not need to pass any additional information to the callback.

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