Page 1 of 1

hexadecimal number send serial communication

Posted: Wed Jun 08, 2022 10:29 am
by seokgi
Hello!
I have a question.
I want to transmit the decimal number 371234 as a 3-byte hexadecimal number through serial communication.
How should I send it?

Thank you for always.

Re: hexadecimal number send serial communication

Posted: Wed Jun 08, 2022 10:45 am
by Steve-Matrix
If you are in control of both sides of the communication, just send it as a number and then convert it on the other side when you want to display it.

But if it needs to be send as a hex string, then use the "NumberToHex" calculation function. This will convert it to "0x0005aa22" and so you may need to trim some of the prefix characters depending on the format you require.

Re: hexadecimal number send serial communication

Posted: Wed Jun 08, 2022 10:58 am
by seokgi
Thanks for your help.
It is output in the form of 0x123456 using Numbertohex, but it is a string. I want to send this as UART(RS232) - send number hexadecimal 0x123456

Re: hexadecimal number send serial communication

Posted: Wed Jun 08, 2022 11:22 am
by BenR
Hello,

If you're sending it as raw binary data then you would do something like this, assuming you're sending the least significant byte first.

LongNumber = 371234
UART::SendChar (LongNumber)
LongNumber = LongNumber >> 8
UART::SendChar (LongNumber)
LongNumber = LongNumber >> 8
UART::SendChar (LongNumber)

This would send the number as three binary bytes.

Binary, Decimal and Hexadecimal are simply different ways of representing a value via a human readable string, in each case the value itself doesn't change.

Re: hexadecimal number send serial communication

Posted: Thu Jun 09, 2022 2:52 pm
by seokgi
Thanks for your help, I solved it well.