티스토리 뷰

목차

    반응형

    NI DAQ 프로그램 (C# 프로그래밍, NI USB-6008 DAQ)



    이 글은 NI에서 공식 발행한 C# 메뉴얼에서 발췌한 내용입니다. 전체 내용은 링크된 pdf 파일에서 확인하세요.


    NI USB-6008 DAQ[C# 프로그래밍] 카메라 제어


    3. My First DAQ App

    We will create a simple application in Visual Studio that uses a NI USB-6008 DAQ device.


    3.1 Introduction

    This application uses the C# API included in the NI DAQmx driver, so make sure that you have installed the NI DAQmx driver in advance. You don’t need Measurement Studio to create this application.


    Start Visual Studio and create a new Windows Forms application. We will create the following application in Visual Studio 2010 and C#:


    ni daq c# 프로그래밍[C# 프로그래밍] 카메라 제어

    The User Interface looks like this:


    ni daq 예제 소스 코드[C# 프로그래밍] 카메라 제어


    We start by connecting the Analog In and Analog Out wires together (a so called Loopback test).


    When we click the “Write Data” button, the value entered in the text box “Analog Out” will be written to the DAQ device. If we have connected the Analog In and Analog Out wires together we will read the same value in the “Analog In” textbox when we push the “Get Data” button.


    3.2 Source Code

    We will go through the different parts of the code in detail.


    References:

    This application uses the C# API included in the NI DAQmx driver, so make sure that you have installed the NI DAQmx driver in advance.


    We need to add the following Assembly references:

    - NationalInstruments.Common

    - NationalInstruments.DAQmx


    We add References by right-clicking the References node in the Solution Explorer:


    visual studio ni daq 프로그래밍[C# 프로그래밍] 카메라 제어


    When we have added the necessary References, the Solution Explorer will look like this:


    c# 프로그래밍 ni daq[C# 프로그래밍] 카메라 제어


    Initialization:

    We need to add the following Namespaces:


    1
    2
    using NationalInstruments;
    using NationalInstruments.DAQmx;
    cs


    Analog Out:

    NI DAQ : We implement the code for writing to the Analog Out channel in the Event Handler for the “Write Data” button:


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    private void btnWriteAnalogOut_Click(object sender, EventArgs e)
    {
        Task analogOutTask = new Task();
        AOChannel myAOChannel;
        myAOChannel = analogOutTask.AOChannels.CreateVoltageChannel (
            "dev1/ao0",
            "myAOChannel",
            0,
            5,
            AOVoltageUnits.Volts
        );
     
        AnalogSingleChannelWriter writer = new
        AnalogSingleChannelWriter(analogOutTask.Stream);
     
        double analogDataOut;
     
        analogDataOut = Convert.ToDouble(txtAnalogOut.Text);
        writer.WriteSingleSample(true, analogDataOut);
    }
    cs


    Analog In:

    We implement the code for reading data from the Analog In channel in the Event Handler for the “Get Data” button:


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    private void btnGetAnalogIn_Click(object sender, EventArgs e)
    {
        Task analogInTask = new Task();
        AIChannel myAIChannel;
        myAIChannel = analogInTask.AIChannels.CreateVoltageChannel(
            "dev1/ai0",
            "myAIChannel",
            AITerminalConfiguration.Differential,
            0,
            5,
            AIVoltageUnits.Volts
        );
     
        AnalogSingleChannelReader reader = new
        AnalogSingleChannelReader(analogInTask.Stream);
     
        double analogDataIn = reader.ReadSingleSample();
        txtAnalogIn.Text = analogDataIn.ToString();
    }
    cs


    NI DAQ 프로그램 (C# 프로그래밍, NI USB-6008 DAQ)

    반응형