Skip to main content
antonius
Associate III
July 8, 2019
Question

Passing float value to UART buffer ?

  • July 8, 2019
  • 10 replies
  • 1994 views

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

This topic has been closed for replies.

10 replies

Tesla DeLorean
Guru
July 8, 2019

strcpy()?

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

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

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
July 8, 2019

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...

Tesla DeLorean
Guru
July 8, 2019

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
antonius
antoniusAuthor
Associate III
August 20, 2019

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

antonius
antoniusAuthor
Associate III
August 20, 2019

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