Error calling char variable from C code by function block of Simulink
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello all again;
I want to ask for your help to understand this problem. I need to make a converter from float value to string, to be send by serial transmit of my Tiva C launchpad. Im trying to make the conversion in a simulink function block using the sprintf function.
During the debug in the Code Compiler Studio it works fine, I only receive a warning that the return variable are not compatible with the format, but the value gets saved to the char variable.
The problem ocurr with Matlab, because regardless the value of the constant, the block always give me the value of -128; I was searching for hours, but i can't found any solution.
This is the C code:
#include <stdio.h>
#include "dobloh.h"
double u;
char doble(double u)
{
static char car[2000];
sprintf(car,"%.3f",u);
return car;
}
"dobloh.h" Header:
#ifndef DOBLADOR_C_
#define DOBLADOR_C_
char doble(double u);
#endif /* DOBLADOR_C_ */
Function block code:
function y = callingDoblez(u)
%#codegen
y=0.0;
y = coder.ceval('doble',u);
Diagram:
I hope you can help me to understand the problem,
Thanks for your time
Regards
Alberto
0 comentarios
Respuestas (2)
Venkatachala Sarma
el 6 de Mayo de 2016
Hi Alberto,
'coder.ceval' should be used only for code generation. For simulation, you will have write your own algorithm.
0 comentarios
Mark McBroom
el 8 de Mayo de 2016
Coder.feval can be used for simulation and code generation. The problem is that your function is supposed to return a single char, but in the c function you are returning a vector, which means the address of car is getting returned. That is likely the cause of the compiler warning. If you only want the first char to be returned, change your code to:
return car[0];
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!