Clients sometimes ask us whether it is possible to An NMR-controlled field is one whose strength is monitored by NMR. As opposed to NMR-regulated.... 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 general principle
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 A standard is the internationally agreed-upon physical representation of a unit. For example, a caesium clock is the standard for... Architecture (NI-VISA) library.
Here are some key functions in this library:
- Call viOpen to open the device. Before that, use viOpenDefaultRM to initialise VISA, and viFindRsrc to Before being able to measure, an NMR teslameter has to search the range of the probe to find the NMR... the resource database for the instrument in question. At the end of the program, be sure to call viClose.
- Call viWrite to write a command, and read the results with viRead. Check the status using viReadSTB.
- Use viAssertTrigger to programmatically trigger an acquisition.
- Use viEnableEvent and viWaitOnEvent, in conjunction with the Status Enable commands (*SRE and related), to wait for certain status-byte conditions.
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 An NMR-controlled field is one whose strength is monitored by NMR. As opposed to NMR-regulated.... for the THM1176 family. It provides the following Matlab commands:
Returns a list of instruments found, as Expanded Unaliased Names
- THM1176MatlabDriver(‘Open’, <SerialNumber> | <‘Expanded Unaliased Name’>)
Returns a handle (> 0), or an error (-1) if the device cannot be opened
- THM1176MatlabDriver(‘Close’, Handle)
Closes the communication port
- THM1176MatlabDriver(‘Read’, Handle [, NumberOfMeasurementsRequested])
Returns the measured magnetic-field The magnetic field is a vector quantity. We may be interested in its individual components or its total magnitude.... in [Tesla]. If NumberOfMeasurementsRequested is not specified, you will receive a single measurement. NumberOfMeasurementsRequested cannot exceed 2048.
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 An NMR-controlled field is one whose strength is monitored by NMR. As opposed to NMR-regulated.... 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)
- Enter the VISA resource name. For the THM1176, the format should be similar to that shown in the illustration, except for the serial number (0000598 – the serial number is written on a label on the electronics housing, or displayed in Setup> Info in the THM1176 software). For other instruments, launch the National Instruments Measurement Automation Explorer (NI-MAX), and expand “Devices and Interfaces” in the left-hand column.
- Enter the VISA Query (or Command). Queries are commands that finish with “?”, and that return a result.
- Under “Operation”, select “Query” or “Command”. (Could be determined automatically, but we were being lazy.)
- Press the “Run” button.
- For a query, the result is displayed in the Result Buffer cell.
- The Status Byte (STB) after the read and write operations are also displayed.
- Run-time errors are displayed in a dialog.
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!
Perl, Python and Tcl
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 An NMR-controlled field is one whose strength is monitored by NMR. As opposed to NMR-regulated.... in scripts.
In addition, Python users can use PyVISA, a Python wrapper for VISA.