Clients sometimes ask us whether it is possible to control our instruments from within programming environments other than LabVIEW. The general answer is yes, but it does take some effort, since Metrolab currently only provides a high-level Application Programming Interface (API) for LabVIEW. In this post, we will explain the general principle, and provide specific instructions and code samples for Matlab and Excel.
The basic idea is to use your programming environment's facility for calling functions in a dynamic link library (DLL), to call the functions in National Instruments' Virtual Instruments Standard Architecture (NI-VISA) library.
Here are some key functions in this library:
VISA is a standard (NI-VISA being the implementation developed by National Instruments), and you can download the library documentation from the relevant standards body, the IVI Foundation. The two most useful documents are probably "VPP-4.3: The VISA Library" and "VPP-4.3.2: VISA Implementation Specification For Textual Languages".
The commands to be sent, and the format of the results, are documented in your instrument's User Manual, for example in Chapter 6 of the THM116 and PT2026 manuals, or Chapter 3 of the FDI2056 manual.
Matlab can call C/C++ functions using the so-called MEX files. Metrolab have written a MEX file that uses the above principle to provide basic instrument control for the THM1176 family. It provides the following Matlab commands:
You can download the Matlab driver from here: Simple Matlab THM1176 Driver. Simply change your working directory to the directory containing the file "THM1176MatlabDriver.mexw64", or move this file into your Matlab path, and execute the commands as shown above.
Note that this driver is provided as-is, with no warranty or support. You can add functionality, or adapt it to control other instruments, by modifying the code provided (Visual Studio solution/project included). If you do, feel free to share your results!
Excel can access DLL functions in a number of ways, as described in this article on the Microsoft Developer Network. The first of these is to link DLL functions into Excel's built-in programming language, Visual Basic (VBA), using the "Declare" statement.
The IVI Foundation document "VPP-4.3.2: VISA Implementation Specification For Textual Languages" (see above; Appendix A.3) contains an interface definition file, "visa32.bas", with a long list of functions and constant definitions, thus doing all the hard work for you. Even better, a copy of visa32.bas is included when you install NI-VISA (C:\Program Files (x86)\IVI Foundation\VISA\WinNT\agvisa\include). This file can be directly imported into Visual Basic for Excel, providing direct access to the entire VISA library.
Using this technique, Metrolab have written a sample Excel file to send arbitrary queries or commands to an instrument. It has been tested with the THM1176, but it should work without modification for the PT2026 or FDI2056, as well. You can down load this Excel file here: Sample Excel Driver code.
Running the spreadsheet is straightforward: (see illustration at top of page)
A real application will of course have to expand on this sample code. Microsoft's Excel VBA reference document is less than stellar, but fortunately there are a number of good tutorials around, such as this one. Again, Metrolab's code is provided as-is, with no warranty or support – and again, feel free to share any interesting functionality you add!
Metrolab have not written any control software in any of these scripting languages, but National Instruments have published an excellent white paper on the subject, "Advanced Scripting in Perl, Python and Tcl." Amongst other topics, the article covers how to call C-Style DLLs from scripts and how to use VISA for instrument control in scripts.
In addition, Python users can use PyVISA, a Python wrapper for VISA.