Send/Receive hex byte over Uart

mickeyTiny
Posts: 1
Joined: Sat Aug 19, 2023 6:41 pm

Send/Receive hex byte over Uart

Postby mickeyTiny » Sat Aug 19, 2023 6:59 pm

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!

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Send/Receive hex byte over Uart

Postby MicroController » Sun Aug 20, 2023 1:38 pm

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'.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 106 guests