Specific problem usining the mex-function with a library by 'Thorlabs'

3 views (last 30 days)
Hi everybody,
I hope, you can help me with my pretty specific problemn.
I have to control some Thorlabs-Hardware (WFS 150C) with Matlab. There is a example-code in C, which I can compile and use and which works just fine.
So my intention is to use this example-code and integrate it into Matlab via mex. This worked somehow, here my code right now:
#include "wfs_drv.h" // Wavefront Sensor driver's header file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mex.h"
/*===============================================================================================================================
Defines
===============================================================================================================================*/
//Notwendig für MEX
#define WFS_close
#define WFS_ErrorMessage
#define WFS_RevisionQuery
/*===============================================================================================================================
Data type definitions
===============================================================================================================================*/
typedef struct
{
char version_wfs_driver[WFS_BUFFER_SIZE];
char version_cam_driver[WFS_BUFFER_SIZE];
int handle;
} instr_t;
/*===============================================================================================================================
Function Prototypes + Global Variables
===============================================================================================================================*/
void handle_errors (int);
instr_t instr = { 0 }; // all instrument related data are stored in this structure
/*===============================================================================================================================
Code
===============================================================================================================================*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int err;
// Get the driver revision
if(err = WFS_RevisionQuery (NULL, instr.version_wfs_driver, instr.version_cam_driver)) // pass NULL because handle is not yet initialized
handle_errors(err);
printf("Camera USB driver version : %s\n", instr.version_cam_driver);
printf("WFS instrument driver version : %s\n\n", instr.version_wfs_driver);
}
/*===============================================================================================================================
Handle Errors
This function retrieves the appropriate text to the given error number and closes the connection in case of an error
===============================================================================================================================*/
void handle_errors (int err)
{
char buf[WFS_ERR_DESCR_BUFFER_SIZE];
if(!err) return;
// Get error string
WFS_ErrorMessage (instr.handle, err, buf);
if(err < 0) // errors
{
printf("\nWavefront Sensor Error: %s\n", buf);
// close instrument after an error has occured
printf("Sample program will be closed because of the occured error, press <ENTER>.");
WFS_close(instr.handle); // required to release allocated driver data
fflush(stdin);
getchar();
exit(1);
}
}
I removed a lot of the code to keep it as simple as possible.
This is the mex-command I use: mex minimal_mex.c -v -largeArrayDims -IC:\Users\[...] -lWFS_Drv -LC:\Users\[...]
Having exactly the code shown above, there is one warning:
minimal_mex_forum.c(43) : warning C4047: '=' : 'int' differs in levels of indirection from 'char *'
I'm not understanding this one a 100%, but when I startet the mexed function, Matlab crashed. I found out how to remove this warning as well, just change the following lines:
if(err = WFS_RevisionQuery (NULL, instr.version_wfs_driver, instr.version_cam_driver)) // pass NULL because handle is not yet initialized
handle_errors(err);
into
WFS_RevisionQuery (NULL, instr.version_wfs_driver, instr.version_cam_driver);
With this minor change, the mexed code runs, but does not work, because the output keeps empty.
Here the description of WFS_RevisionQuery:
Quote:
_WFS_RevisionQuery
int WFS_RevisionQuery (WFS_hdl, char instrumentDriverRevision[], char camDriverRevision[]);
Purpose
This function returns the revisions of the WFS instrument driver and camera driver.
Parameters
Input
Name Type Description
instrumentHandle WFS_hdl
This parameter accepts the instrument handle returned by function init to select the desired instrument driver session. You may also pass NULL. In this case only a valid instrument driver revision is returned.
Output
Name Type Description
instrumentDriverRevision char []
This parameter returns the WFS Instrument Driver software revision.
Note: The string must contain at least WFS_BUFFER_SIZE (256) elements (char[WFS_BUFFER_SIZE]).
Revision is coded for example: "3.0"
camDriverRevision char []
This parameter returns the Camera Driver Revision the WFS instrument driver is calling. A valid instrument handle is required for this.
Format:
major.minor.built_no for WFS150/WFS300 series instruments
major.minor for WFS10 series instruments
Note: The string must contain at least WFS_BUFFER_SIZE (256) elements (char[WFS_BUFFER_SIZE]).
Return Value
Name Type Description
status int
This value shows the status code returned by the function call.
For Status Codes see function ErrorMessage._
The same code, but without the mex-specific changes, compiles and works just fine under c.
So now I would guess, that mex has some problem with the library, but I can't say which. Is someone able to help me?
One apprehension is, that I failed at one point. Those lines:
#define WFS_close
#define WFS_ErrorMessage
#define WFS_RevisionQuery
were included by me, because otherwise compiling stopped with errors like this one:
minimal_mex.obj : error LNK2019: unresolved external symbol WFS_ErrorMessage referenced in function handle_errors
Is it possible that the compiling worked this way, but these functions have no sense anymore? On the other side seems everything allright with the library, because Matlab finds it during the mex-operation.
My specification: Windows 7 Enterprise (64 bit) Matlab: R2013b Compiler: Microsoft Software Development Kit (SDK) 7.1

