ni.com checkout is currently experiencing issues.

Support teams are actively working on the resolution.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make repeated calls to the reader in the AcqVoltageSamples_IntClkAnalogRef example code.

Solved!
Go to solution

I want to make repeated calls to the reader in the AcqVoltageSamples_IntClkAnalogRef example code.  A comment in the code says that I can do so by simply calling the reader.BeginReadMultiSample method from the Callback function. I tried this unsuccessfully using the same parameters for the reader. Can this be done from the callback function? If yes, how do I call the reader? I'm programming in C#.

 

Thanks,

 John

0 Kudos
Message 1 of 7
(4,202 Views)

John,

 

When you say you were unsuccessful what exactly do you mean, did you receive errors? Did it not compile? Or did the second acquisition just not happen?

 

Doug F

Applications Engineer

National Instruments

Doug Farrell
Solutions Marketing - Automotive
National Instruments

National Instruments Automotive Solutions
0 Kudos
Message 2 of 7
(4,189 Views)

Hi Doug,

 

I placed the example code in a method on a Windows form and launch the method using a button click.  If I supply a trigger pulse within a few seconds of clicking the button, all works well - the voltages are written to the data table. I can repeat the process ad infinitum with same good result.  If, after clicking , I wait 5 or 10 seconds before supplying the trigger pulse I get the following exception.message "Measurements: Reading relative to the reference trigger or to the start of pretrigger samples position before the acquistion is complete. This sounds like a timeout problem but I have yet to discover where to set the timeout to infinite (if that is indeed what is needed).  

 

The real world hardware that I'm supporting will send trigger pulses to the application at intervals which can vary from one every few seconds to intervals as long as several hours.  Any help that you can offer with the programming will be much appreciated.

 

Thanks,

John 

 

 

 

  private void AcquireData()

//this code creates all the objects necessary to acquire and store the data for one 'sweep'

//It creates the objects and waits for the trigger pulse before executing. References to relevant DAQmx classes

//were declared at the form level 

try

{

//create a new Task

slaveTask = new Task();

//Initialize local variables

 double sampleRate = Convert.ToDouble(this.cbxSampleRate.Text);

double minVolts = Convert.ToDouble(this.cbxMinVolts.Text);

double maxVolts = Convert.ToDouble(this.cbxMaxVolts.Text);

int samplesPerChannel = Convert.ToInt32(this.cbxSamplesPerChannel.Text);

//Create a channel

slaveTask.AIChannels.CreateVoltageChannel("Dev1/ai0", "",(AITerminalConfiguration)(-1), minVolts, maxVolts, AIVoltageUnits.Volts);

 

//Configure Timing Specs

slaveTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising,

SampleQuantityMode.FiniteSamples, samplesPerChannel);

//Configure reference trigger. We will trigger using a 1 volt level and will record 2 pre-trigger samples (the minimum)

slaveTask.Triggers.ReferenceTrigger.ConfigureAnalogEdgeTrigger("APFI0", AnalogEdgeReferenceTriggerSlope.Rising, 1.0, 2);

//Verify the Task

slaveTask.Control(TaskAction.Verify);

//Prepare the data structure for the data we will collect

//Prepare the table for Data

InitializeDataTable(slaveTask.AIChannels, ref dataTable);

acquisitionDataGrid.DataSource = dataTable;

 

//instantiate the data reader

reader = new AnalogMultiChannelReader(slaveTask.Stream);

reader.SynchronizeCallbacks = true;reader.BeginReadMultiSample(Convert.ToInt32(cbxSamplesPerChannel.Text), new AsyncCallback(slaveCallBack), null);

}

catch (DaqException exception)

{

MessageBox.Show(exception.Message);

slaveTask.Dispose();

}

 

}

0 Kudos
Message 3 of 7
(4,164 Views)

I believe the DAQmx read is timing out after the default 10 seconds when it does not see a trigger.  I would increase this timeout to whatever the maximum period before the trigger fires could be.

 

Doug

Applications Engineer

National Instruments

Doug Farrell
Solutions Marketing - Automotive
National Instruments

National Instruments Automotive Solutions
0 Kudos
Message 4 of 7
(4,148 Views)

Thanks again.  Where do I set the timeout? 

 

John

0 Kudos
Message 5 of 7
(4,141 Views)
Haha, sorry about that, check out this KnowledgeBase article.
Doug Farrell
Solutions Marketing - Automotive
National Instruments

National Instruments Automotive Solutions
0 Kudos
Message 6 of 7
(4,128 Views)
Solution
Accepted by topic author JohnTootle

Doug,

No problem.  I found which class  the timeout property belonged to without too much difficulty.  I now have a solution for the repeated calls.  The two keys were the infinite timeout setting and Disposing of all DAQmx objects in the finally section of the CallBack function before calling the AcquireData() method for the next round of reading and processing the data.  The user sets blQuit to false and invokes AcquireData() by clicking the Start button.  Repeated calls are then made 'automatically' until the user clicks the Quit button.

 

Thanks for your help,

John

 

 private void AcquireData()

//this code creates all the objects necessary to acquire and store the data for one 'sweep'

{

slaveTask = new Task();  double sampleRate = Convert.ToDouble(this.cbxSampleRate.Text);

double minVolts = Convert.ToDouble(this.cbxMinVolts.Text);

double maxVolts = Convert.ToDouble(this.cbxMaxVolts.Text); int samplesPerChannel = Convert.ToInt32(this.cbxSamplesPerChannel.Text); slaveTask.AIChannels.CreateVoltageChannel("Dev1/ai0", "",(AITerminalConfiguration)(-1), minVolts, maxVolts, AIVoltageUnits.Volts);

 

//Configure Timing Specs

slaveTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, samplesPerChannel);

//Configure reference trigger. We will trigger using a 1 volt level and will record 2 pre-trigger samples (the minimum)

slaveTask.Triggers.ReferenceTrigger.ConfigureAnalogEdgeTrigger("APFI0", AnalogEdgeReferenceTriggerSlope.Rising, 1.0, 2);

slaveTask.Stream.Timeout = -1;

// slaveTask.WaitUntilDone();

//Verify the Task

slaveTask.Control(TaskAction.Verify); InitializeDataTable(slaveTask.AIChannels, ref dataTable);

acquisitionDataGrid.DataSource = dataTable;

 

reader = new AnalogMultiChannelReader(slaveTask.Stream); reader.SynchronizeCallbacks = true;reader.BeginReadMultiSample(Convert.ToInt32(cbxSamplesPerChannel.Text), new AsyncCallback(slaveCallBack), null);

}

catch (DaqException exception)

{

MessageBox.Show(exception.Message);

slaveTask.Dispose();

}

 

}

private void slaveCallBack(IAsyncResult ar)

{

try

{

//read the available data from the channel

data = reader.EndReadMultiSample(ar);

dataToDataTable(data,
ref dataTable);

//plot the data here, if blKeep also write it to file

}

catch (DaqException exception)

{

MessageBox.Show(exception.Message);

}

finally

{

this.Refresh();

slaveTask.Dispose();

if (!blQuit)

{

AcquireData();

}

}

}

 

0 Kudos
Message 7 of 7
(4,122 Views)