cancel
Showing results for 
Search instead for 
Did you mean: 

Sending integers over USART as ASCII characters, using HAL Libraries

salih kan
Associate

Posted on February 27, 2018 at 09:17

Hello Everyone;

To send ASCII characters via USART I normally use Hal libraries and the function below;

HAL_UART_Transmit(&huart2, (uint_t*)'Hello', 6,10);

But, that one is for characters only.

How can I send the value of an integer over USART as its ASCII characters ? Which functions and libraries should I use ?

. .

. For example (dummy program just to show my purpose):

int i = ADC read value; //Lets say i=15

printf(i); //value should be sent in ASCII characters, so 1 and then 5 should be sent in ASCII.

. .

.

Thanks for your help, Kind regards

Salih

6 REPLIES 6
Posted on February 27, 2018 at 10:08

You can use sprintf(char *buffer, char *formatted_string) to convert printf-like formatted string to a text (char * buffer for example)  then send this buffer through the HAL_UART_Transmit()

You can also implement some primitive function required by printf - I mean putchar() where the actual implementation will contain the HAL_UART_Transmit(). I used both approaches with Keil IDE.

Posted on February 27, 2018 at 10:29

 ,

 ,

Something like that for the first option.

♯ include <,string.h>,

char buffer[100],

int a=15,

sprintf(buffer, '%d', a),

HAL_UART_Transmit(&,huart1, (uint8_t *)buffer, strlen(buffer), 1000) ,

assumed uart1

---------------------------------------------------------------

In case of using regular printf.

If you use Keil just google retarget.c to find Keil way of redirecting the printf to the UART.

Andrew Neil
Evangelist III
Posted on March 03, 2018 at 19:37

This has already been answered in your cross-post on the Keil forum:

http://www.keil.com/forum/63397/

 
Posted on March 03, 2018 at 19:44

Seems to be the same solution... or I cannot see the difference...

Posted on March 03, 2018 at 19:50

Yes - it is.

Posted on March 03, 2018 at 19:56

Great. You know, I closely follow a few people here and try to understand every piece of what you say.

It's the only way for me to learn something from professionals