NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Member
Schimmi
Posts: 1
0 Kudos

[C#] Digital Edge counting, Linear position, Read Buffered

Hello,

I'm trying to read buffered data from a NI USB-6210.

My goal is to sample the linear position(edge count) and the time the edge occurred.
For the time I use the internal sample clock.

But I don't get it to work. What am I doing wrong?


Thanks for your help!

My code for now:

*******************************************************

namespace LinearPositioningBuffered
{

    public partial class MainWindow : Window
    {
        private Task aiTask;
        private Task counterTask;
        private Task runningTask;
        private CounterReader counterInReader;
        private AnalogMultiChannelReader reader;
        private AsyncCallback asyncCB;

        private double[] data;
        private int BufferSize;

        public MainWindow()
        {
            InitializeComponent();
            uxStopButton.IsEnabled = false;
            asyncCB = new AsyncCallback(CounterInCallback);
        }


        private void ReadChannelBuffered()
        {         
            int sampleRate = 1000;
            BufferSize = sampleRate * 10;
            bool zIndexEnable = false;
            double zIndexValue = 0;
            double intialPosition = 0;
            double distPerPulse = 1;
            try
            {
                aiTask = new Task();
                // create a channel
                aiTask.AIChannels.CreateVoltageChannel("dev2/ai0", "", (AITerminalConfiguration)(-1), -5, 5, AIVoltageUnits.Volts);

                // Configure timing specs    
                aiTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 2000);

                // Verify the task
                aiTask.Control(TaskAction.Verify);
              
                counterTask = new Task();

                counterTask.CIChannels.CreateLinearEncoderChannel("dev2/ctr1", "LinearPosition", CIEncoderDecodingType.X1,
                    zIndexEnable, zIndexValue, CIEncoderZIndexPhase.AHighBLow, distPerPulse, intialPosition, CILinearEncoderUnits.Meters);
                
                //Configure timing
                counterTask.Timing.ConfigureSampleClock("/dev2/ai/SampleClock", 0, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, BufferSize);
                counterTask.Control(TaskAction.Verify);

                runningTask = counterTask;
                counterInReader = new CounterReader(counterTask.Stream);
                counterInReader.SynchronizeCallbacks = true;

                counterInReader.BeginReadMultiSampleDouble(BufferSize, asyncCB, counterTask);

                // Read the data
                reader = new AnalogMultiChannelReader(aiTask.Stream);
                reader.ReadSingleSample();

                uxStartButton.IsEnabled = false;
                uxStopButton.IsEnabled = true;

            }
            catch (DaqException exception)
            {
                counterTask.Dispose();
                runningTask = null;
                MessageBox.Show(exception.Message, "ReadChannelBuffered");
            }        
        }

        
        private void CounterInCallback(IAsyncResult ar)
        {
            try
            {
                if (runningTask == ar.AsyncState)
                {
                    data = counterInReader.EndReadMultiSampleDouble(ar);
                    for (int idx = 0; idx < data.Length; idx++)
                    {
                        Console.WriteLine(string.Format("{0}: {1}", idx, data[idx]));
                    }
                    counterInReader.BeginReadMultiSampleDouble(BufferSize, asyncCB, counterTask);
                }
            }
            catch (DaqException exception)
            {
                MessageBox.Show(exception.Message, "CounterInCallback");
                Stop();
            }
        }

        private void Stop()
        {
            runningTask = null;
            counterTask.Dispose();
            uxStartButton.IsEnabled = true;
            uxStopButton.IsEnabled = false;
        }

        private void uxStartButton_Click(object sender, RoutedEventArgs e)
        {
            ReadChannelBuffered();
        }

        private void uxStopButton_Click(object sender, RoutedEventArgs e)
        {
            Stop();
        }        
    }
}

By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page