Help on read hex from serial

I have spend more than 8 hours on this. Please help.

I'm using MCU ESP8266 and it connect rx and tx to another voice recognition module.

The module send hex when get specific command.

So I want to read the hex I get then perform other tasks.

The hex is fixed.

So my goal is when I received F4 06 03 FF print Yes.
when I received F4 06 05 FF print no.

I only successful work on reading serial using string. But hex those thing too difficult for me.

Anyone can give me an example code?
Apperaciate that. Thanks

Below is the example code for handle with serial for the voice module from seller.
But I'm not able to convert it to arduino.

static void _hb_uart_recv(char *buf, int len) 
{
  int i;
  static uint8_t i_c;
  static bool start_rec_flag = 0;

  for (i = 0; i < len; i++) 
  {
    LOGR(TAG, "%02x ", (uint8_t)buf[i]);

    if ((uint8_t)buf[i] == 0xF4)            //Head Ok, starting to receive
    {
      i_c = 0;
      start_rec_flag = 1;
    }

    if (start_rec_flag == 1) 
    {
      rec_buf[i_c] = (uint8_t)buf[i];
      i_c++;

      if (i_c > 3)                         //6 byte received 
      {
        i_c = 0;
        start_rec_flag = 0;

        if(rec_buf[0] == 0xF4 && rec_buf[1] == 0x06 && rec_buf[2] > 0x00)
        {
            switch (rec_buf[2])
            {
            case 1: user_player_play(AUDIO_PLAY_REPLY, "101"); break;

            default:
              break;
            }
            memset(rec_buf,0,sizeof(rec_buf));
        }
          memset(rec_buf,0,sizeof(rec_buf));
        }
          memset(rec_buf,0,sizeof(rec_buf));
        }
      }
    }
  } 
}

could you provide a link to the module or even better the UART protocol documentation that is being used?

There is no UART protocol documentation.

The only documentation I got was When I say:
Hi xxx. I get F4 06 01 FF
Turn onf the light. I get F4 06 02 FF
same as other commands.

I can use serial monitor to get those hex when I connect directly to the module.
The problem now is just using ESP8266 to read those hex under Arduino.

I would suggest to study Serial Input Basics to handle this, the last part is about Binary data which is your case

(what's the module's name / reference?)

[quote="sasukebinbin, post:4, topic:849987"]
F4 06 01 FF[/quote]

in general, information is serially transmitted in either ASCII or binary (e.g. ethernet). i think what you asking is how to translate an ASCII string formatted as hex data into non-ascii values

sscanf () is one approach

I don't think so. clearly the module is using a binary protocol if ((uint8_t)buf[i] == 0xF4)

it would be interesting to have more than snippets (Snippets R Us!) and see how _hb_uart_recv() is being called, likely by another function listening to the Serial input and building a buffer (buf) which will then be parsed to extract a command into the rec_buf buffer. it seems 0xF4 is the start marker

so a Serial.read() might return 0xF4 and the terminator byte is 0xFF

may be...
not knowing what the device is nor the protocol does not help.

the head marker might actually be 0xF4 0x06

looking at the snippet it seems that a command can be either 4 bytes or 6 bytes long (may be, if we trust the comment) but it seems they trigger a command without looking at the other bytes but you need to have received more than 4 bytes. so the FF might not be the end...

@sasukebinbin - I'd say assume that the start marker is 0xF4 and collect bytes until a timeout or 0xFF and then just compare with your values

the tutorial would get you going. try that and post your progress

Thanks everyone for the reply.
I have figure out how to achieve this.
And it's really simple.

Just use the example code for readBytes

const int BUFFER_SIZE = 50;
char buf[BUFFER_SIZE];

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // check if data is available
  if (Serial.available() > 0) {
    // read the incoming bytes:
    int rlen = Serial.readBytes(buf, BUFFER_SIZE);

    // prints the received data
    Serial.print("I received: ");
    for(int i = 0; i < rlen; i++)
      Serial.print(buf[i]);
  }
}

That should work most of the time, it will be a bit slow because there is a 1s timeout (by default) for readBytes() to return if no new byte is coming through (and seems you get only 4 or 6 bytes and your buffer is 50) . That will add visible latency to your handling of the recognised sentence.

Thanks man.
I can not feel the delay.
But I actually get 4 bytes. So what's the buffer size I should set here?

But I actually get 4 bytes. So what’s the buffer size I should set here?

then make it 4 would be a minimum. 6 would get a bit safer, there would be a delay but if it doesn't bother you that might actually help with synchronising

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.