INTEGRATING LABVIEW AND C#

http://erdosmiller.com/integrating-labview-and-c-1/


This article is intended to go over the very basics of integrating C# with LabVIEW. In this article we’ll go over creating a very basic C# class, compiling a DLL, and accessing that class from LabVIEW. We’ll be using Visual Studio 2010 and LabVIEW 2010 on Windows 7.

 

You can access all of the code for LabVIEW and Visual Studio in the following GitHub repository: https://github.com/erdosmiller/lvcstut1

 

CREATING A DLL IN VISUAL STUDIO

 

Open Visual Studio 2010 and create a new project. We want a Visual C# Console Application (or class library.)

 

 

Rename the project to “LabVIEWInterface” and the Solution to “LabVIEWIntegration.

 

 

Edit your assembly info so that the version of the library automatically increments with each build. This will help when interfacing the DLL to LabVIEW as it will be able to detect the change. Modify your AssemblyInfo.cs to look like this at the end.

 

 

It’s also a good idea to fill out the general information. Here’s ours.

 

 

We don’t need “Program.cs,” so let’s go ahead and delete it. Your solution should now look like this.

 

 

We want the output of our project to be a DLL and not an EXE, so we need to modify the project settings. Right click on the project to access it’s properties. Change the “Output Type” to “Class Library.” Also, as of this writing, LabVIEW 2010 is not compatible with the .NET 4 run-time, so we’ll need to change our target framework to .NET 3.5. We also want to change our Assembly Name and Default Namespace. Change the Assembly Name and Default Namespace to be “LabVIEWInterface.” When you save your settings, it may warn you, but you can ignore it.

 

 

Now let’s add our class. Right-click on the project, scroll to “Add” and choose class. In the “Add New Item” menu, choose Class and hit “Add.” You’ll notice a new class was created with the default namespace we specified. Now try and build your project. Right-click on the project and select “Open Folder in Windows Explorer.” Then browse to “bin/Debug.” You should see the compiled DLL, “LabVIEWInterface.dll.”

 

 

 

Now let’s make our class do something useful; we’ll add our a cosine function. Modify your code to look like this. Also, rename your class file to “MyMath.cs” and build the project again.

 

 

 

If your build was successful, then you’re ready to move on to the LabVIEW portion of the tutorial.

 

CALLING THE DLL FROM LABVIEW

 

Open LabVIEW 2010 and create a new empty project, save that project somewhere in your workspace as “CSInterface.lvproj.”

 


 

 

Let’s retrieve our DLL from visual studio. Go back to Visual Studio, and right click on your project. Choose “Open Folder in Windows Explorer” and browse to “bin/Debug.” Copy “LabVIEWInterface.dll” to where you saved your LabVIEW project.

 

Now add the DLL to your LabVIEW project. Right click on “My Computer” in the LabVIEW project, choose “Add” and then “File.” Browse and select the DLL.

 

 

Now let’s create a blank VI. Right click on “My Computer” and select “New” and then “VI.” When the VI opens, save it in the same directory as the DLL as “CSInterface.vi”

 

Now let’s call our DLL, open the block diagram and right click. Expand to the full function menu, and browse to “Connectivity and then “.NET.” Pin the .NET palette to the screen.

 

 

The first thing we need to do to interface to our class is instantiate an instance. Place a .NET “Constructor Node” on the block diagram. When you place the node on the block diagram, you’ll be presented with a menu, by default it shows you DLLs that LabVIEW is already aware of. Click “Browse” in the upper right hand corner, browse to where you saved your project and DLL, and open the DLL. LabVIEW will then present you with a dialog  that displays the objects available in the namespace. Select the “MyMath” constructor we created. You now have a “Constructor Node” on the block diagram that points to you “MyMath” class.

 

 

Now we need to add an “Invoke Node” so that we can call our MyCos function. Place an Invoke Node on the block diagram and then wire it up to the MyMath Constructor Node. You’ll notice the text of the Invoke Node is black, this represents that we’ve not yet selected a method. Click on it once and select the “MyCos” method. We’re now presented with the input argument on the left and the output on the right.

 

 

Now let’s add a control for the input and an indicator for the output. Right click on each side, on the left choose “Create Control,” on the right choose “Create Indicator.”

 

 

The last thing we need to do is dispose of our reference to the class. Place a “Close Reference” block from the .NET palette on the diagram and wire it up to the right of the Invoke Node.

 

 

Now flip to the Front Panel “Ctrl-E” and arrange your controls and indicators. Here’s ours:

 

 

Now we’re ready to run the code! Enter a value for X and press run! When we run with “x” as “0″, we get back “1″ from the MyCos function as expected.

 

Now that our basic example is working, it’s time to close this tutorial. We hope this tutorial gave you a basic insight to interfacing C# and LabVIEW, and that you can use it as a starting point for building applications that do something interesting.


