MyDAQ

A Fizipedia wikiből

Example: programming the NI myDAQ data acquisition card using C# language.

Download: myDAQ_prog.zip

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NationalInstruments.DAQmx;
 
namespace myDAQ
{
    public partial class Form1 : Form
    {
        private NationalInstruments.DAQmx.Task  InTask;
        private AnalogSingleChannelReader analogReader;
 
        private NationalInstruments.DAQmx.Task OutTask;
        private AnalogSingleChannelWriter analogWriter;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // Set up DAQ Card Input & Output
            InTask = new NationalInstruments.DAQmx.Task();
            InTask.AIChannels.CreateVoltageChannel("myDAQ1/ai0", "",
                AITerminalConfiguration.Differential, -10, 10, AIVoltageUnits.Volts);
            analogReader = new AnalogSingleChannelReader(InTask.Stream);
 
            OutTask = new NationalInstruments.DAQmx.Task();
            OutTask.AOChannels.CreateVoltageChannel("myDAQ1/ao0", "",
                -10, 10, AOVoltageUnits.Volts);
            analogWriter = new AnalogSingleChannelWriter(OutTask.Stream);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // Read from Input
            InputextBox.Text = Convert.ToString(analogReader.ReadSingleSample());
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            // Write to Output
            analogWriter.WriteSingleSample(true, Convert.ToDouble(OutputtextBox.Text));
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            InTask.Dispose();
            OutTask.Dispose();
        }
 
    }
}
  • Try this fix, when there is a problem with the different versions of .NET: Paste the following lines into the app.config file:
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>