SlideShare a Scribd company logo
Using USB-6008
in Visual Studio/C#
Hans-Petter Halvorsen
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
Software
• Visual Studio/C#
• DAQmx Driver
• DAQmx Driver can be downloaded for free
from Internet
DAQ
Hans-Petter Halvorsen, M.Sc.
Data Acquisition
DAQ – Data Acquisition
A DAQ System consists of 4 parts:
1. Physical input/output signals, sensors
2. DAQ device/hardware
3. Driver software
4. Your software application (Application software)
NI DAQmx Driver
Your App created
with C#
NI USB 6008 DAQ Device
Sensors, etc.
AD Converter
DA Converter
Measurement Signal
Control Signal
USB
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).
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.
Continuous
vs.
Discrete
Signals
Sampling and Aliasing
Original Signal
Aliasing (“Nedfolding”) -> The Sampling Rate is to low!
Sampling Time
Sampling Frequency
USB-6008
Hans-Petter Halvorsen, M.Sc.
How-To use USB-6008 with Visual Studio
USB
Analog I/O
Digital I/O
USB-6008
PC with Visual Studio
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 Output
AI – Analog Input
DO – Digital Output
DI – Digital Input
USB Connection -10-10V
0-5V
DAQmx Driver
Hans-Petter Halvorsen, M.Sc.
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.x
Support for the version of .NET that your version of
Visual Studio is using.
NI DAQmx Driver Installation
MAX – Measurement & Automation Explorer
NI USB-6008 “Dev1”
You may change the name (“Dev1”)
Analog In
Hans-Petter Halvorsen, M.Sc.
C# Examples
Read Analog Values
We will read the voltage values on different batteries
Read Analog Signals with USB-6008
Multimeter
USB
9V
9V
A 9V Battery is connected to
Analog In on USB-6008
Read from USB-6008 DAQ Device
Simple DAQ in C# with DAQmx
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");
Analog In Example
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");
WinForm App
Add Assembly
References
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 Find
NationalInstruments.DAQmx.dll
C:Program Files (x86)National Instruments...
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
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");
}
}
}
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Analog Out
Hans-Petter Halvorsen, M.Sc.
C# Examples
Write to USB-6008 DAQ Device
Simple DAQ in C# with DAQmx
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);
Analog Out Example
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);
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 Find
NationalInstruments.DAQmx.dll
C:Program Files (x86)National Instruments...
DAQ in C# with DAQmx – Analog Out
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);
}
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Analog Out + Analog In
Hans-Petter Halvorsen, M.Sc.
“Loop-back” Test
Write/Read Data using USB-6008
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Write/Read Data using USB-6008
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Improvements
Hans-Petter Halvorsen, M.Sc.
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
• …
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
Read DAQ Values using a Timer
using System;
using System.Windows.Forms;
using NationalInstruments.DAQmx;
namespace DAQReadwithTimer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Interval = 1000;//ms
timer1.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");
}
}
}
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Trending Data in Visual Studio
https://msdn.microsoft.com/en-us/library/dd489237.aspx
Visual 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
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Using USB-6008fdsfsfsdfaafds in CSharp.pdf
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog

More Related Content

PDF
Daq toolbox examples_matlab
PPTX
Ishiriya Wireless Technologies-MATLAB Data Acquisition
DOCX
Kmem 3116 al2 report (keb090001)
PDF
TC01 - Visvvsvafaffsdfsfsdfaual Studio.pdf
PPTX
micro manit.pptx
PPTX
Data Acquisition Systems presentation
PDF
Introduction to DAQ with LabVIEW - Overview.pdf
PDF
MCC_ProductSelGuide.pdf
Daq toolbox examples_matlab
Ishiriya Wireless Technologies-MATLAB Data Acquisition
Kmem 3116 al2 report (keb090001)
TC01 - Visvvsvafaffsdfsfsdfaual Studio.pdf
micro manit.pptx
Data Acquisition Systems presentation
Introduction to DAQ with LabVIEW - Overview.pdf
MCC_ProductSelGuide.pdf

Similar to Using USB-6008fdsfsfsdfaafds in CSharp.pdf (20)