C#入门资料, 章.1⒈2第 I部分 C#语 言.NET体系结构 …¨…¨∵¨…¨¨3C#与 .NET的 关系………………3公共语言运行库 …………………41.2.1 平台无关性……………………41,2,2 提高性能………………………41.2.3 语言的互操作性……………¨5中间语 言 … ¨ ¨ … … … …… … …61.3.1 面向对象和接口的支持………61.32 不同的值类型和引用类型∴·…71,3.3 强数据类型化¨………………81.3.4 通过异常处理错误…¨……△21.3.5 特性的使用…………………△3程 序 集 …¨… ¨ … …… … … … I31.41 私有程序集…………………△41,4.2 共享程序集…………………△41.4.3 反射……………∵……………141.4.4 并行编程……………………△5。NET FramewOrk类 ¨… ¨¨… …J15名称 空 间 ¨ ¨ ¨ ¨ … … … … … 16用 C#创 建 .NET应用 程序 … … ·161.7.1 创建 AsPJ吡T应用程序……161.7.2 创建 Windows窗体…………181.7.3 使用 WPF… …………¨……181.74 Windows控 件………………△91.7.5 Windows服务………………△91.7.6 WCF¨ ¨¨¨¨¨¨……………………¨¨¨¨·191.7.7 W血dows WF· …¨¨¨¨¨…·……·………19C#在.NET企业 体系 结构中的作用………………………19小结 ………¨¨………………21核Jb C#· ∵第一个 C#程序…¨¨¨……2.1.1 代码 ………………………2.12 编译并运行程序 …………2.1.3 详细介绍 ………………¨变量 … … …… ¨ … … … … … 2,2.1 变量的初始化 ……………2.2.2 类型推断…………………2.2.3 变量的作用域 ……………2⒓ .4 常量………………………预 定义 数 据类 型 … … …… … 2.3.1 值类型和引用类型 ………2.3,2 CTs类 z型 ·:· ………………¨…2.3.3 预定义的值类型…………2.⒊ 4 预定义的引用类型 ………流控 制 … … ∴ … ¨ … … … … 2.4.1 条件语旬 …………………2.4.2 循环·¨·¨·…·………………………………2.4.3 跳转语旬……………¨…枚举¨………………………名称空间………¨¨………2‘。1 ushg语句 …………………2.6.2 名称空间的别名 …………Main()方 法…¨………¨……⒉7.1 多个 Mah0方法…………2.7.2 给Ma血()方法传递参数…有关编译 C#文 件 的更多 内容 控制 台 yo……¨¨¨……¨使用注释………¨…………2.10.1 源文件中的内部注释…2.10.2 XbIL文档…………∵…第 2章2,12,52.62,2.3.4.5.6.72.72.82.92.10.8.9第 7版)#预 处理器指令 …¨¨¨……5811.1 +dcfme矛旧hndef¨ ¨…¨………·5911.2 #i£ #elit+e1se习 阳#g“Ⅱf· ……·5911,3 +warning不口#crror¨ ¨¨¨¨¨¨6011,4 桁egion和#endrcgion¨ ¨¨¨¨611.5 #1inc· ¨¨¨¨……………………………………6I11.6 劫r鲳m犷…………………·61#编 程规则 ……………………⒍12.1 关于标识符的规则………·62122 用法约定…………………·6小结 …¨¨……………………铌象和类型¨ ¨¨… … … ¨ …69和结构 …¨¨¨¨¨¨…¨·69…………¨…………………⒛.1 数据成员…………………… 70.2 函数成员……………………·71.3 只读字段……………………·83名类型 …¨¨¨¨………… 泓构 ¨……¨…¨¨¨¨¨… 85.1 结构是值类型………………·86.2 结构和继承…………………·87,3 结构的构造函数……………·87分类 …¨¨………………… 盯态类 ………………………… B9ect类 ……¨………………·⒆.1 systeln.⒇ect0方 法·…………………90.2 Tos“吧o方法·…………¨¨¨…………90展方法 ……¨……………… 呢结 ………¨…………………叨承 … … ¨… ¨ … … … … … ·95承 的类型 …………………… %,1 实现继承和接 口继承 ………·95.2 多重继承………………∵ … 95.3 结构和类 ……………………·9现继承 ¨¨…………………%.1 虚方法………………………·97.2 隐藏方法……………………·98.3 调用函数的基类版本 ………·994.2.4 抽象类和抽象函数 ……∷·△004,2,5 密封类和密封方法 …………1004.2.6 派生类的构造 函数 …………1014.3 修饰 符 … ¨ … … … … … ¨ … …1064.3.1 可见性修饰符 ……………¨1064.3.2 其他修饰符 …………………1064.4接口… … … … ∴·… …¨ … … …1074.4,1 定义和实现接 口……………1084,4,2 派生的接口………………¨1114.5 /l、 绉占¨¨¨¨¨…………………………………………·113第 5章 泛型 ¨ … … … ¨… … …… ¨……1155,1 概述 … ¨ … … … … …… … … …1155,1.1 性胄旨……………∶·…¨¨¨¨¨¨¨¨¨¨¨1165.1.2 类型安全………………¨ 1175.1.3 二进制代码的重用 ……… 1175.1.4 代码的扩展……………… 1175.1.5 命名约定………………… 1185.2 创建泛型类…¨………………1185.3 泛型类的功能¨………¨Ⅱ…¨1225,3.1 默认值…………………¨1235,⒊ 2 约束 ……………………… 1235,3,3 继承 ……………………… 1265,3.4 静态成员………………… 1275.4 泛 型接 口… … ¨ … …… … … …1275.4.1 协变和抗变……………… 1285.4.2 泛型接口的协变………… 1295.4.3 泛型接口的抗变 ………… 1305.5 泛 型 结 构 ¨ … … … … … ¨ … ¨1315,6 泛 型方 法 … … … … …… … … ¨1345,6.1 泛型方法示例…………… 1345,6.2 带约束的泛型方法……… 1355,6,3 带委托的泛型方法 ……… 1365.64 泛型方法规范…………… 1375.7'小 结 … … ∴·… … ¨ … … … … … 138第 6章 数组……¨…¨……………1396.1 简单数组…………¨…………1396.1.1 数组的声明………………¨1396.1.2 数组的初始化 ………………1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值