1

We are using Crossworks IDE to run freeRTOS in C++. Here, we are sending data via serial com using "HAL_UART_Transmit" built in STM32_HAL function. We want to send Sensor data via serial com to external device.

here is an exmaple format:

float humiditySensorValues[2];
uint8_t buffer[100] =  {"Temperature = ? \r\n"} ;    

HAL_UART_Transmit(&husart3, buffer, sizeof(buffer), HAL_MAX_DELAY);

In the above code we want to replace "? -> humiditySensorValues[0]" and send the data.

where humiditySensorValues[0] contains updated temperature data value in degrees.

Any suggestion to resolve the above issue would be much appreciated.

Siva
  • 13
  • 3
  • Are you looking for `snprintf(buffer, 100, "Temperature = %f \r\n", humiditySensorValues[0]);` You mention C++, but the question is tagged C, and `snprintf` is arguably not appropriate in C++. – William Pursell Jan 15 '21 at 10:29
  • 1
    I suggest to learn C first, then to start uC programming. Eithout basic knowledge it is not possible write programs. – 0___________ Jan 15 '21 at 10:51
  • If you are using C++ then tag your question C++ not C. String handling is quite different between the two languages. C++ opens up the option to make something even less efficient than sprintf (which is an achievement!) by using sstream or std::string with heap allocation and all manner of other PC goo. – Lundin Jan 15 '21 at 10:58
  • You might also want to mention with STM32 this is, because using floating point on Cortex M below M4 without FPU is quackery - it will link in horrible software floating point libs. – Lundin Jan 15 '21 at 11:01

1 Answers1

2

Use snprintf:

char buffer[100] = {0};
int len = snprintf(buffer, sizeof(buffer), "Temperature = %f \r\n", (double)humiditySensorValues[0]);
HAL_UART_Transmit(&husart3, (uint8_t*)buffer, len, HAL_MAX_DELAY);

Do not use sprintf as that does not check for buffer overflow.

koder
  • 2,038
  • 7
  • 10
  • also, add how to print floats as ARM standard library has float printf support disabled. – 0___________ Jan 15 '21 at 10:47
  • @P__JsupportswomeninPoland, it can be enabled, see [this](https://stackoverflow.com/questions/28334435/stm32-printf-float-variable) post. – koder Jan 15 '21 at 12:07