PDF
Acs frisco 2017
PDF
G code based data receiving and control system
PPTX
Data Acquisition System
PPTX
Introduction to National Instrument Data Logging Machine Monitoring and Pow...
PPTX
Digital to Analog Converter by LDCE students
PDF
AUTOMATIZACION LabVIEW LINX and Arduino.pdf
PDF
Introduction to lab view 8.6 in 3 hours
PPT
TMCS - Guide to Data Acquisition Systems (Daq).ppt
PPTX
Lesson 1 - Setting Up Hardware.pptx
PDF
ScilabTEC 2015 - Embedded Solutions
PPTX
Incorporating Wireless Measurements with Wired Data Acquisition Systems
PPTX
BHAVESH AGRAWAL.pptx
PDF
Lab view daq signal conditioniing
DOCX
Guide To Data Acquisition Systems (Daq Systems) - TMCS.docx
PPT
Labview.ppt
PPTX
Data acquisition system
PDF
Introduction to lab_view
PPTX
Introduction to lab view 8.6 in 3 hours
PPT
Cap.10
Acs frisco 2017
G code based data receiving and control system
Data Acquisition System
Introduction to National Instrument Data Logging Machine Monitoring and Pow...
Digital to Analog Converter by LDCE students
AUTOMATIZACION LabVIEW LINX and Arduino.pdf
Introduction to lab view 8.6 in 3 hours
TMCS - Guide to Data Acquisition Systems (Daq).ppt
Lesson 1 - Setting Up Hardware.pptx
ScilabTEC 2015 - Embedded Solutions
Incorporating Wireless Measurements with Wired Data Acquisition Systems
BHAVESH AGRAWAL.pptx
Lab view daq signal conditioniing
Guide To Data Acquisition Systems (Daq Systems) - TMCS.docx
Labview.ppt
Data acquisition system
Introduction to lab_view
Introduction to lab view 8.6 in 3 hours
Cap.10
Ad

More from ManhHoangVan (20)

PDF
MQTfsdaffffffffffffffffffffffffffffffffffffffffT.pdf
PPTX
Modbusprofibus01profibus01profibus01.pptx
PPTX
HARprofibus01profibus01profibus01profibus01T.pptx
PDF
OPprofibus01profibus01profibus01profibus01C.pdf
PPTX
Profinetprofibus01profibus01profibus01profibus01.pptx
PPTX
profibus01profiprofibus01profibus01bus01.pptx
PDF
Discrete Control Sysfstem in LabVIEW.pdf
PDF
04. SQL Servesdafr with CSharp WinForms.pdf
PDF
05. Datalogging SQL Server with CSharp WinForms.pdf
PDF
09. OPC DA with Measurement Studio 2019.pdf
PDF
Create and Use Multipfsfsdfafasle Forms.pdf
PDF
Azure DevOfsdfsdfsfasfsdfasfsdfsdfsdps.pdf
PPTX
OPC_Basics_Webcasfsfsfdsfsdafsdfsdfsdfasfdasft_SWTB.pptx
PPTX
OPdfsafsdfasdfaaaaaaafdsfasdfdasfdasfdasfC.pptx
PPT
4597231dsafsfsafsafsfsafsfaffsdfdsfsfds.ppt
PDF
1 circuit variables1 circuit variables.pdf
PDF
Lect-06Lect-06Lect-06Lect-06Lect-06Lect-06Lect-06Lect-06
PDF
Lllsjjsjsjjshshjshjsjjsjjsjjzjsjjzjjzjjzj
PDF
discrete_state_spaceeeeeerrrrrrrrrrrrrrrr
PDF
Labhbbbbbbbbbbbb;ggghhhhhhhhhhhhhhhhhhhhhhhhhhhh
MQTfsdaffffffffffffffffffffffffffffffffffffffffT.pdf
Modbusprofibus01profibus01profibus01.pptx
HARprofibus01profibus01profibus01profibus01T.pptx
OPprofibus01profibus01profibus01profibus01C.pdf
Profinetprofibus01profibus01profibus01profibus01.pptx
profibus01profiprofibus01profibus01bus01.pptx
Discrete Control Sysfstem in LabVIEW.pdf
04. SQL Servesdafr with CSharp WinForms.pdf
05. Datalogging SQL Server with CSharp WinForms.pdf
09. OPC DA with Measurement Studio 2019.pdf
Create and Use Multipfsfsdfafasle Forms.pdf
Azure DevOfsdfsdfsfasfsdfasfsdfsdfsdps.pdf
OPC_Basics_Webcasfsfsfdsfsdafsdfsdfsdfasfdasft_SWTB.pptx
OPdfsafsdfasdfaaaaaaafdsfasdfdasfdasfdasfC.pptx
4597231dsafsfsafsafsfsafsfaffsdfdsfsfds.ppt
1 circuit variables1 circuit variables.pdf
Lect-06Lect-06Lect-06Lect-06Lect-06Lect-06Lect-06Lect-06
Lllsjjsjsjjshshjshjsjjsjjsjjzjsjjzjjzjjzj
discrete_state_spaceeeeeerrrrrrrrrrrrrrrr
Labhbbbbbbbbbbbb;ggghhhhhhhhhhhhhhhhhhhhhhhhhhhh
Ad

