1

我正在尝试创建一个用 C 语言编写的 2 级 S-Function,它具有不同数量的输出端口,具体取决于连接到输入端口的信号数据类型。

实际背景是我有一些其他的 S-Function,它们通过调用来定义自定义数据类型ssRegisterDataType。这些数据类型描述了指向某些数据的指针。

我现在想要另一个可以使用这些自定义数据类型(指针)的 S-Function,并根据输入的指针类型输出某些数据。

ssSetNumOutputPorts我尝试通过设置输出端口的数量

static void mdlSetInputPortDataType(SimStruct *S, int portIndex,DTypeId dType)
{
    if( portIndex == 0 )
    {
        if(      dType == ssGetDataTypeId(S, "ptrtype1" ) )
        {
            if (!ssSetNumOutputPorts(S, nOutportsPtr1)) return;
        }
        else if( dType == ssGetDataTypeId(S, "ptrtype2" ) )
        {
            if (!ssSetNumOutputPorts(S, nOutportsPtr2)) return;
        }
        else if( dType == ssGetDataTypeId(S, "ptrtype3" ) )
        {
            if (!ssSetNumOutputPorts(S, nOutportsPtr3)) return;
        }
        else if( dType == ssGetDataTypeId(S, "ptrtype4" ) ) 
        {
            if (!ssSetNumOutputPorts(S, nOutportsPtr4)) return;
        }
        else
        {
            ssSetErrorStatus(S, "Input data type is not supported."); return;
        }


        if (!ssSetInputPortDataType(S, portIndex, dType)) return;
    }
} /* mdlSetInputPortDataType */

mdlInitializeSizes我将 inport 的数据类型设置为DYNAMICALLY_TYPED.

但是 Simulink 一直告诉我我应该在mdlSetInputPortDataType调用ssSetNumOutputPorts. 我假设在这个函数内部只有输入端口的数据类型可以改变。

有人知道这个问题的解决方案吗?我能想到的唯一解决方法是屏蔽 S-Function 并让用户手动选择他希望处理的指针类型。但是,对于数据类型传播,这似乎是混乱且不必要的。

4

1 回答 1

2

您需要在 mdlInitializeSizes 中设置端口数。在那个函数之后你不能改变它。唯一的解决方法是你提到的那个。

于 2012-12-06T14:40:40.173 回答