The advantages of using VISA library in MATLAB for instrument control are: direct (i.e. faster) access to ports, be able to use GTL (go-to-local) and handles communication error better, etc. Use LOADLIBRARY to load the VISA library and CALLLIB to access the functions in the loaded library, e.g.
loadlibrary('visa32.dll','visa.h','alias','visa32');
[err, defaultRm] = calllib('visa32', 'viOpenDefaultRM', defaultRm);
However, limitations in LOADLIBRARY have caused a lot of trouble, and it works differently for different Windows architecture. Here is the solution that has been tested on 32- and 64-bit Windows:
1. C++ compiler in MATLAB:
a. This only affects 64-bit MATLAB;
b. A MATLAB add-on called ‘MinGW-w64 Compiler’ must be installed:
i. this is relatively easy for R2017b, just use MATLAB’s Add-Ons Manager;
ii. for MATLAB version prior to R2017b, it is very difficult:
Download and install 'MinGW-w64' from:
tdm-gcc.tdragon.net/download
sourceforge.net/projects/tdm-gcc/postdownload?source=dlp
sourceforge.net/projects/mingw/files/
Then configure MATLAB as described in:
de.mathworks.com/help/matlab/matlab_external/compiling-c-mex-files-with-mingw.html
de.mathworks.com/help/matlab/matlab_external/install-mingw-support-package.html
de.mathworks.com/matlabcentral/answers/313298-i-already-have-mingw-on-my-computer-how-do-i-configure-it-to-work-with-matlab
2. VISA library file:
a. Different DLL file for different Windows architecture;
b. The library file is available after the installation of NI-488.2 driver (the latest version to date is 'NI4882_1760f0.exe';
c. For 32-bit Windows and 32-bit MATLAB:
i. Library file: ‘visa32.dll’
ii. Location: ‘C:\Windows\System32\’
d. For 64-bit Windows and 64-bit MATLAB:
i. Library file: ‘visa64.dll’
ii. Location: ‘C:\Windows\System32\’
iii. Notes:
1. According to NI’s website, the 64-bit VISA DLL is ‘visa32.dll’ that resides in 'C:\Windows\SysWOW64. This is not right;
2. Alternatively, a 64-bit version of ‘visa32.dll’, which is smaller than ‘visa64.dll’ installed by NI-488.2 driver, can be downloaded from: https://www.azdll.net/files/visa32-dll.html.
3. Header files:
a. Two headers files are needed to load the VISA library: ‘visa.h’ and ‘visatype.h’;
b. Both are available after the installation of NI-488.2 driver;
c. For 32-bit Windows, both files are in:
‘C:\Program Files\IVI Foundation\VISA\Win64\include’
d. For 64-bit Windows, both files are in:
‘C:\Program Files (x86)\IVI Foundation\VISA\WinNT\include’
e. ‘visatype.h’ can be used as it is;
f. ‘visa.h’ must be modified for MATLAB’s LOADLIBRARY. Changes made on ‘visa.h’ are:
i. Add the following at the beginning of the header file:
#ifdef _WIN64
#define __fastcall
#endif
ii. Mask out 'viInstallHandler' and 'viUninstallHandler', as they are not commonly used;
iii. Remove ', ...' from the following functions as it’s not supported by LOADLIBRARY (https://de.mathworks.com/matlabcentral/answers/103180-why-do-i-get-warnings-when-loading-a-dll-with-loadlibrary-in-matlab-7-13-r2011b):
viPrintf, viSPrintf, viScanf, viSScanf, viQueryf
iv. Replace 'ViVAList' with 'ViConstBuf' in the following functions:
viVPrintf, viVSPrintf, viVScanf, viVSScanf, viVQueryf
v. Replace the 'viVxiServantResponse' function with the following:
#ifndef _WIN64
ViStatus _VI_FUNC viVxiServantResponse(ViSession vi, ViInt16 mode, ViUInt32 resp);
#endif
g. The modified ‘visa.h’, renamed as ‘visa_Matlab.h’, can be downloaded from visa_Matlab.h.
Example:
% ‘visa32.dl’ or ‘visa64.dll’, ‘visa_Matlab.h’ and ‘visatype.h’ must be
% available in the local directory
archstr = computer('arch');
if ~isempty(strfind(archstr, 'PCWIN64')) || ~isempty(strfind(archstr, 'win64'))
dllName = 'visa64';
else
dllName = 'visa32';
end
headerFileName = 'visa_Matlab.h';
loadlibrary ([dllName '.dll'], 'visa_Matlab.h','alias',dllName);
visaFunctions = libfunctions(dllName,'-full');