public void Stop()
        {
            if (TaskHandle == 0)
            {
                throw new InvalidOperationException("Task not yet created. First create a task");
            }
            _task.Stop();

            // clear old data
            for (int i = 0; i < _nodes.Count; i++)
            {
                for (int j = 0; j < _usableData.GetLength(1); j++)
                {
                    _usableData[i, j] = 0;
                }

                for (int j = 0; j < _bufferData.GetLength(1); j++)
                {
                    _bufferData[i, j] = 0;
                }

                _channelCount[i] = 0;
            }

            State = SessionTaskState.Stopped;
        }
 public void DestroyTask()
 {
     _task.Dispose();
     TaskHandle        = 0;
     SamplesPerChannel = 0;
     _nodes.Clear();
     State = SessionTaskState.None;
 }
 public void Stop()
 {
     if (TaskHandle == 0)
     {
         throw new InvalidOperationException("Task not yet created. First create a task");
     }
     _task.Stop();
     State = SessionTaskState.Stopped;
 }
        public void DestroyTask()
        {
            if (TaskHandle != 0)  //throw new InvalidOperationException("Task not yet created. First create a task");
            {
                NidaQmxHelper.DAQmxClearTask(TaskHandle);
                TaskHandle = 0;
            }

            SamplesPerChannel = 0;
            _nodes.Clear();
            State = SessionTaskState.None;
        }
Example #5
0
 private void CleanupAndThrow(int code)
 {
     NidaQmxHelper.DAQmxClearTask(TaskHandle);
     TaskHandle        = 0;
     SamplesPerChannel = 0;
     _nodes.Clear();
     State = SessionTaskState.None;
     if (_counter != null)
     {
         _counter.Dispose();
     }
     throw new NidaqException(code);
 }
        public void Stop()
        {
            if (TaskHandle == 0)
            {
                throw new InvalidOperationException("Task not yet created. First create a task");
            }

            var result = NidaQmxHelper.DAQmxStopTask(TaskHandle);

            if (result < 0)
            {
                CleanupAndThrow(result, false);
            }

            State = SessionTaskState.Stopped;
        }
Example #7
0
        public void DestroyTask()
        {
            if (TaskHandle == 0)
            {
                throw new InvalidOperationException("Task not yet created. First create a task");
            }

            NidaQmxHelper.DAQmxClearTask(TaskHandle);
            TaskHandle        = 0;
            SamplesPerChannel = 0;
            _nodes.Clear();
            if (_counter != null)
            {
                _counter.Dispose();
            }
            State = SessionTaskState.None;
        }
        private void CleanupAndThrow(int code, bool doThrow = true)
        {
            NidaQmxHelper.DAQmxClearTask(TaskHandle);
            TaskHandle        = 0;
            SamplesPerChannel = 0;
            _nodes.Clear();
            State = SessionTaskState.None;

            if (doThrow)
            {
                throw new NidaqException(code);
            }
            else
            {
                Parent.SessionGraph.Context.Notify(
                    new NodeSystemLib2.Generic.GraphNotification(
                        NodeSystemLib2.Generic.GraphNotification.NotificationType.Error,
                        NidaQmxHelper.GetError(code)
                        )
                    );
            }
        }
