Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

USB-6501 continuously toggling T/F to a single channel in a C# program

Hi, I am pretty green at NI DAQ-MX development.  I have inherited a simple labview project that continuously toggles a channel on a USB-6501 device to T/F for 5,000 cycles with a configurable number of sec pause between each set that I have converted to a C# WinForm project.  I am doing the toggling of the channel in a BackGroundWorker's DoWork event.  Occasionally the NationalInstruments.DAQmx.Task object throws an Exception:

 

Task Name: _unnamedTask<485>

Status Code: -50405

 

What should I do in this instance?  From a programming perspective should I be handling the Exception and trying to set up to write to the channel again?  Currently, I am just throwing the exception because I am not sure.  It can take days of testing for the exception to happen, but it never occured with the labview project..

 

Also I would to confirm I am doing the best practice of continuously creating a new DAQmx.Task and DigitalSingleChannelWriter objects for each write, or should I simply be reusing them?  A single run of my software runs for 12-14 hrs and the exception is only thrown every 3-4 runs.

 

Here is a stripped-down version of the programs my DoWork event

 

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)

{
const bool RETRACT = false;
const bool INSERT = true;

var jobSettings = e.Argument as JobSettings;

DigitalSingleChannelWriter writer = null;
Task digitalOutputTask = null;

for (int i = 0; i < jobSettings.TotalCycles; i++) {

mBusy.WaitOne();
if (worker.CancellationPending) {
e.Cancel = true;
return;
}

try {
digitalOutputTask = new Task();
digitalOutputTask.DOChannels.CreateChannel($"{jobSettings.NIDevName}/port1/line3","DigitalOutput",ChannelLineGrouping.OneChannelForEachLine);

writer = new DigitalSingleChannelWriter(digitalOutputTask.Stream);
writer?.WriteSingleSampleSingleLine(true, RETRACT);
}
catch(Exception ex){
System.Diagnostics.Debug.WriteLine("Exception occured WHEN trying to Rectract: " + ex.Message);
throw;
}

finally {
digitalOutputTask.Dispose();
digitalOutputTask = null;
}

//report status & worker progress

//wait x seconds
Thread.Sleep(Convert.ToInt32(jobSettings.SecondsToRetract * 1000));

//report status & worker progress

try {
digitalOutputTask = new Task();
digitalOutputTask.DOChannels.CreateChannel($"{jobSettings.NIDevName}/port1/line3","DigitalOutput",ChannelLineGrouping.OneChannelForEachLine);

writer = new DigitalSingleChannelWriter(digitalOutputTask.Stream);
writer?.WriteSingleSampleSingleLine(true, INSERT);
}
catch(Exception ex){
System.Diagnostics.Debug.WriteLine("Exception occured WHEN trying to INSERT: " + ex.Message);
throw;
}
finally {
digitalOutputTask.Dispose();
digitalOutputTask = null;
}

//report status & worker progress

if (worker.CancellationPending) {
e.Cancel = true;
return;
}

//wait x seconds to insert
Thread.Sleep(Convert.ToInt32(jobSettings.SecondsToInsert * 1000));


//report status & worker progress

} //for loop

} //DoWork event

 

 

 

 

0 Kudos
Message 1 of 1
(171 Views)