Answers (3)

Ken Atwell
Ken Atwell on 5 Sep 2014
From the documentation to WFS_RevisionQuery you copy/pasted, it returns a character string -- the warning is happening because you are assigning it to an int.
From there, if you want to return that string to MATLAB, you will need to wrap it in an mxArray using an API like mxCreateString. Likewise, if you later want to pass data form MATLAB to C, you will need to unpack that data from an mxArray. Finally, look toward mexPrintf if you want to display output in the MATLAB Command window. Basically, you are going to have to develop a level of proficiency in the "mx" and "mex" APIs to be successful.
If that sounds like too much work and you have a DLL for the library you are using, loadlibrary might be a quick-and-dirty way forward.
  1 Comment
Daniel
Daniel on 5 Sep 2014
Sry, I saw the 'Comment'-feature too late, I wrote a full reply. Not sure If you get a notification about it, so I'll comment quick :P

Sign in to comment.


Daniel
Daniel on 5 Sep 2014
Hi Ken,
thanks for your reply! For now, it would be enough for me to display the correct numbers, a transfer to MATLAB is the next step. I've tried the mexPrintf-command as well, but it did not work as well.
When I understood the documentation of WFS_RevisionQuery correct, the first err should be a int as well, because there is the status saved. The "instr.version_cam_driver" and "instr.version_wfs_driver" variables are defined as chars.
I've tried 'loadlibrary' as well, but this did not work as well. My code:
function [] = matlab_test()
addpath('C:\Users\[...]')
libname = 'WFS_Drv'
hfile = 'WFS_Drv'
loadlibrary (libname,hfile)
end
There were error like:
Warning: Warnings messages were produced while parsing. Check the functions you intend to use
for correctness. Warning text can be viewed using:
[notfound,warnings]=loadlibrary(...)
> In loadlibrary at 343
In matlab_test at 8
Error loading library intermediate output follows.
The actual error is at the end of this output.
*********
Type 'int__fastcall' was not found. Defaulting to type error.
Found on line 347 of input from line 346 of file C:\\Users\\[...]
and afterwards (in red):
*********
Error using loadlibrary (line 418)
Building WFS_Drv_thunk_pcwin64 failed. Compiler output is:
cl -I"C:\Program Files\MATLAB\R2013b\extern\include" /W3 /D_CRT_SECURE_NO_DEPRECATE
/D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /nologo
-I"C:\Users\kpg-db\Desktop\C_Programmierung\MATLAB-Test"
-I"C:\Users\kpg-db\Desktop\C_Programmierung\MATLAB-Test" "WFS_Drv_thunk_pcwin64.c" -LD
-Fe"WFS_Drv_thunk_pcwin64.dll"
WFS_Drv_thunk_pcwin64.c
WFS_Drv_thunk_pcwin64.c(33) : error C2143: syntax error : missing ')' before '*'
WFS_Drv_thunk_pcwin64.c(33) : error C2066: cast to function type is illegal
WFS_Drv_thunk_pcwin64.c(33) : error C2143: syntax error : missing ')' before '*'
WFS_Drv_thunk_pcwin64.c(33) : error C2059: syntax error : ')'
WFS_Drv_thunk_pcwin64.c(42) : error C2143: syntax error : missing ')' before '*'
Error in matlab_test (line 8)
loadlibrary (libname,hfile)
The Thorlabs-support just told me the following:
For the wave front sensor: we provide a .NET library as well as Labview Vis. You should be able to call the .NET library in Matlab. All files can be downloaded from our website here: http://www.thorlabs.de/software_pages/ViewSoftwarePage.cfm?Code=WFS. The files Thorlabs.WFS.Interop.dll and Thorlabs.WFS.Interop.xml get saved on your PC under the following path: C:\Program Files\Microsoft.NET\Primary Interop Assemblies.
But I'm not really sure how to implement that one in MATLAB - I've tried the following:
function [] = matlab_test()
addpath('C:\Users\kpg-db\Desktop\C_Programmierung\MATLAB-Test')
status1 = char
status2 = char
xDoc = xmlread('Thorlabs.WFS.XML')
xmlwrite(xDoc)
xRoot = xDoc.WFS_RevisionQuery (NaN,status1,status2)
end
which did not work as well - the xmlwrite-command worked, but then I got the error:
No appropriate method, property, or field WFS_RevisionQuery for class org.apache.xerces.dom.DeferredDocumentImpl.
Error in matlab_test (line 18)
xRoot = xDoc.WFS_RevisionQuery (NaN,status1,status2)
Then I tried the "asmInfo = NET.addAssembly('Thorlabs.WFS')" command, but this one does not work as well.
Any other ideas?
Kind regards, Daniel

Lukas
Lukas on 14 Nov 2017
Hi Daniel Did you find a solution to control the Thorlabs WFS 150C via MATLAB?

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!