Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Asynchronously Writing Digital ports

Solved!
Go to solution

I am using PCI-6221 and DAQmx 8.7.2 and C# for programming

 

I want to write digital ports first, and after it completes, it will get a single sample analog input.

 

Here is my program. 

 

Can anyone point out the problem ? Because I used step-by-step debug, the AnalogSingleSampleRead() is not executed.

 

/////////////////////////////////////////////////////////////////// 

namespace NItest

{

 

public partial class Form1 : Form

{

//global variables

private double data;

private Task digitalWriteTask = new Task();

private Task AnalogTask = new Task(); private DigitalMultiChannelWriter ThreePortsWriter;

private AnalogSingleChannelReader myAnalogReader;

byte[,] DOutputss = new byte[3, 8];

 

public Form1()

{

   InitializeComponent();

   initNI();

}

   

private void initNI()

{

digitalWriteTask.DOChannels.CreateChannel(Port0, "", ChannelLineGrouping.OneChannelForAllLines);  

digitalWriteTask.DOChannels.CreateChannel(Port1,
"", ChannelLineGrouping.OneChannelForAllLines);

digitalWriteTask.DOChannels.CreateChannel(Port2, "", ChannelLineGrouping.OneChannelForAllLines);  

digitalWriteTask.Control(TaskAction.Verify);  

ThreePortsWriter = new DigitalMultiChannelWriter(digitalWriteTask.Stream);

digitalWriteTask.Start();

 

AnalogTask.AIChannels.CreateVoltageChannel("Dev1/ai0", "", AITerminalConfiguration.Rse, 0, 5, AIVoltageUnits.Volts);

AnalogTask.Control(TaskAction.Verify);   

myAnalogReader =
new AnalogSingleChannelReader(AnalogTask.Stream);

AnalogTask.Start();

ThreePortsWriter.SynchronizeCallbacks =
true;

}//end of iniNI

 

 private void DAQmxAsyncWrite()

{

   //[give some value to DOutputss]

 

   IAsyncResult handle=ThreePortsWriter.BeginWriteMultiSamplePort(false, DOutputss, new AsyncCallback(myCallback), null);

}

 

private void myCallback(IAsyncResult ar)

{

    ThreePortsWriter.EndWrite(ar);

    data = myAnalogReader.ReadSingleSample();

}

 

}

}

//////////////////////////////////////////////////////////////////// 

 

When I call DAQmxAsyncWrite(), the program never execute myCallback().

 

Why please?

 

Thank you all.

 

0 Kudos
Message 1 of 6
(4,906 Views)

Hi,

 

What version of measurement studio are you using?

 

Would you like to upload the rest of your program in a zip file please?

 

 Thanks,

Message Edited by Mark M on 12-04-2008 05:21 PM
Best regards,

Mark M.
Applications Engineer
National Instruments UK & Ireland
0 Kudos
Message 2 of 6
(4,888 Views)

Measurement studio version is 8.5

 

Here is my zip file.

 

Thanks very much.

 

 

 

0 Kudos
Message 3 of 6
(4,879 Views)

Hi timinmanchester,

 

Thanks for your post. I have noticed you've also post here: please keep to one post. 

 

Also, I beleive you have opened a service request at NI UK, and I will work with you via email to answer your question.

Kind Regards
James Hillman
Applications Engineer 2008 to 2009 National Instruments UK & Ireland
Loughborough University UK - 2006 to 2011
Remember Kudos those who help! 😉
0 Kudos
Message 4 of 6
(4,869 Views)
Solution
Accepted by topic author timinmanchester

Hi timinmanchester and All,

 

I hope your all well.

 

Here is the code which resolved the problem - please see attached. 

 

Here is some of the information I gave timinmanchester,

 

nt mT, mR;
mT=Convert.ToInt16(numericUpDown1.Value);
mR=Convert.ToInt16(numericUpDown2.Value);           
MatrixControl(mT,mR);
label1.Text = Convert.ToString(data);

 

DAQmxAsyncWrite() is never called, and as a result, nor is the callback myCallback(IAsyncResult ar).  If you insert the DAQmxAsyncWrite() function into the button1_Click(object sender, EventArgs e) calback so that the callback looks like this:

 

int mT, mR;     

mT=Convert.ToInt16(numericUpDown1.Value);     

mR=Convert.ToInt16(numericUpDown2.Value);     

MatrixControl(mT,mR);      

DAQmxAsyncWrite();     

label1.Text = Convert.ToString(data);

 

Then the Digital Write will take place, IAsyncResult will be regiestered, but it will not be handled by myCallback() until button1_Click()is finished executing.  This is how events work.  They get registered, and handled in the order that they occurred.  So since the button was clicked on first, it’s callback must finish executing before the IAsyncResult callback can begin executing.  For this reason, you will have some delay between when the digital write finishes and when the analog read begins.  A better implementation would move the label1.Text = Convert.ToString(data); line from the button1_Click() callback to the myCallback()callback, so that it looks like this:

 

ThreePortsWriter.EndWrite(ar);     

data = myAnalogReader.ReadSingleSample();     

label1.Text = Convert.ToString(data);

 

Which is quicker, since you have moved the line to execute after you perform the Analog Read.   To make sure that the Analog Read happens as soon as the callback event is registered, then you should re-arrange this callback so that it reads:

 

data = myAnalogReader.ReadSingleSample();     

ThreePortsWriter.EndWrite(ar);     

label1.Text = Convert.ToString(data);

 

This way the first line that executes in the callback is the analog read.  I have rebuilt what you sent me to reflect these changes.  Please let me know if you have any questions about this.  Hopefully this answers your question from the software perspective.

 

Kind Regards
James Hillman
Applications Engineer 2008 to 2009 National Instruments UK & Ireland
Loughborough University UK - 2006 to 2011
Remember Kudos those who help! 😉
Message 5 of 6
(4,816 Views)
Thanks very much
0 Kudos
Message 6 of 6
(4,799 Views)