09-22-2008 11:35 AM
I'm doing a triggered read on some analog inputs. Periodically, I have to terminate and restart the task because i'm changing the trigger line between "up" and "down" (pfi8 and pfi9).
Here's the code that (re)sets up the read:
_psdTask.Timing.ConfigureSampleClock(string.Empty, SAMPLE_RATE,if (null != _psdTask)
{
_psdTask.Control(TaskAction.Abort);_psdTask.Dispose();
}
_psdTask = new Task();//Create a virtual channel
_psdTask.AIChannels.CreateVoltageChannel(GetSampleLines(), string.Empty,AITerminalConfiguration.Differential, -10.0, 10.0, AIVoltageUnits.Volts);
//Configure the timing parameters & set it to retrieve exactly N samples
SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, SAMPLES_PER_CHANNEL);
switch (_trigger){
case PSDTriggerSource.Continuous:_psdTask.Triggers.StartTrigger.ConfigureNone();
_psdTask.Triggers.StartTrigger.Delay = 1.0 / SAMPLES_PER_SECOND_POLLING;
_psdTask.Triggers.StartTrigger.DelayUnits = StartTriggerDelayUnits.Seconds;break;
case PSDTriggerSource.DownSensor:_psdTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(GetDownTriggerLine(),
DigitalEdgeStartTriggerEdge.Rising);break;
case PSDTriggerSource.UpSensor:_psdTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger(GetUpTriggerLine(),
DigitalEdgeStartTriggerEdge.Rising); break;}
//Verify the Task
_psdTask.Control(TaskAction.Verify);// make it never time out
_psdTask.Stream.Timeout = -1; // This is the line with the null reference
The last line sometimes throws a NullReferenceException, and the stack trace ends in my code. I'm assuming the Stream property returns NULL.
Any idea what can cause this (or how to work around it)? I was expecting that Verify() would have reported any issues, but it never throws an exception.
Thanks!
John Duddy
09-25-2008 06:14 PM
Hello,
Please do some more probing. Lets setup a breakpoint prior to the _psdTask.Stream.Timeout line and run the code. When the code stops due to the breakpoint, navigate back to the code and find _psdTask. Right click and Add Watch. Please check if _psdTask is null and then expand out to check if Stream or Timeout is null. Just looking for more insight.
09-26-2008 10:43 AM
Many thanks for the reply.
Because the NullReferenceException happens only when I access the Stream, and because I access members of the task, and because new() cannot fail in C# without throwing an OutOfMemoryException, I think it's safe to assume that the task is not null.
I can set a conditional breakpoint that stops if Stream is null. As I mentioned, it is very intermittent. It may take me some days to get this done, since the equipment is under heavy use at the moment - I'll keep you posted.