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.
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.
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.
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.
Remaining data (before and after) are useless.
From this, I extract the data 0x03 and confirm the checksum 0x05 0x39
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