From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I start sampling voltage when calling method, and wait till sampling ends?

I'm using NI 9207 with cDAQ-9174, from Visual Studio 2013 (.net3.5)

 

What I want to do is,

 

- measure analog input voltage from NI 9207

- sampling speed 30msec interval ( = 30000Hz ), sampling count 1000

- call function then starts sampling, and waits till sampling is in end, then get analog input data ( 1000 sample datas)

 

And I wrote a code like below.

 

        public void Setup(string[] aiChannelNames, double max, double min, int samplingCount, double samplingSpeed_Hz)
        {
            try
            {
                if(aiTask != null)
                {
                    aiTask.Stop();
                    aiTask.Dispose();
                }

                aiTask = new Task(NI_MULTI_ADC_TASK_NAME);

                foreach (var aiChannelName in aiChannelNames)
                {
                    AIChannel aiChannel = aiTask.AIChannels.CreateVoltageChannel(
                        aiChannelName,
                        aiChannelName.Replace("/", ""),
                        AITerminalConfiguration.Differential,
                        min,
                        max,
                        AIVoltageUnits.Volts
                        );
                    aiChannelList.Add(aiChannel);
                }

                this.samplingCount = samplingCount;

                aiTask.Timing.ConfigureSampleClock(
                    string.Empty,   //  for internal clock
                    samplingSpeed_Hz,
                    SampleClockActiveEdge.Rising,
                    SampleQuantityMode.ContinuousSamples,
                    samplingCount
                    );

                multiChannelReader = new AnalogMultiChannelReader(aiTask.Stream);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public double[,] MultiRead()
        {
            double[,] multiSampleDatas = null;
            try
            {
                if (multiChannelReader == null)
                {
                    throw new Exception("NI_Multi_ADC not setup");
                }

                aiTask.Stop();
                //aiTask.Start();

                multiSampleDatas = multiChannelReader.ReadMultiSample(samplingCount);
            }
            catch (Exception)
            {
                throw;
            }
            return multiSampleDatas;
        }

 

First call Setup(...) method, and after, call MultiRead() method for several times.

 

In unit test, I tested MultiRead() function.

( called method Setup("...", 10, 0, 1000, 30000) )

For result,  this function does not takes time for 30msec * 1000 ( = 30sec ), method ended in about 300msec, but 1000 sampling data is returned.

 

For question,

 

1. Why ReadMultiSample() ends and returns data so quickly? When is sampling started?

2. Do you need asynchronous callback to wait till sampling data is sampled? Is there a class or method for this?

3. Witch sample code or document is good to read for this quesiton?

 

 

0 Kudos
Message 1 of 2
(4,705 Views)

Sorry, this was my mistake.

 

I was setting 30000Hz for sampling speed for 30msec interval, 

But correct sampling speed was 33.333...Hz for it.

 

The program has run that I wanted.

0 Kudos
Message 2 of 2
(4,691 Views)