Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

edge counter

I'm using a USB-6009, trying to read the counter with on demand timing with Measurement Studion .net 2010. This is the code I'm using to find the edge counter task. 

foreach (String s in DaqSystem.Local.Tasks)
            {

            using (Task t = DaqSystem.Local.LoadTask(s))
                    {
                     if (t.CIChannels.Count > 0)
                        {
                                                        for (int a = 0; a < t.CIChannels.Count; a++)
                            {
                                                                counterTask = s;
                                
                                t.CIChannels[a].CountEdgesActiveEdge = CICountEdgesActiveEdge.Falling;
                                t.CIChannels[a].CountEdgesCountDirection = CICountEdgesCountDirection.Up;                                
                                t.Control(TaskAction.Verify);
                                t.Start();
                                
                            }
                            displayText.Append(channels.ToString());
                        }        

                    }


            }


 I have a timer set up to read the counter value at specific intervals so I can calculate the events per second of the counter input. I use this code to read the counter. Right now this code is called from a button_click event. The counter value is always 0. The task works correctly in NI MAX, the counter value increases, and the rate of increase is proportional to the frequency input on ctr0, but in my code the CounterReader always returns 0.

UInt32 newCount = 0;
            double[] analog1;

            try
            {
                using (Task cntrTask = DaqSystem.Local.LoadTask(counterTask))
                {
                    cntrTask.Control(TaskAction.Verify);
                    cntrTask.Stream.Timeout = 1000;
                    
                    CounterReader cntReader = new CounterReader(cntrTask.Stream); 

                    newCount = cntReader.ReadSingleSampleUInt32();

                    textBoxCounter.Text = newCount.ToString();                    
                }

                using (Task adTask = DaqSystem.Local.LoadTask(analogTask))
                {
                    adTask.Stream.Timeout = 1000;
                    AnalogMultiChannelReader analogReader = new AnalogMultiChannelReader(adTask.Stream);
                    analog1 = analogReader.ReadSingleSample();
                    textBoxAnalog.Text = analog1[0].ToString();
                }

            }
            catch (DaqException ex)
            {
                //USBTimer.stopTimer();
                MessageBox.Show(ex.Message);                
            }

 There must be something I'm missing about using the counter, the anaolg voltages are always correct.

 

Any help would be much appreciated.

 

Thanks.

 

0 Kudos
Message 1 of 2
(5,093 Views)

The problem with this is that the "using" keyword disposes of all the objects created in its block when code execution exits the block. The counter task has to be global so it can run. I created a global task attaching it to a CIChannel in the declaration, then verified and started it in the Form.Load event handler and the task now reports the updated counter value.

0 Kudos
Message 2 of 2
(5,083 Views)