Example #9
0
        public void Stop()
        {
            if (TaskHandle == 0)
            {
                throw new InvalidOperationException("Task not yet created. First create a task");
            }

            var result = NidaQmxHelper.DAQmxStopTask(TaskHandle);

            if (result < 0)
            {
                CleanupAndThrow(result);
            }

            try {
                _counter.Stop();
            } catch (NationalInstruments.DAQmx.DaqException e) {
                CleanupAndThrow(e.Error);
            }

            State = SessionTaskState.Stopped;
        }
        /// <summary>
        /// Creates a new DAQmx task and adds channels to it
        /// </summary>
        /// <param name="nodes">Graph nodes of type MetricAnalogInput</param>
        /// <exception cref="InvalidCastException">At least one element in <paramref name="nodes"/> not of type MetricAnalogInput</exception>
        /// <exception cref="InvalidOperationException">At least one element in <paramref name="nodes"/> not connected to the instance's specified device or task already created</exception>
        /// <exception cref="NidaqException">Task or channel could not be created</exception>
        public void CreateTask(IEnumerable <INidaqMetric> nodes)
        {
            if (TaskHandle != 0)
            {
                throw new InvalidOperationException("Task already created. First destroy the old task");
            }

            if (!nodes.All(n => n is MetricAnalogInput))
            {
                throw new InvalidCastException("all passed nodes must be of type MetricAnalogInput");
            }

            if (!nodes.All(n => n.Channel.Device == Device))
            {
                throw new InvalidOperationException("not all passed nodes are connected to device " + Device.Name);
            }

            foreach (var node in nodes.OfType <MetricAnalogInput>())
            {
                node.Samplerate = ClockRate;
            }

            // 1. create task
            var taskHandle = new int[1];
            var result     = NidaQmxHelper.DAQmxCreateTask(null, taskHandle);

            if (result < 0)
            {
                throw new NidaqException(result);
            }
            TaskHandle = taskHandle[0];

            // 2. create channels
            foreach (var input in nodes.OfType <MetricAnalogInput>())
            {
                result = NidaQmxHelper.DAQmxCreateAIVoltageChan(
                    maxVal:                 input.VMax,
                    minVal:                 input.VMin,
                    units:                  NidaQmxHelper.DaQmxValVolts,
                    terminalConfig:         (int)input.TerminalConfig,
                    physicalChannel:        input.Channel.Path,
                    taskHandle:             TaskHandle,
                    nameToAssignToChannel:  null,
                    customScaleName:        null
                    );
                if (result < 0)
                {
                    CleanupAndThrow(result);
                }
                _nodes.Add(input);
            }

            // 3. configure clock
            SamplesPerChannel = ClockRate / 5;
            result            = NidaQmxHelper.DAQmxCfgSampClkTiming(
                activeEdge:                 NidaQmxHelper.DaQmxValRising,
                sampleMode:                 NidaQmxHelper.DaQmxValContSamps,
                sampsPerChan:               (ulong)SamplesPerChannel,
                taskHandle:                 TaskHandle,
                source:                     "",
                rate:                       ClockRate
                );
            if (result < 0)
            {
                CleanupAndThrow(result);
            }

            State = SessionTaskState.Stopped;
        }
