Ejemplo n.º 1
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;
        }