The .NET function palette in LabVIEW provides capabilities to invoke the code written in C# by passing the necessary inputs and getting the results from the function. It's a seamless integration and it's explained in detail in my blog "Calling a C# .NET function in LabVIEW".
If you are using this approach to built a electronic test and measurement code, you may see a most common case where the instrument handle has to be passed from LabVIEW to C#(.NET methods) or vice-versa. But what is so unique about that? is it not same as passing other data-types such as strings and numeric as parameter to .NET methods? not actually, you can see from below image that the NI DCPower instrument handle type in LabVIEW and C# are not the same, this has resulted in a broken wire while passing the instrument handle from LabVIEW to C#.
This is an simple example of NI DCPower instrument session sharing issue, but it is common across any IVI and VISA references that needs to be shared between LabVIEW and C#.
In this case, we can choose the approach to initialize a new instrument handle in C# function, instead of passing the initialized handle from LabVIEW
- Incase of IVI, the instrument handle has to be uninitialized in LabVIEW before initializing in C#. Else the LabVIEW instrument handle will become invalid.
- We may end up creating a new instrument session and closing them every time we call a C# method from LabVIEW. This will increase the test time and bring in inefficiencies.
Lets see how to work with the instrument handles in case of different scenarios in a efficient way..
Sharing IVI session handle between LabVIEW and C# - Session Initialized in LabVIEW:
Lets get started with the first case of sharing an IVI session between LabVIEW and C#, while the session is created(initialized) in LabVIEW.
We will now see a step by step procedure to pass an initialized session from LabVIEW to C# code. Lets consider an C# example method called "StressTest" that takes initialized instrument session as input.
Lets get started with steps to pass the session from LabVIEW to C#:
Step 1: Use the "Refnum to Session" node in LabVIEW to get the instrument session from the IVI instrument handle. The instrument session is U64 type and this can be passed to C# method called using .NET invoke node in LabVIEW.
Step 2: Update the C# code to take instrument session(ulong type) as input, instead of IVI session type.
Instead of editing the "StressTest" method, I am going to create a wrapper method on top of it to access the ulong session as show below.
- As shown in line number 39 in above code, we can use the sessionHandle passed from LabVIEW to create a new instrument driver session object in C#. Please note that this will not create a entirely new session, but it will create a .net driver session object from the existing session in the same process. So any instrument configurations done to the IVI session in LabVIEW are not reverted while creating the session in C#.
- You can now call the StressTest(the C# method), by passing the .NET instrument session object.
- And the end, please remember to Close and Dispose the instrument session in C# as shown in line number 45 and 46. Please note that this will only destroy the session object created in C#. We will still be able to use the Session in LabVIEW without initializing it again.
Step 3: Build the C# project and call the C# method(LV_StessTest) in LabVIEW using the .NET invoke node.
In this way, the IVI session initialized in LabVIEW can be passed to C#. I tried this approach with NI DMM, NI FGEN, NI Scope, NI RFmx and NI Digital drivers, so I believe this will work for other NI IVI instrument session types as well.
You can access the example code used in above steps from this link for reference.
Note: Since I used static C# method as example in the above code, I had to create and close the session in the same method in C#. I encourage you to use non-static classes and methods while developing your C# code, so that you can create a method to pass all the required instrument sessions to C#, and that method can create and store the .NET sessions in the class object. Develop rest of the methods to access the instrument sessions directly from its class object instead of getting it from LabVIEW every time.
0 Comments