Skip to main content
salih kan
Visitor II
February 27, 2018
Question

Sending integers over USART as ASCII characters, using HAL Libraries

  • February 27, 2018
  • 2 replies
  • 9497 views

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

This topic has been closed for replies.

2 replies

Bogdan Golab
Lead
February 27, 2018
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.

Bogdan Golab
Lead
February 27, 2018
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
Super User
March 3, 2018
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/

 
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Bogdan Golab
Lead
March 3, 2018
Posted on March 03, 2018 at 19:44

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

Andrew Neil
Super User
March 3, 2018
Posted on March 03, 2018 at 19:50

Yes - it is.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.