How to Convert UART received data into Float
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 12:13 AM
HI
"I am using an STM32G071CBT6 controller, and I am receiving data using the receive interrupt. I have received some values in my buffer memory, and now I want to convert them to float. If you have any examples, please share them. My buffer memory is of type uint8_t rx_data[1]
Solved! Go to Solution.
- Labels:
-
STM32G0 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 6:29 AM
Would suggest polishing your C skills, perhaps easier on a PC than embedded. Currently you'd fail at interview.
#define STRBUFLEN 32
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
static int index = 0;
static char string[STRBUFLEN + 1];
char c;
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE : This function should not be modified, when the callback is needed,
the HAL_UART_RxCpltCallback can be implemented in the user file.
*/
c = rxbuf[0]; // The byte received, first index
HAL_UART_Receive_IT(&huart3, rxbuf, sizeof(rxbuf)); // restart
HAL_UART_Transmit(&huart3, &c, sizeof(c), 10); // send the holding buffer, not active input one
if ((c == '\r') || (c == '\n') || (index >= STRBUFLEN)) // line termination
{
string[index] = 0; // NUL terminate string so usable by functions
if (index) // non-zero, ie has content
{
datar7 = atof(string);
index = 0; // reset pointer in buffer
}
}
else if (index < STRBUFLEN)
{
string[index++] = c;
}
}
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 12:30 AM
Hello @suriya
You can do this:
float f;
f = (float)rx_data[1];
Hope this is helpful for you. If your question is answered please check this answer as best answer to be diffused.
Best regards.
II
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 3:58 AM
Use sscanf() or atof() or atod()
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 5:26 AM
Hi
I tried this, but it's not working. When I enter "3" in my serial monitor, I get "10.0". In fact, no matter what number I type, I always get "10", and nothing changes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 5:29 AM
Hi
i tried atof() still not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 5:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 5:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 6:08 AM
rxbuf[1] is a char, the second in the array, The basis for the index is zero
The functions use strings, ie collections of bytes. You aren't going to hold a floating point string in a single byte.
Strings need to be NUL terminated.
You need to accumulate a string from the bytes as you receive them, and when you've got enough THEN do the conversion on the string.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 6:29 AM
Would suggest polishing your C skills, perhaps easier on a PC than embedded. Currently you'd fail at interview.
#define STRBUFLEN 32
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
static int index = 0;
static char string[STRBUFLEN + 1];
char c;
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE : This function should not be modified, when the callback is needed,
the HAL_UART_RxCpltCallback can be implemented in the user file.
*/
c = rxbuf[0]; // The byte received, first index
HAL_UART_Receive_IT(&huart3, rxbuf, sizeof(rxbuf)); // restart
HAL_UART_Transmit(&huart3, &c, sizeof(c), 10); // send the holding buffer, not active input one
if ((c == '\r') || (c == '\n') || (index >= STRBUFLEN)) // line termination
{
string[index] = 0; // NUL terminate string so usable by functions
if (index) // non-zero, ie has content
{
datar7 = atof(string);
index = 0; // reset pointer in buffer
}
}
else if (index < STRBUFLEN)
{
string[index++] = c;
}
}
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-09-02 3:06 PM
What values are you sending to the UART?
TimerCallback tutorial! | UART and DMA Idle tutorial!
If you find my solution useful, please click the Accept as Solution so others see the solution.
