Specific problem usining the mex-function with a library by 'Thorlabs'
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (3)
Ken Atwell
am 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.
Lukas
am 14 Nov. 2017
Hi Daniel Did you find a solution to control the Thorlabs WFS 150C via MATLAB?
0 Kommentare
Siehe auch
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!