Main Content

Write an Output Value

Suppose you need to write the value of the output signal to the output port in your S-function. You should write your code so that the pointer to the output value is properly typed. To do this, you can use these steps, which are followed in the example code below:

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

  2. Get the data type ID of the output port using ssGetOutputPortDataType.

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

  4. Have a case for each output 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 write the value by dereferencing the new, fully typed pointer (see b and d).

For example,

static void mdlOutputs(SimStruct *S, int_T tid)
{
    <snip>

    void *pVoidOut = ssGetOutputPortSignal( S, 0 ); (1)

    DTypeId dataTypeIdY0 = ssGetOutputPortDataType( S, 0 ); (2)
      
    fxpStorageContainerCategory storageContainerY0 =
					ssGetDataTypeStorageContainCat( S, 
					dataTypeIdY0 ); (3)

    switch ( storageContainerY0 )
    {
      case FXP_STORAGE_UINT8: (4)
        {
            const uint8_T *pU8_Properly_Typed_Pointer_To_Y0; (a)

            uint8_T u8_Stored_Integer_Y0; (b)

          <snip: code that puts the desired output stored integer
				value in to temporary variable u8_Stored_Integer_Y0>
					
            pU8_Properly_Typed_Pointer_To_Y0 = 
					(const uint8_T  *)pVoidOut; (c)

            *pU8_Properly_Typed_Pointer_To_Y0 = 
					u8_Stored_Integer_Y0; (d)
            
        }
        break;

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

            int8_T s8_Stored_Integer_Y0; (b)

          <snip: code that puts the desired output stored integer
				value in to temporary variable s8_Stored_Integer_Y0>
					
            pS8_Properly_Typed_Pointer_To_Y0 = 
					(const int8_T  *)pVoidY0; (c)

            *pS8_Properly_Typed_Pointer_To_Y0 = 
					s8_Stored_Integer_Y0; (d)
            
        }
        break;

    <snip>

Related Topics