cancel
Showing results for 
Search instead for 
Did you mean: 

Issues writing float data to UART

JayDev
Senior II

Hello everyone!

Ran into some issues writing float data to the UART. I know with Nordic, there was some weird workaround that had to be used to convert it that looked something like this:

 NRF_LOG_INFO("MLX90393 X = " NRF_LOG_FLOAT_MARKER "\r", NRF_LOG_FLOAT(*x));

I wasn't sure if there was some other trick for this, if there's a project related setting that is required to make use of floating point (in case it's disabled by default or something to that effect, etc). I tried finding one but didn't seem to find one in the project options.

Anyway, this is my current attempt at resolving the issue:

void write_mlx90393_to_uart(float x, float y, float z)
{
	x = 250.3;
	y = -112.3;
	z = 15.214;
 
	char x_str[30];
	char y_str[30];
	char z_str[30];
 
	sprintf(x_str, "X: %f", x);
	sprintf(y_str, "Y: %05.2f \n", y);
	sprintf(z_str, "Z: %05.2f \n", z);
 
	debugPrintln(&huart1, x_str);
	debugPrintln(&huart1, y_str);
	debugPrintln(&huart1, z_str);
}

I expected it would convert it but the output for x_str is:

"X: \0\230\0\0 \224\0\0 \204\001\a\030N\0\002\0>\a\0\030\022ÿÿÿÿÿ"
 
or 
 
0x2002ffa8

Had to hardcode the values in this function to even test this as the values aren't being passed to the function as I would've expected. One problem at a time though! Heh . . .

Any suggestions you guys can point me towards would be greatly appreciated. Thanks!

This discussion is locked. Please start a new topic to ask your question.
10 REPLIES 10

Does debugPrintln print in blocking mode or in interrupt? If it's in interrupt mode then your x_str and other variables which are local, will be destroyed when leaving your write_mlx90393_to_uart function. In some instances the variable may still hold their values, but don't count on it.

 

Also as @TDK mentions, your sprintf prints looks to have some values that are 11 to 12 characters, beyond your declared array of 10. I would change the array size to 16.

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.
CAN Jammer an open source CAN bus hacking tool
CANableV3 Open Source