Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

fast digital writes from C#

I'd like to do some fast digital writes to my DAQmx device's individual lines. By fast, I mean sub-millisecond timing per write.

Using NI's examples, it appears I have to do four things to write to a digital line:

1. Create a new "Task".
2. Create a digital output channel for that task.
3. Create a new "DigitalSingleChannelWriter" from the task's "Stream".
4. Call the "WriteSingleSampleSingleLine" method of that "DigitalSingleChannelWriter".

It takes 15 - 30 milliseconds to do all four of those, per digital write.  So I tried doing the first three when my C# form loads, then only calling the fourth one when I want the speedy digital write. I get the following error message when I try that:

An unhandled exception of type 'NationalInstruments.DAQmx.DaqException' occurred in NationalInstruments.DAQmx.dll

Additional information: Measurements: Task specified is invalid or does not exist.

Status Code: -200088


Attached is my entire project in a .zip file. Also, here is my source code which demonstrates the aforementioned error when
checkBox1_CheckedChanged() is called:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NationalInstruments.DAQmx;

namespace DigitalLineWriter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Task digitalWriteTask;
        private DigitalSingleChannelWriter digitalWriter;

        private void Form1_Load(object sender, EventArgs e)
        {
            using (digitalWriteTask = new Task())
            {
                digitalWriteTask.DOChannels.CreateChannel("dev3/port0/line23", "",
                    ChannelLineGrouping.OneChannelForEachLine);
                digitalWriter
                    = new DigitalSingleChannelWriter(digitalWriteTask.Stream);
                digitalWriter.WriteSingleSampleSingleLine(true, true);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            digitalWriter.WriteSingleSampleSingleLine(true, checkBox1.Checked);
        }

    }
}

 

Message 1 of 3
(4,350 Views)
Hi bmihura,

The reason you are getting that exception is because you are disposing the task object in the Form Load event handler and then calling the WriteSingleSampleSingleLine method. The problem is that the digitalWriter is connected to the Task object via its Stream property which you are setting:

digitalWriter = new DigitalSingleChannelWriter(digitalWriteTask.Stream);

So how are you disposing the task? Well, the using statement (i.e. using (digitalWriteTask = new Task()) is defining a scope for the Task object and thus this object will only exist inside of the using statement. Once outside, the task object will be disposed.  So, in your case, you should not be using the using stament. Instead, you will want to dispose the task yourself when you are completely done by calling Task.Dispose.

Best Regards,
Jonathan N.
National Instruments
Message 2 of 3
(4,349 Views)
Thanks. I should have seen that!
0 Kudos
Message 3 of 3
(4,250 Views)