Skip to main content
GSING.19
Associate II
February 20, 2019
Question

Sending hexadecimal number over uart using STM32F407VG

  • February 20, 2019
  • 5 replies
  • 1949 views

Dear All,

I need to send hexadecimal number over uart using STM32F407VG. How can I do that?

Regards,

Gunit

This topic has been closed for replies.

5 replies

S.Ma
Principal
February 20, 2019

Ideally create a generic printf function.

Otherwise, just hardcode what you need.

If it's a byte, it would like: (unoptimized)

uint8_t data = 0x9D; // example
uint8_t ch,hexdigit;
 
hexdigit = data>>8; // MSB '9'
ch = hexdigit + (hexdigit<0x0A)? '0' : 'A' ;
Transmit(ch);
 
hexdigit = data & 0xFF; // LSB 'D'
ch = hexdigit + (hexdigit<0x0A)? '0' : 'A' ;
Transmit(ch);
 
 
 
 
 
 

Tesla DeLorean
Guru
February 20, 2019

Seems like some pretty basic C programming stuff...

see also sprintf() and itoa() usage

http://www.cplusplus.com/reference/cstdlib/itoa/

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
GSING.19
GSING.19Author
Associate II
February 21, 2019

HAL_UART_TRANSMIT function accepts uint8_t as its second argument.

Hence convert hex to int and then transmit

https://www.includehelp.com/c-programs/convert-hexadecimal-to-integer.aspx

AvaTar
Senior III
February 21, 2019

I agree with the other posters.

And want to add, you see the problem the wrong way.

You want to send a string, that happens to be interpretable (represent) as hexadecimal number.

Piranha
Principal III
February 21, 2019

Maybe this isn't the case, but it should be reminded, that if both sides of connection are custom made, then the most effective format is of course binary data packets.