12-03-2008 07:52 AM
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.
Solved! Go to Solution.
12-04-2008 11:12 AM - edited 12-04-2008 11:21 AM
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,
12-05-2008 04:41 AM
Measurement studio version is 8.5
Here is my zip file.
Thanks very much.
12-05-2008 07:57 AM
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.
12-12-2008 06:06 AM
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.
12-13-2008 12:50 PM