Per Erik Strandberg /cv /kurser /blog

Importing .NET code into Labview is almost as easy as the other way around (see Labview And Dot Net Combo Part 2 for some tips and tricks on that). The tricky part for me was to add the dll into the right folder in the Labview project and to compile the .NET code for the 2.0 version of the framework. If you are more into native code then Labview Native Cansi Combo might be of interest to you.

A simple example in C#/.NET

I decide to make a class that retrieves todays date - a couple of lines of code and the usual boiler plate code. I use the wonderful but underestimated out keyword (see [1]). Note that I removed a lot of default references and using statements:
http://www.pererikstrandberg.se/blog/dotnet_in_labview/1_cs_code.png

Target Framework

As you could see in my example where I use Labview And Dot Net Combo Part 2 you need to use an older .NET framework than the latest. Right click on the project in the solution explorer and select properties.
http://www.pererikstrandberg.se/blog/dotnet_in_labview/2_target_framework.png

Add the dll into the Labview project

Once the dll is compiled you need to add it to a (sub-)folder of the Labview project - something like the below image.
http://www.pererikstrandberg.se/blog/dotnet_in_labview/3_lvproj.png

The Labview code

Adding the .NET class and method into Labview is not that hard - you just need a constructor node and an invoke node. Please note that Labview will not find your assembly unless the dll is physically onside the Labview project (that gave me a headache until I figured it out).

http://www.pererikstrandberg.se/blog/dotnet_in_labview/4_labview_code.png

Static Classes

So the obvious question now is: "Why didn't I make the class static since it is such a silly example?" And the answer is I didn't think about at the time. But for the sake of completeness let's add another example where we use a static class and method.

I added the following class to my existing Assembly. As you can see I use a static class and a static method.

    public static class StaticTimeFinder
    {
        public static void WhatTimeIsIt(out int hour, out int minute, out int second)
        {
            hour = DateTime.Now.Hour;
            minute = DateTime.Now.Minute;
            second = DateTime.Now.Second;
        }
    }

To import into Labview I first tried to use the same method as above - but I had little luck. I then found a post in an NI forum (see [2]) where it is explained.

The summary is to skip the constructor node and link directly from the invoke node. Right click in the top left terminal of the invoke node to get the menu where you choose "Select Class" - ".NET" and then browse for your assembly, class and method.

The glorious screen shot illustrates the principle:
http://www.pererikstrandberg.se/blog/dotnet_in_labview/5_static.png


See also Labview Native Cansi Combo
See also Labview And Dot Net Combo Part 2
This page belongs in Kategori Programmering