Example #11
0
        public void CreateTask(IEnumerable <INidaqMetric> nodes)
        {
            if (TaskHandle != 0)
            {
                throw new InvalidOperationException("Task already created. First destroy the old task or task already created");
            }

            if (!nodes.All(n => n is MetricDigitalInput))
            {
                throw new InvalidCastException("all passed nodes must be of type MetricDigitalInput");
            }

            if (!nodes.All(n => n.Channel.Device == Device))
            {
                throw new InvalidOperationException("not all passed nodes are connected to device " + Device.Name);
            }

            // 1. create task
            if (Source == ClockSource.Intern)
            {
                _counter = new NidaqCounterOutput(Device.Name + "/ctr0", ClockRate);
            }

            var taskHandle = new int[1];
            var result     = NidaQmxHelper.DAQmxCreateTask(null, taskHandle);

            if (result < 0)
            {
                throw new NidaqException(result);
            }
            TaskHandle = taskHandle[0];

            // 2. create channels
            foreach (var input in nodes.OfType <MetricDigitalInput>())
            {
                result = NidaQmxHelper.DAQmxCreateDIChan(
                    lines: input.Channel.Path,
                    taskHandle: TaskHandle,
                    lineGrouping: NidaQmxHelper.DAQmx_Val_ChanForAllLines,
                    nameToAssignToChannel: null
                    );
                if (result < 0)
                {
                    CleanupAndThrow(result);
                }
                _nodes.Add(input);
            }

            // 3. configure clock
            SamplesPerChannel = ClockRate / 10;
            result            = NidaQmxHelper.DAQmxCfgSampClkTiming(
                activeEdge: NidaQmxHelper.DaQmxValRising,
                sampleMode: NidaQmxHelper.DaQmxValContSamps,
                sampsPerChan: (ulong)SamplesPerChannel,
                taskHandle: TaskHandle,
                source: (this.Source == ClockSource.Intern) ? ("/" + Device.Name + "/Ctr0InternalOutput") : ClockPath,
                rate: ClockRate
                );
            if (result < 0)
            {
                CleanupAndThrow(result);
            }

            State = SessionTaskState.Stopped;
        }
        public void CreateTask(IEnumerable <INidaqMetric> nodes)
        {
            if (!nodes.All(n => n is MetricDigitalOutput))
            {
                throw new InvalidCastException("all passed nodes must be of type MetricDigitalOutput");
            }

            if (!nodes.All(n => n.Channel.Device == Device))
            {
                throw new InvalidOperationException("not all passed nodes are connected to device " + Device.Name);
            }

            ClockRate = ((MetricDigitalOutput)nodes.First()).Samplerate;

            if (!nodes.All(n => ((MetricDigitalOutput)n).Samplerate == ClockRate))
            {
                ClockRate         = 0;
                SamplesPerChannel = 0;
                throw new InvalidOperationException("NIDAQ: not all digital output nodes in graph have the same samplerate");
            }

            foreach (var n in nodes.OfType <MetricDigitalOutput>())
            {
                n.Samplerate = ClockRate;
            }

            var nidaqBufferSizePerChannel = (int)(BufferLengthMs * (long)ClockRate / 1000);
            var localBufferSizePerChannel = (int)(2 * (BufferLengthMs + PrebufferLengthMs) * (long)ClockRate / 1000);

            SamplesPerChannel = nidaqBufferSizePerChannel;

            // ------------------------------
            // Configure DAQMX Task

            _task = new NationalInstruments.DAQmx.Task();
            foreach (var output in nodes.OfType <MetricDigitalOutput>())
            {
                output.ChannelNumber = _nodes.Count;

                _task.DOChannels.CreateChannel(
                    output.Channel.Path,
                    string.Empty,
                    NationalInstruments.DAQmx.ChannelLineGrouping.OneChannelForEachLine
                    );

                _nodes.Add(output);
            }

            _task.Timing.ConfigureSampleClock(
                ClockPath,
                ClockRate,
                NationalInstruments.DAQmx.SampleClockActiveEdge.Rising,
                NationalInstruments.DAQmx.SampleQuantityMode.ContinuousSamples,
                SamplesPerChannel
                );

            try {
                _task.Stream.ConfigureOutputBuffer(SamplesPerChannel);
            } catch (NationalInstruments.DAQmx.DaqException e) {
                throw new NidaqException(e.Error);
            }

            _task.Stream.Timeout = WriteTimeoutMs;
            _task.Stream.WriteRegenerationMode = NationalInstruments.DAQmx.WriteRegenerationMode.DoNotAllowRegeneration;

            try {
                _task.Control(NationalInstruments.DAQmx.TaskAction.Verify);
            } catch (NationalInstruments.DAQmx.DaqException e) {
                throw new NidaqException(e.Error);
            }

            _task.Stream.Buffer.OutputBufferSize = SamplesPerChannel;

            _writer = new NationalInstruments.DAQmx.DigitalMultiChannelWriter(_task.Stream);

            TaskHandle = _task.GetHashCode();

            _usableData   = new int[_nodes.Count, SamplesPerChannel];
            _bufferData   = new int[_nodes.Count, localBufferSizePerChannel];
            _channelCount = new int[_nodes.Count];

            State  = SessionTaskState.Stopped;
            _first = true;
        }