From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
11-09-2015 01:52 PM
I want to write a simple If Then Else statement in order to do something if a specific Ai channel exists in the task. What is an easy way to do this?
I need to be able to find out what specific channels are in the task so that I can map its plotted data to the correct WaveformGraph on my form. And if the channel does not exist, do nothing (don't try to plot data to its waveform graph).
myTask = New DAQmx.Task 'check what checkboxes are checked and then create each Ai Channel accordingly Try 'Create Daq virtual channels If ch0RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai0", lblCh0Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch1RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai1", lblCh1Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch2RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai2", lblCh2Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch3RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai3", lblCh3Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch4RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai4", lblCh4Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch5RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai5", lblCh5Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch6RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai6", lblCh6Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch7RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai7", lblCh7Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch8RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai16", lblCh8Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch9RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai17", lblCh9Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch10RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai18", lblCh10Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch11RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai19", lblCh11Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch12RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai20", lblCh12Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch13RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai21", lblCh13Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch14RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai22", lblCh14Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If If ch15RecordData = True Then myTask.AIChannels.CreateVoltageChannel("cDAQ9184-1A4BA5FMod1/ai23", lblCh15Wfg.Text, AITerminalConfiguration.Differential, -10, 10, "Linear Scale") End If myTask.Stop() Catch ex As DaqException MessageBox.Show(ex.Message) End Try Try data = reader.ReadWaveform(readRate) 'Stream each channel's data to its specific Wafeformgraph. This code obviously will not work if the channel does not exist in the task. wfgCh0.PlotWaveformAppend(data(0)) : numEdtCh0.Value = data(0).Samples(readRate - 1).Value wfgCh1.PlotWaveformAppend(data(1)) : numEdtCh1.Value = data(1).Samples(readRate - 1).Value wfgCh2.PlotWaveformAppend(data(2)) : numEdtCh2.Value = data(2).Samples(readRate - 1).Value wfgCh3.PlotWaveformAppend(data(3)) : numEdtCh3.Value = data(3).Samples(readRate - 1).Value wfgCh4.PlotWaveformAppend(data(4)) : numEdtCh4.Value = data(4).Samples(readRate - 1).Value wfgCh5.PlotWaveformAppend(data(5)) : numEdtCh5.Value = data(5).Samples(readRate - 1).Value wfgCh6.PlotWaveformAppend(data(6)) : numEdtCh6.Value = data(6).Samples(readRate - 1).Value wfgCh7.PlotWaveformAppend(data(7)) : numEdtCh7.Value = data(7).Samples(readRate - 1).Value wfgCh8.PlotWaveformAppend(data(8)) : numEdtCh8.Value = data(8).Samples(readRate - 1).Value wfgCh9.PlotWaveformAppend(data(9)) : numEdtCh9.Value = data(9).Samples(readRate - 1).Value wfgCh10.PlotWaveformAppend(data(10)) : numEdtCh10.Value = data(10).Samples(readRate - 1).Value wfgCh11.PlotWaveformAppend(data(11)) : numEdtCh11.Value = data(11).Samples(readRate - 1).Value wfgCh12.PlotWaveformAppend(data(12)) : numEdtCh12.Value = data(12).Samples(readRate - 1).Value wfgCh13.PlotWaveformAppend(data(13)) : numEdtCh13.Value = data(13).Samples(readRate - 1).Value wfgCh14.PlotWaveformAppend(data(14)) : numEdtCh14.Value = data(14).Samples(readRate - 1).Value wfgCh15.PlotWaveformAppend(data(15)) : numEdtCh15.Value = data(15).Samples(readRate - 1).Value Catch ex As DaqException MessageBox.Show(ex.Message) reader = Nothing data = Nothing swScopeOnOff.Value = False Return End Try
11-10-2015 06:50 PM
Hi,
Were you able to make any progress on this?
One option might be to use channel expansion to create the virtual channels, and then use a for loop to go through an array of the channels rather than hardcoding all the variables individually.
Regards,
11-11-2015 01:12 PM
Yes, I found that out. Thank you. All is good now.