Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

DAQmx Task Sample Complete Event not received

Solved!
Go to solution
I am using Visual Studio 2005 (in C#).  I'm trying to use the SampleCompleteEventHandler as my callback function when acquiring data.  Here's basically what I have coded.

private void startButton_Click(object sender, System.EventArgs e)
{
    try
    {
        //Create a new task
        myTask = new Task();

        //Create a virtual channel
        myTask.AIChannels.CreateVoltageChannel(physicalChannelComboBox.Text, "",
            (AITerminalConfiguration)(-1), Convert.ToDouble(minimumValueNumeric.Value),
            Convert.ToDouble(maximumValueNumeric.Value), AIVoltageUnits.Volts);

        //Configure the timing parameters
        myTask.Timing.ConfigureSampleClock("", Convert.ToDouble(rateNumeric.Value),
            SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 1000);

        //Verify the Task
        myTask.Control(TaskAction.Verify);

        //Prepare the table for Data
        InitializeDataTable(myTask.AIChannels,ref dataTable);
        acquisitionDataGrid.DataSource=dataTable;

        // Create the event
        myTask.SampleComplete += new SampleCompleteEventHandler(myTask_SampleComplete);

        // Start the acquisition
        myTask.Start();
    }
    catch (DaqException exception)
    {
        //Display Errors
        MessageBox.Show(exception.Message);
        myTask.Dispose();
    }         
}

void myTask_SampleComplete(object sender, SampleCompleteEventArgs e)
{
    // Read Data
}


I never get into the myTask_SampleComplete function.  I know it's acquiring data, because if I change it to finite sampling instead of continuous and wait for it to be done, it finishes.  It still never fires the event to call the callback function though.

Is there something I'm doing wrong?

0 Kudos
Message 1 of 11
(7,683 Views)

Hi rnigro,

There may be something incorrect with the way the SampleComplete event is being used.  From what I understand from the code, you want an Analog Input task to read continuously.  There are several shipping examples that come with Measurement Studio that you may want to try.  They are located in the Measurement Studio folder under DotNET»Examples»DAQmx»Analog In»Measure VoltageI didn't see anything in your code that you would not be able to do with the shipping example.  Is there something that you plan to include where you would need to use the SampleCompleteEventHandler?



Best Regards

Hani R.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 11
(7,666 Views)
I have nothing in my code that requires the use of SampleCompleteEventHandler.  I prefer to use it for a couple of reasons.

1) To me it's the most straight forward.  You setup the event handler up initially, and from then on whenever more data is requested, the task can be setup and when the data becomes available it is caught by the event handler and processed.

2) The examples seem to include a band-aid workaround for .Net 2.0 to use ASyncCallback.  I prefer to not include workarounds in a brand new application if I can prevent it.

It just seems odd that the event handler is included in the assembly, but can't be used.  Or it can't be used in the manner that is normal coding style for events.
0 Kudos
Message 3 of 11
(7,636 Views)

It appears that SamplesComplete occurs when each sample is acquired into the NI-DAQmx driver buffer and is ready for reading. If your sampling rate is too high, you’re going to run into problems.

You probably want to use “Every N Samples”, but it isn’t supported in Measurement Studio NI-DAQmx .NET library. Instead, you would use asynchronous reading, which is what you refer to as a “band-aid”. All of this can be found starting in the “Using NI-DAQmx Events” in the Microsoft Visual Studio 2005 Documentation.

I found other related information in the paragraph “Asynchronous I/O versus Events”, as well as by following the last link.

Mark E.
Precision DC Product Support Engineer
National Instruments

0 Kudos
Message 4 of 11
(7,623 Views)
I believe my sample rate will be well under control.  I actually plan on using SampleQuantityMode.FiniteSamples and waiting in between samples.  This doesn't have any effect on the problem I'm seeing however.

If I were to replace the "// Read Data" line from my orginal post with "MessageBox.Show("Read Data");" and run the program, the message box would never appear.

This is irrespective of what the sample rate is, or whether it's continuous samples or finite samples.  The event is never fired.
0 Kudos
Message 5 of 11
(7,606 Views)

To make sure that your hardware is working and you get valid data, I recommend running a test panel in Measurement & Automation Explorer. This will verify that you have a valid sample clock and AI channel. If that works, then run the example I’ve written that shows that the event is firing. (You might want to turn down the rate because the box could get annoying.)

Hope this helps.

Mark E.
Precision DC Product Support Engineer
National Instruments

0 Kudos
Message 6 of 11
(7,580 Views)
I know my hardware is working because I have no hardware... I setup a simulated device (M-Series PCI-6259).  I ran the test panel, and that worked fine.  I ran your example program, and put a breakpoint on the message box line in the event, and it never reached that line.  Is it because I'm using a simulated device and not the real thing?  I thought the simulated device was suppose to mimic the card exactly. 

I'll have to try it with the real hardware when the equipment is assembled.  In the meantime it looks like I'm stuck using the AsyncCallback method.
0 Kudos
Message 7 of 11
(7,557 Views)
Solution
Accepted by rnigro

Ah yes!

Simulated devices are very similar to real hardware, but do have some considerations when it comes to timing and synchronization. There’s a Developer Zone article entitled “NI-DAQmx Simulated Devices” that describes these considerations in section 4. One of the points is that “An NI-DAQmx simulated device never issues a software event or causes a timed loop to execute.”

I hope that clarifies the behavior you are experiencing. With your actual hardware, everything should work fine with firing the event.

Mark E.
Precision DC Product Support Engineer
National Instruments

Message 8 of 11
(7,533 Views)
Man is my face red!  Thanks for the clarification, and all the time you spent helping me.
0 Kudos
Message 9 of 11
(7,529 Views)
Hi Hani, I am new to application development using Ni DAQ. I want to develop an application which will read the data from the buffer of the channels. I founded one way of doing the same i.e. through SampleCompleteEventHandler handler. But I have founded that all the Ni DAQ devices does not support this event handler. Can you suggest any other way to achieve the same objective? Thanks, Ajay
0 Kudos
Message 10 of 11
(6,634 Views)