Find an known hexa sentence on a bigger hexa sentence

Hello eveybody,

On my project, I'm receiving hexa on the serial Rx, and I need to find on all data received my "known" sentence.
For example, I receive this :

0x23 0xEB 0x54 0x23 0x00 0x18 0x22 0x45 0x72 0xAE 0xA8 ...

My known sentence is : 0x23 0x00 0x18 0x22 and I need to extract also next 3 words (so 0x45 0x72 0xAE) which contain the value I need 0x45 plus the checksum 0x72 0xAE.

How can I extract only needed data ?
Knowing length of the whole data I receive isn't fix, and number of word before or after isn't alsways the same.

Hello
take a look here
https://www.arduino.cc/reference/de/language/variables/data-types/string/functions/indexof/

Thanks for your reply.

That mean I need to register xx characters then make the comparison with my sentence character by character, right ?
If this is the case, that mean I need to wait xx characters until starting to look for. And, I can miss my sentence if xx is too small compare to real answer.

Also, I think this function can only search for a single character, not a sentence. So need to be done for all of them one by one.

ok
take a look here.

Hello,

Try something like this 43uw2o - Online C++ Compiler & Debugging Tool - Ideone.com

I think that you must start explaining your project.

  1. Where does that data come from? Is there a protocol that explains what is being transmitted (and received by the Arduino)?
  2. Do you receive text (the hex representation of a number) or bytes (numbers)?

You are right, I haven't explain enough.

My ESP8266 board is receiving char (actually I use uint8_t which is corresponding to my need) on the UART (serial) port. Data are transmitted at 4800 baud, so I know I'm able to perform some task (comparison, ...) in between 2 different char.
Somewhere on char received, there is the sentence I need to collect and analyze.

@guix , thanks for your proposal. Not fully corresponding to my need, but there is interesting feature inside your code.

Is there a known start and end to the messages you receive?
What separates one transmission from the next?
Can you post a complete message?

The message I receive is an answer from my stove to a request I send. So, yes there is a start, which is just after my initial message is sent.
And next transmission from the stove will be when I will again send a request.

In fact, I know when it start, but I don't know how many characters I need to remove from the start of stove's answer to characters I'm looking for.
Remaining data are scrapped by while (Serial.available() > 0) Serial.read();

How about posting your sketch that sends the request and reads the reply? So we can see what your doing and what the actual reply is. Post the reply from the stove in your reply as well.

Can you post the code which reads the message from the stove, as well as some typical replies which demonstrates this

Knowing length of the whole data I receive isn't fix, and number of word before or after isn't alsways the same.

Ok, I will make this on monday morning and reply here with gathered data.

Hello,

Here is some measurements :

  1. Data received on serial port :
0x81 0x58 0xFF 0xA9 0xFF 0x11 0x00 0x02 0x00 0x58 0x03 0x05 0x39 0x21 0xF8 0x4D 0x01 0xE7 0x11 0x00 0x10 0x13 0x01 0x00 0x6D 0x81 
  1. Sentence I'm looking for :
0x11 0x00 0x02 0x00 0x58 0x03 0x05 0x39

Remaining data (before and after) are useless.
From this, I extract the data 0x03 and confirm the checksum 0x05 0x39

  1. Here the code I currently use to send and receive data :
  uint8_t Query[] = {0x21, 0x00, 0x02, 0x00, 0x52, ReqValue, 0x00, 0x00};
  //Purge remaining data on buffer
  while (Serial.available() > 0) {
    Serial.read();
  }
  
  //Send query to stove and wait for it's reply
  Serial.write(Query, sizeof(Query));
  Serial.flush();
  delay(50);
  
  //Get reply from stove
  uint8_t ReceivedHexa[20];
  for(uint8_t i = 0; i < 20; i++) {
    ReceivedHexa[i] = Serial.read();
  }

On the ReceivedHexa there is some char I don't need and are bothering me. Also, as number of character before and after are not fix, having a fix value (20 currently on the code) to read received data isn't a good solution also (could be too short (I will not catch what I'm looking for) or too long (I will wait for next for next character for ever)).

Do you have any documentation for the stove and the message format? Perhaps there is a byte in the message which indicates length.

The reply message you posted was 26 bytes, beginning and ending with 0x81. Does the message always start and end with 0x81?

Why did you decide to read only 20 bytes?

I'm confused. In your first post you said

My known sentence is : 0x23 0x00 0x18 0x22 and I need to extract also next 3 words (so 0x45 0x72 0xAE) which contain the value I need 0x45 plus the checksum 0x72 0xAE.

Then in this last post

Sentence I'm looking for : 0x11 0x00 0x02 0x00 0x58 0x03 0x05 0x39 From this, I extract the data 0x03 and confirm the checksum 0x05 0x39

How do you know what you are looking for?