Exemple #1
0
        private bool RunTask(InOutPortFlags flags)
        {
            if (flags.HasFlag(InOutPortFlags.UseInput))
            {
                var result = NidaQmxHelper.DAQmxStartTask(_device.InputTaskHandle);
                if (result < 0)
                {
                    NidaQmxHelper.DAQmxClearTask(_device.InputTaskHandle);
                    _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
                    return(false);
                }
            }

            //if (flags.HasFlag(InOutPortFlags.UseOutput)) {
            //    var result = NidaQmxHelper.DAQmxStartTask(_device.OutputTaskHandle);
            //    if (result < 0) {
            //        NidaQmxHelper.DAQmxStopTask(_device.InputTaskHandle);
            //        NidaQmxHelper.DAQmxClearTask(_device.InputTaskHandle);
            //        NidaQmxHelper.DAQmxClearTask(_device.OutputTaskHandle);
            //        _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
            //        return false;
            //    }
            //}

            return(true);
        }
Exemple #2
0
        public bool Start(NidaQmxDevice2 device)
        {
            if (Recording)
            {
                return(false);
            }

            _device = device;

            InOutPortFlags flags = 0;

            if (device.ListeningPorts.Any(p => p.Direction == ChannelDirection.Input))
            {
                flags |= InOutPortFlags.UseInput;
            }
            if (device.ListeningPorts.Any(p => p.Direction == ChannelDirection.Output))
            {
                flags |= InOutPortFlags.UseOutput;
            }

            if (!CreateVirtualChannel(flags))
            {
                return(false);
            }
            if (!ConfigureDevClock(flags))
            {
                return(false);
            }

            if (flags.HasFlag(InOutPortFlags.UseInput))
            {
                Data = Marshal.AllocHGlobal(sizeof(double) * device.SamplesPerChannelInput * device.ListeningPorts.Count());
                foreach (var port in device.ListeningPorts.OfType <NidaQmxChannelInput>())
                {
                    port.SetBufferSize(device.SamplesPerChannelInput);
                }
            }

            if (!RunTask(flags))
            {
                return(false);
            }

            _stopThread = false;
            if (flags.HasFlag(InOutPortFlags.UseInput))
            {
                _pollThread = new Thread(EveryNCallback);
                _pollThread.Start();
            }

            Recording = true;

            return(true);
        }
Exemple #3
0
        private bool ConfigureDevClock(InOutPortFlags flags)
        {
            if (flags.HasFlag(InOutPortFlags.UseInput))
            {
                var result = NidaQmxHelper.DAQmxCfgSampClkTiming(
                    activeEdge: NidaQmxHelper.DaQmxValRising,
                    sampleMode: NidaQmxHelper.DaQmxValContSamps,
                    sampsPerChan: (ulong)_device.SamplesPerChannelInput,
                    taskHandle: _device.InputTaskHandle,
                    source: "",
                    rate: _device.SamplerateInput
                    );

                if (result < 0)
                {
                    NidaQmxHelper.DAQmxClearTask(_device.InputTaskHandle);
                    _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
                    return(false);
                }
            }

            if (flags.HasFlag(InOutPortFlags.UseOutput))
            {
                var result = NidaQmxHelper.DAQmxCfgSampClkTiming(
                    activeEdge: NidaQmxHelper.DaQmxValRising,
                    sampleMode: NidaQmxHelper.DaQmxValContSamps,
                    sampsPerChan: (ulong)_device.SamplesPerChannelOutput,
                    taskHandle: _device.OutputTaskHandle,
                    source: "",
                    rate: _device.SamplerateOutput
                    );

                if (result < 0)
                {
                    NidaQmxHelper.DAQmxClearTask(_device.InputTaskHandle);
                    NidaQmxHelper.DAQmxClearTask(_device.OutputTaskHandle);
                    _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
                    return(false);
                }

                result = NidaQmxHelper.DAQmxCfgOutputBuffer(_device.OutputTaskHandle, _device.SamplesPerChannelOutput);
                if (result < 0)
                {
                    NidaQmxHelper.DAQmxClearTask(_device.InputTaskHandle);
                    NidaQmxHelper.DAQmxClearTask(_device.OutputTaskHandle);
                    _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
                    return(false);
                }
            }

            return(true);
        }
Exemple #4
0
        private bool CreateVirtualChannel(InOutPortFlags flags)
        {
            // ------------------------------------

            if (flags.HasFlag(InOutPortFlags.UseInput))
            {
                var inputs = _device.ListeningPorts.Where(port => port.Direction == ChannelDirection.Input)
                             .Select(port => port.Name);

                if (inputs.Any())
                {
                    var inputPortStrings = string.Join(",", inputs);

                    var result = NidaQmxHelper.DAQmxCreateAIVoltageChan(
                        maxVal:                 10.0,
                        minVal:                 -10.0,
                        units:                  NidaQmxHelper.DaQmxValVolts,
                        terminalConfig:         NidaQmxHelper.DaQmxValRse,
                        physicalChannel:        inputPortStrings,
                        taskHandle:             _device.InputTaskHandle,
                        nameToAssignToChannel:  "chanI" + _device.Name,
                        customScaleName:        null
                        );

                    if (result < 0)
                    {
                        NidaQmxHelper.DAQmxClearTask(_device.InputTaskHandle);
                        _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
                        return(false);
                    }
                }
            }

            // ------------------------------------

            if (flags.HasFlag(InOutPortFlags.UseOutput))
            {
                var outputs = _device.ListeningPorts.Where(port => port.Direction == ChannelDirection.Output)
                              .Select(port => port.Name);

                if (outputs.Any())
                {
                    var outputPortStrings = string.Join(",", outputs);

                    var channelNumber = 0;
                    foreach (var output in _device.ListeningPorts.Where(port => port.Direction == ChannelDirection.Output))
                    {
                        ((NidaQmxChannelOutput)output).ChannelNumber = channelNumber++;
                    }

                    var result = NidaQmxHelper.DAQmxCreateAOVoltageChan(
                        maxVal: 10.0,
                        minVal: -10.0,
                        units: NidaQmxHelper.DaQmxValVolts,
                        physicalChannel: outputPortStrings,
                        taskHandle: _device.OutputTaskHandle,
                        nameToAssignToChannel: "chanO" + _device.Name,
                        customScaleName: null
                        );

                    if (result < 0)
                    {
                        NidaQmxHelper.DAQmxClearTask(_device.OutputTaskHandle);
                        _device.ReportError(new DeviceErrorArgs(NidaQmxHelper.GetError(result)));
                        return(false);
                    }
                }
            }

            // ------------------------------------

            return(true);
        }