private void EveryNCallback() { var dict = new Dictionary <INidaqSessionTask, NILoop.HandleInfo>(); foreach (var task in _tasks.OfType <NidaqSessionAnalogIn>().Where(t => t.Nodes.Count > 0)) { dict.Add( task, new NILoop.HandleInfo { handle = task.TaskHandle, buffer_size = task.SamplesPerChannel * task.Nodes.Count, samples_per_chan = task.SamplesPerChannel, type = (int)NILoop.TASK_TYPE.TASK_TYPE_ANALOG_INPUT, result = 0, mutex_buffers = IntPtr.Zero } ); } foreach (var task in _tasks.OfType <NidaqSessionDigitalIn>().Where(t => t.Nodes.Count > 0)) { dict.Add( task, new NILoop.HandleInfo { handle = task.TaskHandle, buffer_size = task.SamplesPerChannel * task.Nodes.Count, samples_per_chan = task.SamplesPerChannel, type = (int)NILoop.TASK_TYPE.TASK_TYPE_DIGITAL_INPUT, result = 0, mutex_buffers = IntPtr.Zero } ); } var hPoll = NILoop.start_polling(dict.Values.ToArray(), dict.Count); while (!_stopThread) { foreach (var task in dict) { if (task.Key is NidaqSessionAnalogIn) { ProcessInputData(task.Key.Nodes, task.Value, _inputAnalogDataBuffers[task.Key.Device], sizeof(double), hPoll); } else if (task.Key is NidaqSessionDigitalIn) { ProcessInputData(task.Key.Nodes, task.Value, _inputDigitalDataBuffers[task.Key.Device], sizeof(uint), hPoll); } } } NILoop.stop_polling(hPoll); }
private void ProcessInputData(IReadOnlyList <INidaqMetric> listeningPorts, NILoop.HandleInfo taskInfo, IntPtr buffer, int sample_size_bytes, int hPoll) { var result = NILoop.read_buffer(hPoll, taskInfo.handle, buffer, sample_size_bytes * (taskInfo.samples_per_chan * listeningPorts.Count)); switch (result) { case 3: case 1: // task not found SessionGraph.AsyncEmergencyStop(null); _stopThread = true; break; case 2: // queue empty, ignore Thread.Sleep(1); break; case 0: var channelData = buffer; foreach (var port in listeningPorts) { ((IMetricInput)port).DistributeData(channelData, taskInfo.samples_per_chan); channelData = IntPtr.Add(channelData, sample_size_bytes * taskInfo.samples_per_chan); } break; default: // read error System.Diagnostics.Debug.WriteLine(NidaQmxHelper.GetError(result)); SessionGraph.AsyncEmergencyStop(null); _stopThread = true; break; } }