So, currently I'm trying to re-write code that used to check in a timed loop into a ChangeDetection code. The focus of the code was always supposed to ensure the NI software was contained in its own library. Each instanciation is created through a static function and interfaced to another Library. I've been working on this for weeks. I had it working through a quick alteration, but not through the best practices technique I need.
After the instanciation, once the Task and the Reader is created and set, everything looks fine, when I peek at the object. As soon as the object leaves the instanciation, the Task and the reader nullify as if all I did was set the task to "new Task()".
Here is the instanciation:
public NniEventDigInputs(NgIoConfig config)
{
_Task = new Task();
bool first = true;
string address="";
foreach(var item in config.Inputs.Items)
{
_Channels.Add(item.Key, NniEventDigInChHw.Create(config.InputChannels.Device, config.InputChannels.PortIndex, item.ChannelIndex, ref _Task));
address+=first ? string.Format("{0}/port{1}/line{2}", config.InputChannels.Device,config.InputChannels.PortIndex,item.ChannelIndex.ToString()):
string.Format(",{0}/port{1}/line{2}", config.InputChannels.Device, config.InputChannels.PortIndex, item.ChannelIndex.ToString());
first = false;
}
_Task.DIChannels.CreateChannel(address, "", ChannelLineGrouping.OneChannelForAllLines);
_Task.Timing.ConfigureChangeDetection(address, address, SampleQuantityMode.ContinuousSamples, 1000);
_Task.SynchronizeCallbacks = true;
_Task.DigitalChangeDetection += new DigitalChangeDetectionEventHandler(OnHwChannelValueChanged);
_Reader = new DigitalSingleChannelReader(_Task.Stream);
foreach (var item in config.Inputs.Items)
{
_Channels[item.Key].SetReader(_Task, _Reader);
}
_Task.Start();
}
Here is the creation function:
public IEventDigInputs CreateEventDigInputs(NgIoConfig config)
{
return new NniEventDigInputs(config);
}
Here is the interface code for that function:
IEventDigInputs CreateEventDigInputs(NgIoConfig config);
I'm missing something easy I'm sure, but I can't figure it out.
The problem this creates, is when I try to detect the state of the input, like this:
public bool State
{
get
{
bool[] ret = _Reader.ReadSingleSampleMultiLine();
return ret[_Channel];
}
}
The task is empty and sends an NiDAQ exception.