Post by CahitPost by CahitHi all,
In my simulink model, to split a double value into 8 uint8
values, i
Post by Cahitam trying to integrate a simple C Code into an S-function! It's
neither continous nor discrete case.
constant double x--> S-Function --> Display
For that, i am using S-Function Builder and giving all datas
what
Post by Cahitit
Post by Cahitneeds but if i want to build this, it gives me no reaction,
nothing
Post by Cahithappens!
My C Code is,
int transmit (double x,unsigned int y);
int transmit (double x, unsigned int y)
{
union ifas { double d;unsigned char u[8];};
union ifas member;
member.d = x ;
member.u[8] = y;
return y;
}
I also give the header files iostream.h, stdio.h etc in builder
Dialog box. I do not understand what is wrong?
Anyone who worked on such things before, i appreciate your
suggestions really. Thanks in advance!
Cheers,
Cahit
Cahit, you will need a for loop to assign the 8 uint8 values to your
output variable. Also, assuming 'y' is your output variable your
assignment is wrong. Shouldn't it read
y[i] = member.u[i];
Another thing you need to make sure is that your compiler does
support
chars. You can use ssPrintf to print out sizeof(unsigned char) and
sizeof(unsigned short), they should be 1 and 2 respectively. If it
doesn't you'll have to use unsigned shorts and do some shifting or else
use bit fields if those are supported.
If you could copy-paste your mdlInitializeSizes and mdlOutputs
functions I can help you out more with this problem.
HTH,
Ashish.
Hello Ashish,
I really want to thank you in advance for your consideration.
Yes, in your last message you were right, i made a mistake about
assignment of y.Now here is my actual C Code:
void double2integer(double x, unsigned char *y)
{
int i;
union ifas{double d; unsigned char uvalue[8];};
union ifas var;
var.d = x;
for (i=0 ; i<8 ; i++)
{
y[i]=var.uvalue[i];
}
}
int main()
{
unsigned char number[8];
double variable;
double2integer(variable,number);
return 0;
}
And then i just tried this code to adapt to Simulink S-Function. For
this purpose, i took one of C S-Function Templates from simulink/src
file, namely timestwo.c as an example!
It is actually hard for me to adapt this code as template has macros
used with pointers which confuse me.Therefore, i have things about
which i am really concerned.
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return;}
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
// ATTENTION : OutputPortWidth, should i leave it like this
(Dynamically Sized) or submit [8 1] as we expect 8 set of usign8
array?
ssSetNumSampleTimes(S, 1);
/* Take care when specifying exception free code - see
sfuntmpl_doc.c */
ssSetOptions(S,
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR);
}
My mdlOutputs Function:
static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);
// ATTENTION : Again width here.
union ifas { double d ; unsigned char uvalue[8] ; } ;
union ifas var;
var.d = *uPtrs[i];
for (i=0; i<8; i++) // width = 8
{
*y[i]=var.uvalue[i];
}
}
Here, as help menu says, *uPtrs[i] is to access input datas and
similarly *y for output.How about the 'main' function in original C
Code?
To be honest, i could not have time to check it whether it is working
as i have to leave the work now but still wanted to send you to save
time because of time difference. I of course strongly appreciate what
you think or suggest about my code. Thank you very much.
Regards,
Cahit