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.
08-06-2013 12:33 PM
I inherited a program that intensively tests a hardware device. One of the tests turns each AI channel on and off multiple times and reads the waveform of the AI channel. I am using a DAQ PXI 6229 card with C# .NET. The program is hanging in the while loop waiting for the task to complete. This is the code:
public bool TestThisChannel(object nullObject) {
double coversionFactor;
string Name;
string Description;
_PXI6229_SLOT12.getChannelInfo((int)nullObject,out Name,out coversionFactor,out Description);
_PXI6229_SLOT12.StartAnalogInput6s(Name);
for (int i = 0; i < 5; i++) { //5 times
_PXIChassis.SetPowerRelayOn(null);
_PXIChassis.Wait500ms(null);
_PXIChassis.SetPowerRelayOff(null);
_PXIChassis.Wait500ms(null);
}
while (_PXI6229_SLOT12.runningTask!=null) {
} //Wait for task to complete
private Task AnalogMultipleInputTask;
public Task runningTask;
private AsyncCallback analogCallback;
private AnalogMultiChannelReader analogInReader;
public AnalogWaveform<double>[] data;
public void StartAnalogInput6s(string AIChannel)
{
// Create a new task
try {
AnalogMultipleInputTask = new Task();
// Create a virtual channel
AnalogMultipleInputTask.AIChannels.CreateVoltageChannel(AIChannel, "",
(AITerminalConfiguration)(-1), -10 , 10, AIVoltageUnits.Volts);
// Configure the timing parameters
AnalogMultipleInputTask.Timing.ConfigureSampleClock("", 100000, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 600000);
// Verify the Task
AnalogMultipleInputTask.Control(TaskAction.Verify);
// Prepare the table for Data
runningTask = AnalogMultipleInputTask;
analogInReader = new AnalogMultiChannelReader(AnalogMultipleInputTask.Stream);
analogCallback = new AsyncCallback(EndAnalogInput);
// For .NET Framework 2.0 or later, use SynchronizeCallbacks to specify that the object
// marshals callbacks across threads appropriately.
analogInReader.SynchronizeCallbacks = true;
analogInReader.BeginReadWaveform(600000, analogCallback, AnalogMultipleInputTask);
} catch (Exception exception) {
MessageBox.Show(exception.Message);
runningTask = null;
AnalogMultipleInputTask.Dispose();
}
}
public void EndAnalogInput(IAsyncResult ar)
{
try
{
if (runningTask == ar.AsyncState)
{
// Read the available data from the channels
data = analogInReader.EndReadWaveform(ar);
}
}
catch(DaqException exception)
{
//Display Errors
MessageBox.Show(exception.Message);
runningTask = null;
AnalogMultipleInputTask.Dispose();
}
}
Any help will be appreciated.
Thanks,
Howard
Solved! Go to Solution.
08-07-2013 05:28 PM
Hello Howard,
You said that you inherited this code--did it work for the previous owner? Has it worked for you at all?
08-07-2013 06:22 PM
Hi Mason,
Some of the code is working. This code appears to be working in the sense that there are no error messages being produced. In order to test where it is hanging I have previously placed a print statement in the while loop that is waiting for the task to end and it is definitely in an infinite loop waiting for this to happen. Is this the correct method to wait for the end of the task? I am a novice as far as this type of programming goes.
Thanks,
Howard
08-08-2013 06:47 PM
Howard,
Where is the object _PXI6229_SLOT12 instantiated? When was this code written? I would recommend looking at the NI examples for DAQmx programming in C#. These are located in Start » All Programs » National Instruments » NI-DAQ » Text-Based Code Support. You might try to see if you can get one of the examples to run with your hardware to make sure you are communicating with it properly.
08-09-2013 08:56 AM
There is no problem with communication. This is just one of many tests that are performed on the device. The previous tests I have run are producing correct results. The problem I am having with this particular section of code is that the task never seems to end. The while (while (_PXI6229_SLOT12.runningTask!=null) { }) is in an infinite loop. Is there a better way to handle checking for completion of the task?
08-09-2013 11:13 AM
I have also tried the following: while (!_PXI6229_SLOT12.runningTask.IsDone) { } and _PXI6229_SLOT12.runningTask.WaitUntilDone() with the same results.
08-09-2013 02:51 PM
Something else I have found is that the callback is not being executed. What would stop the callback from being executed?
08-12-2013 06:40 PM
You're waiting for runningTask to become null, but in your "StartAnalogInput6s" function the only place that runningTask is assigned null is in the try catch. Therefore the task will not stop running.
you need some logic like this:
while (_PXI6229_SLOT12.runningTask!=null && !complete)
where "complete" is a boolean that gets set to TRUE at the end of "StartAnalogInput6s". This way if you hit an exception (runningTask==null) OR if the task finishes you will exit the loop.
08-13-2013 11:43 AM
Thanks Mason. You got me thinking on the right path. I made some changes based on an example I found from NI. I changed the analogInReader.BeginReadWaveform(..........) to data = analogInReader.ReadWaveform(600000) and I changed the while (_PXI6229_SLOT12.runningTask!=null) { } to _PXI6229_SLOT12.runningTask.WaitUntilDone() and this appears to be working correctly.
Thanks for all your help.
Howard
08-13-2013 11:46 AM
No problem, glad I could help out!