Main Content

Interpret an Input Value

Suppose you need to get the value of the signal on your input port to use in your S-function. You should write your code so that the pointer to the input value is properly typed, so that the values read from the input port are interpreted correctly. To do this, you can use these steps, which are shown in the example code below:

  1. Create a void pointer to the value of the input signal.

  2. Get the data type ID of the input port using ssGetInputPortDataType.

  3. Use the data type ID to get the storage container type of the input.

  4. Have a case for each input storage container type you want to handle. Within each case, you will need to perform the following in some way:

    • Create a pointer of the correct type according to the storage container, and cast the original void pointer into the new fully typed pointer (see a and c).

    • You can now store and use the value by dereferencing the new, fully typed pointer (see b and d).

For example,

static void mdlOutputs(SimStruct *S, int_T tid)
{
    const void *pVoidIn = 
					(const void *)ssGetInputPortSignal( S, 0 ); (1)

    DTypeId dataTypeIdU0 = ssGetInputPortDataType( S, 0 ); (2)
      
    fxpStorageContainerCategory storageContainerU0 =
					ssGetDataTypeStorageContainCat( S, dataTypeIdU0 ); (3)

    switch ( storageContainerU0 )
    {
      case FXP_STORAGE_UINT8: (4)
        {
            const uint8_T *pU8_Properly_Typed_Pointer_To_U0; (a)

            uint8_T u8_Stored_Integer_U0; (b)

            pU8_Properly_Typed_Pointer_To_U0 = 
					(const uint8_T  *)pVoidIn; (c)

            u8_Stored_Integer_U0 = 
					*pU8_Properly_Typed_Pointer_To_U0; (d)
            
            <snip: code that uses input when it's in a uint8_T>
        }
        break;

      case FXP_STORAGE_INT8: (4)
        {
            const int8_T *pS8_Properly_Typed_Pointer_To_U0; (a)

            int8_T s8_Stored_Integer_U0; (b)

            pS8_Properly_Typed_Pointer_To_U0 = 
					(const int8_T  *)pVoidIn; (c)

            s8_Stored_Integer_U0 = 
					*pS8_Properly_Typed_Pointer_To_U0; (d)
            
            <snip: code that uses input when it's in a int8_T>
        }
        break;

Related Topics