More info on What I am trying to do: (This S-Function Creates a Data Class in mdlStart and Passes it to mdlOutputs)
static void mdlOutputs(SimStruct *S, int_T tid)
{
#ifndef MATLAB_MEX_FILE
uint32_T *p_DXCallBack = (uint32_T *) ssGetOutputPortRealSignal(S,0);
DX2004_DDC *DX =(DX2004_DDC *) ssGetPWork(S)[0]; //Re-Create DX from P-Work Variable
p_DXCallBack = (uint32_T *) DX;
printf("DX Knows:%x %x %x ",DX, DX->IOAddress[0], p_DXCallBack);
#endif
}
The goal is to put a pointer to the class out on p_DXCallBack Output Pin
In a different S-Function I want to take this on an input pin and use the Class data and Functions. See Below.
static void mdlOutputs(SimStruct *S, int_T tid)
{
uint32_T *p_DXCallBack = (uint32_T *) ssGetInputPortSignal(S,0);
// void *p_DXCallBack = (void *) ssGetInputPortSignal(S,0);
if (p_DXCallBack != NULL)
{
DX2004_DDC *DX = (DX2004_DDC *) p_DXCallBack;
printf("Wants %x %x %x\n", DX, p_DXCallBack, DX->IOAddress[0]);
}
else
{
printf("NULL \n");
}
}
However somewhere in here there is a problem as the data that i set in the first S-Function is not coming across correctly into the second S-Function. Any suggestions?
http://www.tim.cz/en/nfaq/matlab-simulink/pointer-transfer.php
Phil.
You get a GOLD STAR Phil, this worked out. To test it out quickly I wan't able to use the datatypes with RTW so I went without that extra safety check but it fired up and is working.
Here is what I did,
This is the S-Function that created the pointer: (my class is DX2004_DDC) The syntax was a little off from what we tried the last few days but this works.
#include "DX2004_DDC.h"
static void mdlInitializeSizes(SimStruct *S)
{
if (!ssSetNumOutputPorts(S, 1)) return;
/* Output Port 0 */
ssSetOutputPortWidth(S, 0, 1); //Pointer Output Pin - Nothing else needed
ssSetNumPWork(S, 1); // Memory Space for Pointer to DX Class
}
static void mdlStart(SimStruct *S)
{
DX2004_DDC *DX = new DX2004_DDC; //Create Instance of DX2004
ssGetPWork(S)[0] = (void *) DX;
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
DX2004_DDC *DX =(DX2004_DDC *) ssGetPWork(S)[0]; //Re-Create DX from P-Work Variable
real_T *p_DXCallBack = ssGetOutputPortRealSignal(S,0);
p_DXCallBack[0] = (long int) DX;
DX->IOAddress[0] = 35; //Populate to see if it works
}
static void mdlTerminate(SimStruct *S)
{
DX2004_DDC *DX =(DX2004_DDC *) ssGetPWork(S)[0]; //Re-Create DX from P-Work Variable
delete DX;
}
This is a S-Function that uses the pointer:
#include "DX2004_DDC.h"
if (!ssSetNumInputPorts(S, 1)) return;
/* Input Port 0 */
ssSetInputPortWidth(S, 0, 1); //Pointer Pin - Nothing else needed for this pin
ssSetNumPWork(S, 1); // Memory Space for Pointer to DX Class
// Block update
#define MDL_UPDATE
static void mdlUpdate(SimStruct* S, int_T tid)
{
// convert double from input to pointer
InputRealPtrsType u = ssGetInputPortRealSignalPtrs(S,0);
DX2004_DDC *DX = (DX2004_DDC*)((long int)(*u[0]));
ssGetPWork(S)[0] = (void *)DX;
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
DX2004_DDC *DX = (DX2004_DDC *) ssGetPWork(S)[0];
if (DX == 0)
printf("Fail");
else
printf("Wants %d \n", DX->IOAddress[0]); //Prints 35 to the screen
}