53
Using USB-6008 in Visual Studio/C# Hans-Petter Halvorsen

Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

  • Upload
    others

  • View
    29

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Using USB-6008in Visual Studio/C#

Hans-Petter Halvorsen

Page 2: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Contents

• Visual Studio/C#• What is DAQ?• Using USB-6008 in C#• Analog In• Analog Out• Using Timer, Charts, etc.

USB-6008 I/O Module

Page 3: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Software

• Visual Studio/C#

• DAQmx Driver

• DAQmx Driver can be downloaded for free from Internet

Page 4: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

DAQ

Hans-Petter Halvorsen, M.Sc.

Data Acquisition

Page 5: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

DAQ – Data Acquisition

A DAQ System consists of 4 parts:1. Physical input/output signals, sensors2. DAQ device/hardware3. Driver software4. Your software application (Application software)

NI DAQmx Driver

Your App created with C#

NI USB 6008 DAQ Device

Sensors, etc.

Page 6: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

AD Converter

DA Converter

Measurement Signal

Control SignalUSB

AD & DA Converters

AD – Analog to Digital DA – Digital to Analog

All Analog Signals needs to be converted to Digital Signals before the Computer can use them (AD Converter).

Page 7: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Continuous Signal

Discrete Signal A computer can only deal with discrete signals

k = 0, 1, 2, 3, 4, ....

Ts = Sampling Time

When Ts -> 0, we have a continuous signal, but in a computer that is not possible.

Continuousvs.

DiscreteSignals

Page 8: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Sampling and AliasingOriginal Signal

Aliasing (“Nedfolding”) -> The Sampling Rate is to low!

Sampling Time

Sampling Frequency

Page 9: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

USB-6008

Hans-Petter Halvorsen, M.Sc.

Page 10: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

How-To use USB-6008 with Visual Studio

USB

Analog I/O

Digital I/O

USB-6008

PC with Visual Studio

Page 11: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

NI USB-6008 I/O Module

Specifications:• 8 analog inputs, AI (12-bit, 10 kS/s, -10-10V)• 2 analog outputs, AO (12-bit, 150 S/s, 0-5V)• 12 digital I/O (DI/DO)• 32-bit counter

Note! DAQmx Driver is needed!!

4 different types of Signals:AO – Analog OutputAI – Analog InputDO – Digital OutputDI – Digital Input

USB Connection -10-10V

0-5V

Page 12: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

DAQmx Driver

Hans-Petter Halvorsen, M.Sc.

Page 13: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

NI DAQmx Driver• National Instruments provides a native .NET API for NI-

DAQmx. This is available as a part of the NI-DAQmx driver

• In order to install the DAQmx API for C#, make sure to select “Custom” and then “.NET Support” when installing the DAQmx driver.

• Next, make sure that you select .NET Framework X.xSupport for the version of .NET that your version of Visual Studio is using.

Page 14: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

NI DAQmx Driver Installation

Page 15: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

MAX – Measurement & Automation Explorer

NI USB-6008 “Dev1”

You may change the name (“Dev1”)

Page 16: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Analog In

Hans-Petter Halvorsen, M.Sc.

C# Examples

Page 17: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Read Analog Values

We will read the voltage values on different batteries

Page 18: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Read Analog Signals with USB-6008Multimeter

USB

9V

9V

Page 19: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

A 9V Battery is connected to Analog In on USB-6008

Page 20: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Read from USB-6008 DAQ Device

Page 21: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Simple DAQ in C# with DAQmxusing NationalInstruments.DAQmx;…

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("0.00");

Analog In Example

Page 22: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

using NationalInstruments.DAQmx;…

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("0.00");

Page 23: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

WinForm App

Page 24: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Add Assembly References

Page 25: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Add References to the DAQmx Driver in Visual Studio

using NationalInstruments.DAQmx;

We also need to add the following Namespaces: NationalInstruments.DAQmx.dll

Select «Browse» and FindNationalInstruments.DAQmx.dll

C:\Program Files (x86)\National Instruments\...

Page 26: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 27: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

using System;using System.Windows.Forms;using NationalInstruments.DAQmx;

