compile mex file with external dll. The function works fine but it seems it didn't stop.

1 view (last 30 days)
I'm using matlab 2015a and visual studio 2010 I was going to use BitScope with matlab. So I got bitlib.h, BitLib.lib, BitLib.dll and compiled mex file.
the code is this
if true
% code
#include "mex.h"
#include "bitlib.h"
#define MY_DEVICES 1 /* open one device only / #define MY_PROBE_FILE "" / default probe file if unspecified */
#define MY_DEVICE 0 #define MY_CHANNEL 0 #define MY_MODE BL_MODE_FAST
double MY_RATE = 1000000; /* capture sample rate / int MY_SIZE = 11000; / number of samples to capture */
int m; double d[11000]; /* let's get 500 samples */ void Test_Main(int nlhs, mxArray *plhs[]);
static void MyExit() { mexPrintf("bscmex.MyExit() called!\n"); /* Do cleanup here ... */ return; }
void mexFunction(int nlhs, mxArray plhs[], / Output variables / int nrhs, const mxArray *prhs[]) / Input variables / { double *pointer; / pointer to real data in new array */ int i = 0;
// plhs[0] = mxCreateNumericMatrix(MY_SIZE, 1, mxDOUBLE_CLASS, mxREAL); pointer = mxGetPr(prhs[0]); // plhs[0] = mxCreateNumericMatrix(MY_SIZE, 1, mxDOUBLE_CLASS, mxREAL); // pointer = mxGetPr(plhs[0]);
mexAtExit(MyExit); /* Register MyExit() to run when MEX?function is cleared */
mexPrintf("\nStarting : Test\n");
Test_Main(nlhs, plhs);
/* Create a local array and load data */ for ( i = 0; i < m; i++ ) { pointer[i] = d[i]; }
return;
}
void Test_Main(int nlhs, mxArray plhs[]) { / * Open and select the first channel on the first device. */ double check_rate; int check_sample_size; double check_range; int check_range_step; int check_coupling; int check_state; int n = MY_SIZE; int i = 0;
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
BL_Initialize();
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
mexPrintf("\nStarting : Attempting to open %d device%s...\n",MY_DEVICES,MY_DEVICES!=1?"s":""); if ( ! BL_Open(MY_PROBE_FILE,MY_DEVICE) ) { mexPrintf("Failed to find a devices.\n"); mexPrintf("Data acquisition complete. Dump Log...\n"); BL_Close(); return; } mexPrintf("BL_Open"); if ( BL_Select(BL_SELECT_DEVICE,MY_DEVICE) != MY_DEVICE ) { mexPrintf("Failed to select device %d.\n",MY_DEVICE); mexPrintf("Data acquisition complete. Dump Log...\n"); BL_Close(); return; } mexPrintf("BL_Select"); // if ( BL_Select(BL_SELECT_CHANNEL,MY_CHANNEL) != MY_CHANNEL ) { // mexPrintf("Failed to select channel %d.\n",MY_CHANNEL); // mexPrintf("Data acquisition complete. Dump Log...\n"); // BL_Close(); // return; // }
// mexPrintf("State : %s\n",BL_Name(0)); // mexPrintf("BL_Version(BL_VERSION_DEVICE ) : %s\n",BL_Version(BL_VERSION_DEVICE )); // mexPrintf("BL_Version(BL_VERSION_LIBRARY) : %s\n",BL_Version(BL_VERSION_LIBRARY )); // mexPrintf("BL_ID() : %s\n",BL_ID());
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
/*
* Prepare to capture one channel...
*/
if ( BL_Mode(MY_MODE) != MY_MODE ) {
mexPrintf("Failed to select mode %d.\n",MY_MODE);
mexPrintf("Data acquisition complete. Dump Log...\n");
BL_Close();
return;
}
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
check_rate = BL_Rate(MY_RATE); /* optional, default BL_MAX_RATE */
mexPrintf("Sampling rate Set to : %f Hz\n",check_rate);
check_sample_size = BL_Size(MY_SIZE); /* optional, default BL_MAX_SIZE */
// check_sample_size = BL_Size(MY_SIZE); /* optional, default BL_MAX_SIZE */
mexPrintf("Sample Size is Set to : %d \n",check_sample_size);
BL_Select(BL_SELECT_CHANNEL,MY_CHANNEL); /* choose the channel / BL_Trigger(BL_ZERO,BL_TRIG_RISE); / optional when untriggered / BL_Select(BL_SELECT_SOURCE,BL_SOURCE_POD); / use the POD input / check_range_step = BL_Count(BL_COUNT_RANGE); mexPrintf("Total Range Step : %d\n",check_range_step); check_range = BL_Range(3); / maximum range / mexPrintf("ADC Range Set to : %f V\n",check_range); // if(BL_Coupling(BL_COUPLING_AC)==1) // { // printf("Coupling Set to mode : %d V\n",BL_Coupling(BL_COUPLING_AC)); // } BL_Offset(BL_ZERO); / optional, default 0 / BL_Enable(1); / at least one channel must be initialised */
/* * Capture and acquire the data... */
mexPrintf("Trace: %d samples @ %.0fHz = %fs\n",BL_Size(BL_ASK),BL_Rate(BL_ASK), BL_Time(BL_ASK));
if ( BL_Trace(BL_TRACE_FORCED,BL_SYNCHRONOUS) ) { /* capture data (without a trigger) / i = 0; while ((BL_State() != BL_STATE_DONE) && i<100) { i++; } BL_Select(BL_SELECT_CHANNEL,MY_CHANNEL); / optional if only one channel */ m = BL_Acquire(n, d); mexPrintf("m : %d\n",m); }
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
mexPrintf("Data acquisition complete. Dump Log...\n");
mexPrintf("%s\n",BL_Log());
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
BL_Halt();
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
//
BL_Close();
check_state = BL_State();
mexPrintf("State : %d\n",check_state);
return;
}
end
it works when doing this
mex bscmex.c BitLib.lib
a = 1:11000;
bscmex(a);
The first problem is that when that is done i can't to anything else. It seems like the code is still running somewhere.
The Second problem is that I can run bscmex(a); only for a few times. When I run it aboutn 4~6times the matlab turns off itself.
I still think that all this is maybe memory problem. But I cannot solve this problem.
When I tried clear all after finished, the matlab stoped.
I really want to solve this problem. pease help me.

Answers (0)

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!