Archive for November, 2009

Using .NET in LabVIEW

November 30, 2009

If you have .NET class libraries from previous work, but now need to work in LabVIEW, you can easily use those libraries in your LabVIEW applications.

In the LabVIEW functions window look for .NET under Connectivity.

LvFunctions

Place the Constructor VI on your block diagram. A window will pop up to let you browse to the .NET dll you want to use. After selecting the dll, you then select which class from the dll that you want to use. Use the Property Node and Invoke Node VIs to utilize the .NET object.

The following image shows a LabView block diagram for using the Calc.dll which was created in previous blog post.

LvBlockDiagram

The following image shows the LabVIEW front panel for the application.

LvFrontPanel

The front panel here shows the error in control and error out indicator. If this is your top level VI, you may want to hide these by right clicking on them, selecting Advanced then Hide Control or Hide Indicator. A properly written application would also have some error checking and handling built in.

C# Class Libraries

November 29, 2009

When writing programs for .NET it works best to put all code into class libraries. This makes it easier to reuse the code in other .NET projects as well as in other environments.

Here I will do a simple four function calculator to show how to set up multiple class library projects in a single solution in Visual Studio.

The following image shows the projects in the Solution Explorer window.

SolutionExplorer

The CsLibDemo is a Windows Application project. The other projects are Class Library projects.  When the wizard created the CsLibDemo project it created the Program.cs file and a Windows Form file.  I moved the Windows Form file to the CsLibDemoUI project. Any user interface Forms and Control Librairs that are specific to this application can be put into this project.  Any user interface classes that are to be used in other applications should be placed in a separate project.

The following image is a screen shot of Windows Explorer, showing how the projects are organized on the hard drive.

WindowsExplorer

The following image is the user interface for testing the class library.

Calc

Unit testing is useful for ensuring the proper functioning of your class library. Download NUnit from nunit.org. Here is a screen shot of the NUnit program.

NUnit

Here is the code in the Calc.cs file.

namespace MyCalc
{
    public class Calc
    {
        public double Add(double a, double b)
        {
            return a + b;
        }

        public double Subtract(double a, double b)
        {
            return a - b;
        }

        public double Multiply(double a, double b)
        {
            return a * b;
        }

        public double Divide(double a, double b)
        {
            return a / b;
        }
    }
}

Here is the code from CsLibDemoForm.cs

namespace CsLibDemoUI
{
    public partial class CsLibDemoForm : Form
    {
        Calc calc = new Calc();

        public CsLibDemoForm()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            textBoxC.Text = calc.Add(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
        }

        private void buttonSubtract_Click(object sender, EventArgs e)
        {
            textBoxC.Text = calc.Subtract(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
        }

        private void buttonMultiply_Click(object sender, EventArgs e)
        {
            textBoxC.Text = calc.Multiply(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
        }

        private void buttonDivide_Click(object sender, EventArgs e)
        {
            textBoxC.Text = calc.Divide(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
        }
    }
}

Here is the unit test code from CalcTest.cs

namespace CsLibDemo.Tests 
{
	using System;
	using NUnit.Framework;
    using MyCalc;

	[TestFixture] 
	public class CalcTest
	{
		protected double dValue1;
		protected double dValue2;
        protected Calc calc;
		
		[SetUp] public void Init() 
		{
			dValue1= 6;
			dValue2= 3;
            calc = new Calc();
		}

		[Test] public void Add() 
		{
            double result = calc.Add(dValue1, dValue2);
			Assert.AreEqual(9, result, "Add");
		}

        [Test]
        public void Subtract()
        {
            double result = calc.Subtract(dValue1, dValue2);
            Assert.AreEqual(3, result, "Subtract");
        }

        [Test]
        public void Multiply()
        {
            double result = calc.Multiply(dValue1, dValue2);
            Assert.AreEqual(18, result, "Multiply");
        }

		[Test] public void Divide() 
		{
            double result = calc.Divide(dValue1, dValue2);
            Assert.AreEqual(2, result, "Multiply");
        }
	}
}

The unit test code here needs some improvement, but I was keeping it simple for demonstration purposes.

In a future blog I will talk about reusing this class library in LabVIEW.

If anyone has ideas about how to format code in wordpress, or wants more details please leave comments.


Follow

Get every new post delivered to your Inbox.