Oldal: 1/6 A feladat során megismerkedünk a C# és a LabVIEW összekapcsolásának egy lehetőségével, pontosabban nagyon egyszerű C#- ban írt kódból fordítunk DLL-t, amit meghívunk LabVIEW-ból. Az eljárás LabVIEW 2011, 2014 és Visual Studio 2010, 2012, 2013 és Windows 8.1 rendszer alatt lett tesztelve. 1. Nyissuk meg a Visual Studio-t. 2. Hozzunk létre egy C# Console Application-t (opcionálisan class library-t).
Oldal: 2/6 3. Nevezzük át a projektet "LabVIEWInterface"-re a solutiont "LabVIEWIntegration"-ra. 4. Szerkesszük az assembly info-t úgy, hogy a library verziója minden egyes buildnél növekedjen. Ez hasznos lesz később, hogy követni tudjuk LabVIEW-ből melyik verziót használjk majd. Módosítsuk az AssemblyInfo.cs végét a következőképp: // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: [assembly: AssemblyVersion("1.0.*")] //[assembly: AssemblyVersion("1.0.0.0")] //[assembly: AssemblyFileVersion("1.0.0.0")] 5. Szintén hasznos kitölteni a az assembly info-t. Például // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("LabVIEWInterface")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("SZE")] [assembly: AssemblyProduct("LabVIEW C# interface")] [assembly: AssemblyCopyright("Copyright 2015")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] 6. Töröljük a Program.cs fájlt. 7. Mivel nem EXE, hanem DLL fájlt szeretnénk készíteni jobb klikk a projekten» Properties. Ez kell látnunk:
Oldal: 3/6 Magyarázat: a LabVIEW (legalábbis 2011-2014) alapvetően.net 3.5 runtime-ot támogat, de van lehetőségünk a 4.0 vagy a 4.5 runtime-ra is áttérni. Ehhez a LabVIEW.exe mellé egy LabVIEW.exe.config fájlt kell létrehozni a következő tartalommal: <configuration> <startup uselegacyv2runtimeactivationpolicy="true"> <supportedruntime version="v4.0.30319"/> </startup> </configuration> Persze a ezt majd a buildelt exe mellé is oda kell tenni, csak ott nem LabVIEW.exe.config névvel, hanem az aktuális exe nevével helyettesítve. Az egyszerűség kedvéért válasszuk a Target framework-ot 3.5-nek az Output type-ot pedig Class Library-nek. 8. Adjunk hozzá egy új osztályt. Legyen a neve MyMath.cs.
Oldal: 4/6 9. Módosítsuk a tartalmát így: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LabVIEWInterface { public class MyMath { public double MyCos(double x) { return Math.Cos(x); } } } 10. Most buildeljük a DLL-t. 1> LabVIEWInterface -> C:\Teszt\LabVIEWInterface\bin\Debug\LabVIEWInterface.dll ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== 11. Most nyissuk meg a LabVIEW-t. 12. Create an Empty Project (context menu) 13. 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.
Oldal: 5/6 14. 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. 15. DLL In Project 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..net Palette 16. 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. Constructor Node (Palette) Constructor Node (Block Diagram) 17. 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
Oldal: 6/6 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. Wire Invoke NodeNow let s add a control for the input and an indicator for the output. Right click on each side, on 18. the left choose Create Control, on the right choose Create Indicator. Invoke Node 19. 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. With Dispose Reference 20. Now flip to the Front Panel Ctrl-E and arrange your controls and indicators. Here s ours: Front Panel Control and Indicator 21. 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.