Recently uploaded (20)

PPTX
Construction Project Organization Group 2.pptx
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
web development for engineering and engineering
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
Construction Project Organization Group 2.pptx
additive manufacturing of ss316l using mig welding
Lesson 3_Tessellation.pptx finite Mathematics
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
web development for engineering and engineering
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Digital Logic Computer Design lecture notes
CYBER-CRIMES AND SECURITY A guide to understanding
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Foundation to blockchain - A guide to Blockchain Tech
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Model Code of Practice - Construction Work - 21102022 .pdf
Lecture Notes Electrical Wiring System Components
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)

Using USB-6008fdsfsfsdfaafds in CSharp.pdf

  • 1. Using USB-6008 in Visual Studio/C# Hans-Petter Halvorsen
  • 2. 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
  • 3. Software • Visual Studio/C# • DAQmx Driver • DAQmx Driver can be downloaded for free from Internet
  • 5. DAQ – Data Acquisition A DAQ System consists of 4 parts: 1. Physical input/output signals, sensors 2. DAQ device/hardware 3. Driver software 4. Your software application (Application software) NI DAQmx Driver Your App created with C# NI USB 6008 DAQ Device Sensors, etc.
  • 6. AD Converter DA Converter Measurement Signal Control Signal USB 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).
  • 7. 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. Continuous vs. Discrete Signals
  • 8. Sampling and Aliasing Original Signal Aliasing (“Nedfolding”) -> The Sampling Rate is to low! Sampling Time Sampling Frequency
  • 10. How-To use USB-6008 with Visual Studio USB Analog I/O Digital I/O USB-6008 PC with Visual Studio
  • 11. 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 Output AI – Analog Input DO – Digital Output DI – Digital Input USB Connection -10-10V 0-5V
  • 13. 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.x Support for the version of .NET that your version of Visual Studio is using.
  • 14. NI DAQmx Driver Installation
  • 15. MAX – Measurement & Automation Explorer NI USB-6008 “Dev1” You may change the name (“Dev1”)
  • 16. Analog In Hans-Petter Halvorsen, M.Sc. C# Examples
  • 17. Read Analog Values We will read the voltage values on different batteries
  • 18. Read Analog Signals with USB-6008 Multimeter USB 9V 9V
  • 19. A 9V Battery is connected to Analog In on USB-6008
  • 20. Read from USB-6008 DAQ Device
  • 21. Simple DAQ in C# with DAQmx 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"); Analog In Example
  • 22. 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");
  • 25. 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 Find NationalInstruments.DAQmx.dll C:Program Files (x86)National Instruments...
  • 27. 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"); } } }
  • 30. Write to USB-6008 DAQ Device
  • 31. Simple DAQ in C# with DAQmx 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); Analog Out Example
  • 32. 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);
  • 33. 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 Find NationalInstruments.DAQmx.dll C:Program Files (x86)National Instruments...
  • 34. DAQ in C# with DAQmx – Analog Out 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); }
  • 37. Analog Out + Analog In Hans-Petter Halvorsen, M.Sc.
  • 44. 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 • …
  • 45. 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
  • 46. Read DAQ Values using a Timer
  • 47. using System; using System.Windows.Forms; using NationalInstruments.DAQmx; namespace DAQReadwithTimer { public partial class Form1 : Form { public Form1() { InitializeComponent(); timer1.Interval = 1000;//ms timer1.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"); } } }
  • 49. Trending Data in Visual Studio https://msdn.microsoft.com/en-us/library/dd489237.aspx Visual 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
  • 53. Hans-Petter Halvorsen University of South-Eastern Norway www.usn.no E-mail: hans.p.halvorsen@usn.no Web: https://www.halvorsen.blog