Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling Start on task dedicated to analog input takes an unexpectedly long amount of time

Hello friends,

 

I am working with a cDaq-9174 and a NI 9201 analog input module. I have a Task that is dedicated to reading analog input from 2 channels. I am finding that on my machine it takes anywhere from 70 to 100 ms for it's Start task member function to execute. This seems oddly long to me as  starting analog output tasks and digital output tasks takes significantly less time (< 10 ms) than it does for the analog input task. It is unclear to me if the time it takes this function to execute is expected or if it actually is taking unusually long. I am left with the following questions before I provide some code.

  • If Start is taking unusually long to execute, any ideas as to what might be causing this?
  • If Start is taking an expected amount of time, why does this take so long?

Below is the relevant code broken into 3 classes. The Program class is what gets executed at start up.

using System;
using System.Threading.Tasks;
class Program
{
    private AnalogInput _input;

    static void Main(string[] args)
    {
        var _input = new AnalogInput(new HardwareInfo());
        _input.Configure();
        Task.WaitAll(_input.PrepareHardware(1000));
        _input.Start();

        var key = Console.ReadKey(false).Key;
    }
}

The AnalogInput class contains the task and function call in question. It is shown below.

using System.Diagnostics;
using System.Threading;
using NationalInstruments.DAQmx;
using Task = System.Threading.Tasks.Task;
public class AnalogInput
{
    private NationalInstruments.DAQmx.Task _task;
    private AnalogMultiChannelReader _reader;
    private readonly HardwareInfo _hardwareInfo;
    public int NumberOfSamplesToRead { get; set; }
    private double[,] _readSamples { get; set; }

    public AnalogInput(HardwareInfo hardwareInfo)
    {
        _hardwareInfo = hardwareInfo;
        NumberOfSamplesToRead = 2;
    }

    public void Configure()
    {
        if (_task != null)
        {
            _task.Dispose();
            _task = null;
        }

        _task = new NationalInstruments.DAQmx.Task("AnalogInputTask");
        _task.AIChannels.CreateVoltageChannel(_hardwareInfo.AnalogInputModuleName + "/ai0", "ai0", (AITerminalConfiguration)(-1), -10.0, 10.0, AIVoltageUnits.Volts);
        _task.AIChannels.CreateVoltageChannel(_hardwareInfo.AnalogInputModuleName + "/ai1", "ai1", (AITerminalConfiguration)(-1), -10.0, 10.0, AIVoltageUnits.Volts);
        _task.Timing.ConfigureSampleClock("", _hardwareInfo.AnalogInputSamplingRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, NumberOfSamplesToRead);
        _task.ConfigureLogging("C:\\tmp\\input.tdms", TdmsLoggingOperation.OpenOrCreate, LoggingMode.LogAndRead, "Group Name");
        _task.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/" + _hardwareInfo.SecondDigitalIoModuleName + "/PFI0", DigitalEdgeStartTriggerEdge.Rising);
        _task.Stream.Timeout = Timeout.Infinite;
        _task.Control(TaskAction.Verify);
        _reader = new AnalogMultiChannelReader(_task.Stream);
    }
public Task PrepareHardware(int numberOfSamplesToRead) { return Task.Run(() => { NumberOfSamplesToRead = numberOfSamplesToRead * 4; _task.Timing.ConfigureSampleClock("", _hardwareInfo.AnalogInputSamplingRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, NumberOfSamplesToRead); }); } public void Start() { var sw = new Stopwatch(); sw.Start(); _task.Start(); sw.Stop(); Debug.WriteLine("Task start took " + sw.ElapsedMilliseconds + "ms"); sw.Restart(); _reader.BeginReadMultiSample(NumberOfSamplesToRead, asyncResult => { _readSamples = _reader.EndReadMultiSample(asyncResult); /* Do something with read samples here */ }, null); if (!sw.IsRunning) return; sw.Stop(); Debug.WriteLine("Begin read took " + sw.ElapsedMilliseconds + "ms"); } }

Although it's not relevant to the question, I will include the HardwareInfo class for completeness.

using System;
using System.Linq;
using NationalInstruments.DAQmx;
 public class HardwareInfo
{
    public HardwareInfo()
    {
        string[] namesOfAttachedDevices = DaqSystem.Local.Devices;

        if (namesOfAttachedDevices.Length > 0)
        {
            DeviceName = namesOfAttachedDevices.First();  // Guaranteed to be hardware or simulator I need.
        }
        else
        {
            throw new ApplicationException("No DAQ device connected or simulated.");
        }

        // Determine names of the modules in the compact daq.
        var device = DaqSystem.Local.LoadDevice(DeviceName);
        string[] moduleNames = device.ChassisModuleDeviceNames;
        AnalogOutputModuleName = moduleNames[0];
        AnalogInputModuleName = moduleNames[1];
        FirstDigitalIoModuleName = moduleNames[2];
        SecondDigitalIoModuleName = moduleNames[3];
    }

    public int AnalogOutputSamplingRate => 5000;

    public int AnalogInputSamplingRate => AnalogOutputSamplingRate * 4;

    public string DeviceName { get; }

    public string AnalogOutputModuleName { get; }

    public string AnalogInputModuleName { get; }

    public string FirstDigitalIoModuleName { get; }

    public string SecondDigitalIoModuleName { get; }
}

Below is an example of the Debug output from running the provided code.

Task start took 77ms
Begin read took 1ms

Any information you can share will be appreciated!

 

Edit: Changed title

0 Kudos
Message 1 of 3
(2,408 Views)

Hey b!tmaster!

 

So I can't say for certain if it should be taking that long, however what we can do, is run an example and benchmark that to see if that same timing behavior occurs for the example. That way we can narrow down and see if it is an issue that is specific to your code, or something else.

Applications Engineering
National Instruments
0 Kudos
Message 2 of 3
(2,380 Views)

Thanks for the suggestion. I will give this a shot when I get some extra time and report back. For now, I have adopted continuous analog input and changed when I begin the task to meet timing requirements for my application.

0 Kudos
Message 3 of 3
(2,374 Views)