필터 지우기
필터 지우기

Dynamically sized IO ports in simulink

조회 수: 8 (최근 30일)
Benjamin Wang
Benjamin Wang 2011년 4월 14일
Hi,
I am working on a C-MEX s-function, which has 2 inputs and 1 output. All of the i/o ports are dynamically sized. The output port should always have the same size with the second input port.
Here are some fragments of my code.
/* ... */
static void mdlInitializeSizes(SimStruct *S)
{
... // Set two input and one output ports to dynamically sized
}
#if defined(MATLAB_MEX_FILE)
#define MDL_SET_INPUT_PORT_WIDTH
static void mdlSetInputPortWidth(SimStruct *S, int_T port, int_T inputPortWidth)
{
ssSetInputPortWidth(S, port, inputPortWidth);
/* This is very likely wrong */
if (1 == port) {
ssSetOutputPortWidth(S, port, inputPortWidth);
}
}
#define MDL_SET_OUTPUT_PORT_WIDTH
static void mdlSetOutputPortWidth(/* ... */)
{
/* I don't know how to set here */
}
static void mdlSetDefaultPortDimensionInfo(SimStruct *S)
{
ssSetInputPortWidth(S, 0, 10);
ssSetInputPortWidth(S, 1, 3);
ssSetOutputPortWidth(S, 0, 3);
}
#endif
/* ... */
It could be compiled with no error, but everytime I run the test simulink model with this s-function in it, MATLAB crashes.
Could someone tell me how to solve this, please?
Thanks in advance.
Ben

채택된 답변

MarkB
MarkB 2011년 4월 14일
The code above is mostly right, but there are some minor changes that you will need to make:
  • The calls to "mdlSetOutputPortWidth" and "mdlSetInputPortWidth" can occur for any port that was originally set as dynamically typed, even if was assigned a dimension later on. As a result, you should confirm that the output port is still dynamically typed before you assign the output port dimension in "mdlSetInputPortWidth". If it's still dynamically typed, assign it. If it isn't check to see if the output port dimensions match what you would have assigned. If not, throw an error.
  • The "mdlSetOutputPortWidth" should be a mirror-image/reverse of your "mdlSetInputPortWidth" function. People often mistakenly assume that dimensions only propagate "downstream", but it is entirely possible that your block's output port dimension gets assigned first. In this case, you should assign that dimension to the second input port as well.
  • "mdlSetDefaultPortDimensionInfo" gets called if any of the ports are still dynamically typed, so it is incorrect to assume that it means that all of the ports are still unspecified. Therefore, instead of just assigning dimensions to all of the ports, you should check each one to confirm that it is still dynamically typed, and only assign dimensions in that case.
  댓글 수: 1
Benjamin Wang
Benjamin Wang 2011년 4월 14일
Thank you Mark. The information you posted is very helpful. I've changed my code according to your suggestion.
The reason why my code got crashed was that I accidently initialized an 'int_T' type pointer with the return value of ssGetInputPortSignal, which actually is a 'real_T' type pointer. Then I used it to index some array and got pointer overflow. Did not see this until just now...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by