Skip to main content
GSING.19
Associate II
Feb 20, 2019 3:12 PM UTC
Question

Sending hexadecimal number over uart using STM32F407VG

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
Feb 20, 2019 4:28 PM UTC

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
Feb 20, 2019 5:14 PM UTC

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 Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
GSING.19
GSING.19Author
Associate II
Feb 21, 2019 11:31 AM UTC

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
Feb 21, 2019 11:53 AM UTC

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
Feb 21, 2019 8:30 PM UTC

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.