cancel
Showing results for 
Search instead for 
Did you mean: 

Sending hexadecimal number over uart using STM32F407VG

GSING.19
Associate II

Dear All,

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

Regards,

Gunit

5 REPLIES 5
S.Ma
Principal

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);
 
 
 
 
 
 

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 Up vote any posts that you find helpful, it shows what's working..
GSING.19
Associate II

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
Lead

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
Chief II

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.