DAQMX kártya példaprogram

A Fizipedia wikiből
CSharp DAQMX.png

A Form tartalma:

  • Button startButton;
  • Button readButton;
  • TextBox inputBox;

Megjegyzés: a kártya azonosítója ("DevXX") a MAX listájában ellenőrizhető. Az alábbi kódban a "Dev1" stringet át kell írni a megfelelő értékre, amennyiben szükséges.

Tutorial program for DAQMX data acquisition card

Contents of the Form:

  • Button startButton;
  • Button readButton;
  • TextBox inputBox;

Note: The identifier of the card ("DevXX") can be verified using MAX (Measurement and Automation Explorer). When using the following code, replace the "Dev1" string with the actual identifier of the connected device.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Add the following references manually to the header
using NationalInstruments.DAQmx; //Don't forget to add the references to the NI libraries in the project explorer as well
using System.IO;
namespace DAQMX
    public partial class Form1 : Form
        //Global variables
        private Task OutTask;    //kimeneti Task: Dev1/ao0
        private AnalogSingleChannelWriter writer;  
        private Task InTask;    //bemeneti Task: Dev1/ai0
        private AnalogSingleChannelReader reader;
        double Minimumvoltage = 0;
        double Maximumvoltage = 5;
        public Form1()
        private void Form1_Load(object sender, EventArgs e)
            //Read button is disabled when the program loads
	//The measurement has to be initialized first
	//For example by using a button press
        private void startButton_Click(object sender, System.EventArgs e)
			OutTask = new Task();  //Task constructor
			//Creating the output channel
			OutTask.AOChannels.CreateVoltageChannel("Dev1/ao0", "",
                    Minimumvoltage, Maximumvoltage,
			writer = new AnalogSingleChannelWriter(OutTask.Stream);
			InTask = new Task();  //Task constructor
			//Creating the input channel
			InTask.AIChannels.CreateVoltageChannel("Dev1/ai0", "",
                    AITerminalConfiguration.Differential, Minimumvoltage, Maximumvoltage,
			reader = new AnalogSingleChannelReader(InTask.Stream);
                        //The read button is enabled after the initialization
        private void readButton_Click(object sender, System.EventArgs e)
                        Double input = 0;
                        //The below function performs the writing and reading of the voltage values
                        //displaying the result
        //writing voltage (value stored in data variable)
        //and reading voltage on the input
	private Double write_And_Read()
			Double data=0.2;   //always between 0 and 5 V!
                        //the function returns the measured voltage
			return reader.ReadSingleSample();
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            //free up resources