Example Code

Share RFSA Session from .NET to LabVIEW-built Interop Assembly DLL (Class Library)

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

 

Overview
This example shows how to build a LabVIEW .NET DLL (interop assembly) in such a way that it can be called from .NET while passing instrument handles. Regular inputs and outputs (strings, numerics) are straightforward to pass between DLL methods and the .NET environment, but this can become tricky when dealing with session references to hardware (DAQmx, NI-SCOPE, NI-FGEN, NI-RFSA, NI-RFSG to name a few). Moreover, if additional driver-level calls are required on the .NET side, things can become even tricker.

This example shows one way to pass an instrument handle from the LabVIEW DLL environment to Visual Studio (C#) in multiple scenarios:

When all the instrument-level calls are made within the LabVIEW DLL

When the instrument session is initialized in the LabVIEW DLL but instrument-level calls are made in both the LabVIEW and .NET

When the instrument session is initialized in Visual Studio (C#) but intrsrument-level calls are made in both the LabVIEW DLL and .NET

This example uses the RFSA .NET Class Library, which is a recommended approach to .NET programming of instruments. Refer to the National Instruments .NET Driver Support page for more details on available .NET drivers for your instrument.


Description
First, the LabVIEW DLL functions need to be configured properly. Instead of passing the RFSA handle directly as inputs and outputs to the VIs, the functions Session to Refnum and Refnum to Session are used. These allow casting from an integer (U64) to a RFSA handle inside the LabVIEW VI, which prevents having to deal with the LVBaseRefnum (the type of the RFSA Handle as a regular input or output) on the .NET side.

Once the DLL is built, this can be called from .NET without too many issues. A RFSA object in .NET will have an underlying handle of type IntPtr, which will need to be converted to U64 before being passed to the DLL.

Note that you can initialize an RFSA object on an existing instrument session using the Class Library in .NET. This is done using the following method:

rfsaDotNetSession = new NIRfsa(rfsaIntPtr);

Where rfsaIntPtr can be acquired from the U64 Session Out output from a DLL call. To extract the IntPtr of an existing RFSA session in .NET to pass into LabVIEW, use the following method:

rfsaIntPtr = rfsaDotNetSession.DangerousGetInstrumentHandle();

You can then use rfsaIntPtr to convert to a U64 and pass into the LabVIEW DLL call.


Requirements

  • LabVIEW 2014 (or compatible)
  • LabVIEW Run-Time 14
  • Visual Studio 2012 or later
  • NI-RFSA 14.5 or later
  • NI-RFSA .NET Class Library 14.5 or later (.NET 4.0)
  • RFSA Instrument (NI PXIe-5644R was used for this example)


Steps to Implement or Execute Code

  1. Open the attached dotNETClassLibraryToLV solution.
  2. Change the resource name in Program.cs to match your RFSA-specific instrument alias:

string RfsaResourceName = "RIO0";

  1. Build and run the solution.

 

Additional Information or References
VI Block Diagram

 Block Diagram.png

 **This document has been updated to meet the current required format for the NI Code Exchange.**

 

 

Xavier

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.