How to receive HEX "question" from serial and send HEX "answer" to serial?

How to receive HEX question from serial and send HEX answer to serial?

I found snippet below, if value is 2 (ASCII) then it sends AB 9F 21 0E 49 (HEX) back to serial.

char DATA = {0xAB,0x9F,0x21,0x0E,0x49};

byte val = 0;

void loop() {
while (Serial.available() > 0){
val = Serial.read();

if (val == ‘2’){
for(byte i =0;i<sizeof(DATA);i++)(Serial.print(DATA*));*

  • }*
    }
    }[/td]
    [/tr]
    [/table]
    But in my case I want to receive a HEX command instead of ASCII and send a HEX back to serial.
    For example, if value is 01 02 23 00 01 00 1E (HEX) then send AB 9F 21 0E 49 (HEX) back to serial.
    *Received value doesn’t have a standard length. *

Please use code tags when posting code.

To send hex values instead of ASCII use

Serial.write();

Your for loop won’t work like that anyway.

for(byte i =0; i<sizeof(DATA); i++){
(Serial.write(DATA[i]));
}

Thank you for your reply!

What I wanted to know is how to read a HEX-code from serial for sending also a HEX-code to serial.

Something like this:
if (val == ‘01 02 23 00 01 00 1E’){
for(byte i =0;i<sizeof(DATA);i++)(Serial.print(DATA));
}

So if I ask a hexadecimal “question” it has to answer a hexadecimal “answer”.

for(byte i =0;i<sizeof(DATA);i++)(Serial.print(DATA)); => also works for sending HEX-data to serial port.

Is this for communicating between Arduinos or are you trying to interface with a sensor or PC?

Is this HEXadecimal number: 01 02 23 00 01 00 1E? If yes, it should be like this (without space): 0102230001001E.

Or

Is it a code/command in HEX notation with inter spaces?

Yes, the HEX "question" which is send from the serial port is 01 02 23 00 01 00 1E.

Power_Broker:
Is this for communicating between Arduinos or are you trying to interface with a sensor or PC?

ALSO:
If communicating with a sensor, please post the datasheet. If communicating with a PC, please post the code running on the PC.

joentje1978:
Yes, the HEX "question" which is send from the serial port is 01 02 23 00 01 00 1E.

The word HEX can be confusing. The number 65 in decimal is 0x41 in hex. They both mean exactly the same thing.

You can send the number 65 as two characters 6 and 5 or you can send it as a single byte with the value 65 (which is also the binary value 0b01000001 and which also happens to be the ASCII code for the letter 'A')

So when you say the data is "01 02 23 00 01 00 1E" do you mean it is a string of 20 characters (counting the spaces) in 20 bytes or do you mean it is a series of 7 bytes?

If you are not sure how to answer that question then please post a link to the datasheet for the device that is sending the data.

...R

See attachment, code 01 02 23 00 01 00 1E.

If I send this code to the ESP-module with Docklight I want to get a hexadecimal answer back.

Docklight.jpg

"hexadecimal", "decimal", "binary" means next to nothing here. Those are terms to describe the format of a human readable representation of a number.

It makes sense only if the data is displayed or stored in ASCII.

The seen shot you present is being displayed in hex. The underlying data is not hex. If it were it would consist only of the values 0x30-0x39 ('0'-'9') and 0x41-0x46 ('A'-'F') or 0x61-0x66 ('a'-'f').

That data is just a stream of bytes, which can be displayed in various human readable forms, as indeed the window is offering you.

I found snippet below, if value is 2 (ASCII) then it sends AB 9F 21 0E 49 (HEX) back to serial.

No, it doesn't. The snippet won't work, because Serial.print() will assume that ASCII characters are encoded by the data array (some aren't) and the data array is not zero terminated as required, so additional garbage will be spewed out.

HEX is a human readable representation of binary data.

0x41 and A represent the same entity; but, A is human readable (human friendly) and not 0x41. HEX is a compact form of binary. It does not matter in what format/base we submit data to the computer, the computer sees/processes data in bit form and interprets according to the intention of the user.