Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing and reading an analog output/input

Hello everyone!

 

I want to create a code that will write to an analog output when you press the "write data" button and will read an analog input when you press the "read data" button.

The analog output was supposed to be connected to the analog input but because I'm trying to simulate it using the NI Max Simulated Device I'm not able do to it. So whenever i try to read the analog output I get the value of 2.46253852961821 V.

 

Is this because I'm using the simulated device?

When I do connect it to the will my code work?

 

Here's the code:

private void button1_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 = double.Parse(textBox1.Text);

            writer.WriteSingleSample(true, analogDataOut);
        }

private void button2_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 data = reader.ReadSingleSample();
            textBox2.Text = Convert.ToString(data);
        }

I appreciate any help. The device model is a USB-6351.

0 Kudos
Message 1 of 4
(2,884 Views)

In regard to the value you are seeing currently, do you get that value consistently? When I simulate an input on a USB-6351 I get a simulated sine wave, not a constant analog signal.  

0 Kudos
Message 2 of 4
(2,844 Views)

Thanks for answering, Titen.

 

Yes. I always get the same value of 2,462538...

Do you think it's possible I'm reading the analog input at the same point of the sine wave everytime?

 

When I change the minimum and maximum value for 0 to 10V the value change for 4,92507705923643.

0 Kudos
Message 3 of 4
(2,841 Views)

Sorry. I couldn't edit my post above.

 

I tested it on the card and it worked.

Message 4 of 4
(2,834 Views)