private bool CreateTaskHandle() { int[] taskHandle = new int[1]; var result = NidaQmxHelper.DAQmxCreateTask(null, taskHandle); if (result < 0) { OnError?.Invoke(this, new DeviceErrorArgs("Could not create input nidaq task")); return(false); } InputTaskHandle = taskHandle[0]; result = NidaQmxHelper.DAQmxCreateTask(null, taskHandle); if (result < 0) { OnError?.Invoke(this, new DeviceErrorArgs("Could not create output nidaq task")); return(false); } OutputTaskHandle = taskHandle[0]; return(true); }
/// <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; }
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; }