Page 1 of 1

Send/Receive hex byte over Uart

Posted: Sat Aug 19, 2023 6:59 pm
by mickeyTiny
I'm using esp32 dev module to send hex byte to MCP39F511 power ic. This ic can response over uart if received valid hex byte, example if i send "0xA5 0x04 0x53 0xFC" from esp32 to these ic then ic will response hex byte=0x06(Acknowledge). But i am having difficulty how to send data as hex byte and how to get response from power ic. I used the source code of espidf but still only sending ascii and not hex bytes. I'm using espidf. Thank you and hope every help me please!

Re: Send/Receive hex byte over Uart

Posted: Sun Aug 20, 2023 1:38 pm
by MicroController
To represent bytes, or any numerical value, in C, you need to write them as numbers, not as strings.
e.g.

Code: Select all

const uint8_t data[] = { 0xA5, 0x04, 0x53, 0xFC };
defines an array of 4 bytes with the values you provided. In fact, there is no such thing as a "hex byte"; but in C code you can write numeric literals, e.g. byte values, in different bases: 0xA5 is exactly the same as 165 or 0b10100101‬.
While 0xA5 in C code is a number, "0xA5" (notice the quotation marks!) is a 4-character string.
For text output/logging the value is converted into readable ASCII; the single byte value 0xA5 would be converted into the 3 characters '1', '6', and '5'.