1

With Matlab R2015b on 32-bit Windows 7, LOADLIBRARY was working fine:

library = 'C:\Windows\System32\visa32.dll';
headerFile  = 'C:\Program Files\IVI Foundation\VISA\WinNT\include\visa_Matlab.h';
loadlibrary(library, headerFile, 'alias','visa32');

and I can use functions in 'visa32.dll', such as,

defaultRm = 0;
[err, defaultRm] = calllib('visa32', 'viOpenDefaultRM', defaultRm);

vi = 0;
InstrAddr = 'GPIB0::29::INSTR';
pInstrAddr = libpointer('int8Ptr', [int8( InstrAddr ) 0] );
[err, tempVar, vi] = calllib('visa32', 'viOpen', defaultRm, pInstrAddr, 0, 0, vi);

to open instrument's communication port...

But after I have upgraded to 64-bit Windows 7 and Matlab R2017b, LOADLIBRARY won't work any more, even I have use 62-bit versions of 'visa32.dll' and 'visa.h'. There are two problems:

(1) Firstly, Matlab complaints about not have the right compiler installed and can't even run LOADLIBRAY. Follow their online instruction to install the MinGW-w64 Compiler addon. This problem seems to have revolved.

(2) LOADLIBRAY seems to be able to run, but then it has the following error:

Failed to parse type '( fastcall )) viOpenDefaultRM ( ViPSession vi' original input '( fastcall )) viOpenDefaultRM ( ViPSession vi ' Found on line 217 of input from line 93 of file C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Include\visa.h Error parsing argument for function attribute function may be invalid. ...

I wonder if somebody could help please. Many thanks in advance!

  • A 32bit DLL [can't be loaded](https://stackoverflow.com/questions/2265023/load-32bit-dll-library-in-64bit-application) in a 64bit application. – rahnema1 Jul 17 '18 at 10:55
  • Hi rahnema1, Though it's called 'visa32.dll', but I believe it's a 64-bit DLL, because it was installed by the latest NI 488.2 driver and resides in 'C:\Windows\System32\visa32.dll'. – user5266266 Jul 18 '18 at 12:22
  • To be sure you can [test](https://stackoverflow.com/questions/495244/how-can-i-test-a-windows-dll-file-to-determine-if-it-is-32-bit-or-64-bit) if it is 32bit or 64bi. – rahnema1 Jul 18 '18 at 12:31
  • A big THANKS, rahnema1. You have got me thinking. I have download a 64-bit version of 'visa32.dll' from https://www.azdll.net/files/visa32-dll.html. It work. – user5266266 Jul 18 '18 at 15:12
  • Now LOADLIBRARY seems to work, but there are some warning messages, such as the following: Warning: The data type 'FcnPtr' used by function viInstallHandler does not exist. Warning: The data type 'error' used by function viPrintf does not exist. Warning: The data type '__builtin_va_list' used by function viVPrintf does not exist. There are few more, but are all similar. They all complain about these three data types not exist. Why? – user5266266 Jul 18 '18 at 15:20
  • I have made a mistake in my post, 32-bit 'visa32.dll' resides in 'C:\Windows\System32', while the 64-bit version is in 'C:\Windows\SysWOW64'. I copied the wrong link. I did use the one in 'C:\Windows\SysWOW64'. – user5266266 Jul 18 '18 at 15:21
  • I have no idea about errors but you can search the header files for such types. – rahnema1 Jul 18 '18 at 17:39
  • 1
    Thanks again @rahnema1. Appreciate your help. – user5266266 Jul 19 '18 at 09:17

1 Answers1

0

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');