namespace DAQRead{

public partial class Form1 : Form{

public Form1(){

InitializeComponent();}

private void btnRead_Click(object sender, EventArgs e){

Task analogInTask = new Task();

AIChannel myAIChannel;

myAIChannel = analogInTask.AIChannels.CreateVoltageChannel("dev1/ai0","myAIChannel",AITerminalConfiguration.Differential,0,10,AIVoltageUnits.Volts);

AnalogSingleChannelReader reader = new AnalogSingleChannelReader(analogInTask.Stream);

double analogDataIn = reader.ReadSingleSample();

txtDaqValue.Text = analogDataIn.ToString("0.00");

}}

}

Page 28: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 29: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Analog Out

Hans-Petter Halvorsen, M.Sc.

C# Examples

Page 30: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Write to USB-6008 DAQ Device

Page 31: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Simple DAQ in C# with DAQmxusing NationalInstruments.DAQmx;…

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);

Analog Out Example

Page 32: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

using NationalInstruments.DAQmx;…

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);

Page 33: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Add References to the DAQmx Driver in Visual Studio

using NationalInstruments.DAQmx;

We also need to add the following Namespaces: NationalInstruments.DAQmx.dll

Select «Browse» and FindNationalInstruments.DAQmx.dll

C:\Program Files (x86)\National Instruments\...

Page 34: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

DAQ in C# with DAQmx – Analog Outprivate 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);

}

Page 35: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 36: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 37: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Analog Out + Analog In

Hans-Petter Halvorsen, M.Sc.

Page 38: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

“Loop-back” Test

Page 39: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Write/Read Data using USB-6008

Page 40: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 41: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Write/Read Data using USB-6008

Page 42: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 43: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Improvements

Hans-Petter Halvorsen, M.Sc.

Page 44: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Improvements

• Using a Timer

• Trend/Plot the Data from the DAQ device in a Chart

• Create and Use separate Classes for implementing the DAQ code

• …

Page 45: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Timer

public Form1(){

InitializeComponent();

timer1.Start();}

private void timer1_Tick(object sender, EventArgs e){

… //Read from DAQ Device… //Formatting… //Plot Data

}

Timer Event:

Initialization:

Properties:

Structure your Code properly!!Define Classes and Methods which you can use here

In Visual Studio you may want to use a Timer instead of a While Loop in order to read values at specific intervals.

You may specify the Timer Interval in the Properties Window

Select the “Timer” component in the Toolbox

Double-click on the Timer object in order to create the Event

2

3

4

1

Page 46: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Read DAQ Values using a Timer

Page 47: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

using System;using System.Windows.Forms;using NationalInstruments.DAQmx;

namespace DAQReadwithTimer{

public partial class Form1 : Form{

public Form1(){

InitializeComponent();

timer1.Interval = 1000;//mstimer1.Start();

}

private void timer1_Tick(object sender, EventArgs e){

Task analogInTask = new Task();AIChannel myAIChannel;

myAIChannel = analogInTask.AIChannels.CreateVoltageChannel("dev1/ai0","myAIChannel",AITerminalConfiguration.Differential,0,10,AIVoltageUnits.Volts);

AnalogSingleChannelReader reader = new AnalogSingleChannelReader(analogInTask.Stream);

double analogDataIn = reader.ReadSingleSample();

txtDaqRead.Text = analogDataIn.ToString("0.00");

}}

}

Page 48: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 49: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Trending Data in Visual Studiohttps://msdn.microsoft.com/en-us/library/dd489237.aspxVisual Studio has a Chart control that you can use in Windows Forms or Web application (ASP.NET)

http://www.i-programmer.info/programming/uiux/2756-getting-started-with-net-charts.html

using System.Windows.Forms.DataVisualization.Charting;...chart1.Series.Clear();chart1.Series.Add("My Data");chart1.Series["My Data"].ChartType = SeriesChartType.Line;...int[] x = {1, 2, 3, 4, 5, 6, 7, 8};int[] y = {20, 22, 25, 24, 28, 27, 24, 26};for (int i = 0; i < x.Length; i++){

chart1.Series["My Data"].Points.AddXY(x[i],y[i]);}

Creating a Web App? Use the following Namespace instead:System.Web.UI.DataVisualization.Charting

Page 50: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 51: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 52: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,
Page 53: Using USB-6008 in Visual Studio/C# USB-6008 in...•In order to install the DAQmx API for C#, make sure to select Custom and then .NET Support when installing the DAQmx driver. •Next,

Hans-Petter Halvorsen

University of South-Eastern Norway

www.usn.no

E-mail: [email protected]

Web: https://www.halvorsen.blog