cancel
Showing results for 
Search instead for 
Did you mean: 

Passing float value to UART buffer ?

antonius
Senior

Dear Members,

How can I pass float value from RPM to UART buffer

float ==> (uint8_t*)&aTxBuffer8 ?

(uint8_t*)&aTxBuffer8 = snprintf(str_rpm,sizeof(str_rpm),"%f",rpm);

?

Thanks

10 REPLIES 10

strcpy()?

You could use pointers, or use a function sending the string directly.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gp73
Associate III

define a union composed by (float and a vector of uint8_t).

The vector dimension depend on how many bytes is implemented the float in your architecture.

In this way you can use ever the byte.

Bets regards

S.Ma
Principal

if it's to show a value to users, make it simple:

uint32_t rpm_x100;
rpm_x100 = (uint32_t) (rpm * 100);

Make your own printf with custom markers like %D for decimal X.X, %C for X.XX, %M for X.*** from int value...

Or functions like ftoa()

The question from the OP is more one of moving strings around, and the fact you can just write something to the address of the pointer with the kind of assignment used in the question.

I seem to recall sprintf() returning the number of bytes, not a pointer to the string. http://www.cplusplus.com/reference/cstdio/snprintf/

char aTxBuffer8[16];

int TxBuffer8length = snprintf(aTxBuffer8,sizeof(aTxBuffer8),"%f",rpm);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

How about passing this :

             *temperature=((temp_x/65536.0)*165.0)-40.0;
 
		uint8_t Sensor_variable[]=*temperature;
 
	HAL_UART_Transmit_IT(&huart1,(uint8_t*)&Sensor_variable,sizeof(Sensor_variable)); 
 
 
??
Thanks

char Sensor_variable[16];
				int TxBuffer8length = snprintf(Sensor_variable,sizeof(Sensor_variable),"%2.2lf",*temperature);
				HAL_UART_Transmit_IT(&huart1,(uint8_t*)&Sensor_variable,sizeof(Sensor_variable)); //Temp and humidity variable
 

Am I right ?

antonius
Senior

Sensor_variable will be transmitted on buffer and the content = *temperature ?

No​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

What do I miss then? Thanks