MATLAB is crashing down while building s-function

6 views (last 30 days)
According to a project, we have to write a s-function. Of course we are very new to Matlab and there are problems.
We are trying to write a s-function, wich is able to communicate with xPC Target. So we wrote our C code and we are trying to get the application started. At first we want a very simple program. It takes a constant from a constant-block an adds or multiplies another constant to it and returns the value. In this case we have one input-port and one output-port dynamically sized. While compiling the s-function there are no problems, but as soon as we want to build it for xPC MATLAB crashes down and we have to restart it.
The second problem is, our library doesn't appear in the simulink library. We've tried to add the path to MATLAB and by copy it into the folder "thirdpartydrivers" but none of this solutions we read works. So we have to drag and drop our s-function-block from our library.
The main-goal of this project is it, to write a s-function-block and add it to a consisting Simulink/Stateflow-model to communicate with a security-system. This system communicates with 9Bit. Our idea is it to read the parity-bit directly from the stack and send it, to know if the message wich was send is from the master or the slave. In order to realize it, we tried to change the code of consisting MATLAB-send and receive code.
Do you guys have any idea how to fix these problems? And can you tell me, if we are on the right way or what we can change to get to good result? We wrote the following code as good as we could, so please have a look.
Many thanks in advance
#define S_ FUNCTION_LEVEL 2
#undef S_FUNCTION_NAME
#define S_FUNCTION_NAME s_function_test
#include <stddef.h>
#include <stdlib.h>
#include "tmwtypes.h"
#include "simstruc.h"
#ifdef MATLAB_MEX_FILE
#include "mex.h"
#else
#include <windows.h>
#include <string.h>
#include "xpcimports.h"
#endif
#define MDL_INITIALIZE_SAMPLE_TIMES mdlInitializeSampleTimes()
#define MDL_SET_INPUT_PORT_DIMENSION_INFO mdlSetInputPortDimensionInfo()
#define MDL_SET_OUTPUT_PORT_DIMENSION_INFO mdlSetOutputPortDimensionInfo()
#define MDL_SET_INPUT_PORT_DATA_TYPE mdlSetInputPortDataType()
#define INPUT 1
#define OUTPUT 1
#define PORT_ARG ssGetSFcnParam(S, 0)
#define sampleTimeIndex 1
#define offsetTimeIndex 1
#define sample_time 0.01
#define offset_time 0.01
void mdlInitializeSizes(SimStruct *S)
{
int_T inputPortIdx =1;
int_T outputPortIdx = 1;
if(!ssSetNumInputPorts(S, 1)) return;
if(!ssSetNumOutputPorts(S, 1)) return;
ssSetInputPortDimensionInfo(S, 0, DYNAMIC_DIMENSION);
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortDataType(S, 0, DYNAMICALLY_TYPED );
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetOutputPortDimensionInfo(S, outputPortIdx, DYNAMIC_DIMENSION);
ssSetOutputPortDataType(S, 0, DYNAMICALLY_TYPED );
}
void mdlSetInputPortDataType(SimStruct *S, int_T port, DTypeId id)
{
if(id!= -1){
ssSetInputPortDataType(S, 0, DYNAMICALLY_TYPED);
if(!ssSetInputPortDataType(S, 0,DYNAMICALLY_TYPED)){
ssSetErrorStatus(S, "Input-Port-Type Error");
return;
}
}
}
void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, sampleTimeIndex, sample_time);
ssSetOffsetTime(S, offsetTimeIndex, offset_time);
}
void mdlSetInputPortDimensionInfo(SimStruct *S, int_T port,const DimsInfo_T *dimsInfo)
{
DimsInfo_T dims[1];
SimStruct width;
int_T numDims;
width = *S;
numDims = port;
*dims = *dimsInfo;
if(numDims != INPUT)
{
ssSetErrorStatus(S, "Fehler bei Input-Port Groesse\n");
return;
}
}
void mdlSetOutputPortDimensionInfo(SimStruct *S, int_T port,const DimsInfo_T *dimsInfo)
{
DimsInfo_T dims[1];
SimStruct width;
int_T numDims;
width = *S;
numDims = port;
*dims = *dimsInfo;
if(numDims != OUTPUT)
{
ssSetErrorStatus(S, "Fehler am Output-Port...Groesse\n");
return;
}
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
int *current = ssGetIWork(S);
int *recLength = ssGetIWork(S) + 1;
int port = (int *)mxGetData(PORT_ARG);/*[0] -1;*/
int buffer, count;
if(mxGetData == 0)
{
printf("No Data available\n");
return;
}
if(*current == 0)
*recLength = (int)ssGetInputPortRealSignal(S, 0)[0];
while(current != 0 )
{
buffer += port;
}
count += buffer;
printf("Data from constant-block:%d\n", count);
}
static void mdlTerminate(SimStruct *S)
{
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
  2 Comments
Suneesh
Suneesh on 11 Aug 2013
Was "S_ FUNCTION_LEVEL" a copy-paste error?
Kaustubha Govind
Kaustubha Govind on 12 Aug 2013
Did you mean that MATLAB crashes when running the S-function (and not building/compiling it)? If the crash happens at execution, could you please try debugging your S-function to figure out which line the crash happens at.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!