Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Callback during continous sampling not working

Hello,

 

The callback in the added code doesn't work. The callback code has a comment of <------- CALLBACK NOT WORKING behind it. It just go through the ContSampleThrmcpl code once and then it just terminates. There are no compiler warnings as well. Google isn't of much help either. Anyone knows whats wrong?

 

 

With kind regards,

 

Yami_Bas

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using NationalInstruments.DAQmx;

namespace TorrefractieCS
{
    public class ContSampleThermocouple
    {
        private AnalogMultiChannelReader analogInReader;
        private AsyncCallback analogCallback;
        private NationalInstruments.AnalogWaveform<double>[] data;
        private Task myTask;
        private Task runningTask;
        private DataColumn[] dataColumn = null;
        private DataTable dataTable = null;
        private int sampsPerChanGlobal;
        private int counter = 0;

        public void ContSampleThrmcpl(string channels, int sampleRate, int sampsPerChan, double minVal, double maxVal)
        {
            
            dataTable = new DataTable();
            sampsPerChanGlobal = sampsPerChan;

            try
            {
                Console.WriteLine("Start");
                // Create task and channel
                myTask = new Task();
                AIChannel myChannel;

                // Set properties
                AIThermocoupleType thermocoupleType = AIThermocoupleType.K;
                AIAutoZeroMode autoZeroMode = AIAutoZeroMode.EverySample;

                // Configure channel
                myChannel = myTask.AIChannels.CreateThermocoupleChannel(channels, "", minVal, maxVal, thermocoupleType, AITemperatureUnits.DegreesC);
                myChannel.AutoZeroMode = autoZeroMode;
                myTask.Timing.ConfigureSampleClock("", (double)sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, sampsPerChan);

                // Create analog in reader
                myTask.Control(TaskAction.Verify);
                runningTask = myTask;
                analogInReader = new AnalogMultiChannelReader(myTask.Stream);
                analogCallback = new AsyncCallback(AnalogInCallback);
                
                // Prepare table for data
                InitializeDataTable(myTask.AIChannels, ref dataTable);

                // Use SynchronizeCallbacks to specify that the object 
                // marshals callbacks across threads appropriately.
                analogInReader.SynchronizeCallbacks = true;
                analogInReader.BeginReadWaveform(10, analogCallback, myTask); // <-------- CALLBACK NOT WORKING
                Console.WriteLine("Amount of callbacks: " + counter);
                counter++;
            }
            catch (DaqException exception)
            {
                MessageBox.Show(exception.Message);
                myTask.Dispose();
                runningTask = null;
            }
            
        }

        public void AnalogInCallback(IAsyncResult ar) // <-------- CALLBACK NOT WORKING
        {
            Console.WriteLine("Callback");
            try
            {
                if (runningTask != null && runningTask == ar.AsyncState)
                {
                    data = analogInReader.EndReadWaveform(ar);

                    dataToDataTable(data, ref dataTable);

                    analogInReader.BeginMemoryOptimizedReadWaveform(10, analogCallback, myTask, data);
                }
            }
            catch (DaqException exception)
            {
                MessageBox.Show(exception.Message);
                myTask.Dispose();
                runningTask = null;
            }

        }

        public void InitializeDataTable(AIChannelCollection channelCollection, ref DataTable data)
        {
            if (channelCollection == null)
            {
                return;
            }

            int numOfChannels = channelCollection.Count;
            data.Rows.Clear();
            data.Columns.Clear();
            dataColumn = new DataColumn[numOfChannels];
            int numOfRows = sampsPerChanGlobal;

            for (int currentChannelIndex = 0; currentChannelIndex < numOfChannels; currentChannelIndex++)
            {
                dataColumn[currentChannelIndex] = new DataColumn();
                dataColumn[currentChannelIndex].DataType = typeof(double);
                dataColumn[currentChannelIndex].ColumnName = channelCollection[currentChannelIndex].PhysicalName;
            }

            data.Columns.AddRange(dataColumn);

            for (int currentDataIndex = 0; currentDataIndex < numOfRows; currentDataIndex++)
            {
                object[] rowArr = new object[numOfChannels];
                data.Rows.Add(rowArr);
            }
            Console.WriteLine("Initialize DataTable");
        }

        public void dataToDataTable(NationalInstruments.AnalogWaveform<double>[] sourceArray, ref DataTable dataTable)
        {
            Console.WriteLine("Putting data in DataTable.");
            // Iterate over channels
            int currentLineIndex = 0;
            foreach (NationalInstruments.AnalogWaveform<double> waveform in sourceArray)
            {
                for (int sample = 0; sample < waveform.Samples.Count; ++sample)
                {
                    if (sample == 10)
                        break;

                    dataTable.Rows[sample][currentLineIndex] = waveform.Samples[sample].Value;
                }
                currentLineIndex++;
            }
        }

    }
}

 

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

Hello Yami_Bas,

 

Does the callback work if you set/change the following?

"analogInReader.SynchronizeCallbacks = false;"

 

Does this default example work?

http://zone.ni.com/reference/en-XX/help/370473H-01/mstudiowebhelp/html/daqsafelydisposetask/

 

I would like to troubleshoot it at my side, but I'm missing information concerning your forms to do this.
Would it be in any way possible to share the forms that you are using at your side? (eg.  by sharing a zip of your sample project)

Kind Regards,
Thierry C - CLA, CTA - Senior R&D Engineer (Former Support Engineer) - National Instruments
If someone helped you, let them know. Mark as solved and/or give a kudo. 😉
0 Kudos
Message 2 of 2
(5,335 Views)