Getting code from serial data

Hi, i am looking for a little advice on extracting a code from serial data received from a site, so.. i have the code to receive the data, and i can print it to serial as a super long string of characters, but i want to find and then take action with a specific code, for example i want to find

~1010~ in the middle of the string at a potentially random position, i know i need to search for a start character and the read from there and end with the end character but i am just struggling to find just the right function and examples to get just what i need.

once i have the value i can continue from there, but am just missing the link from something like

if (client.available()) {
    char c = client.read();

here i need to know how to extract the data, and then i can continue to do something with it.

i am sure it's relatively simple but i just need a hint ( or an answer)

Thanks in advance

Its not actually "relatively simple", since parsing, which is the technical term
for what you want to do is a complex problem with vast library of research papers.

For the simple cases you probably want to learn just a little about regular expression
matching, or simple string-matching algorithms. In practice a simple state machine
approach is likely to be a viable option, as is the "read a line, scan it" approach
(assuming lines fit in memory). You can also use a circular buffer for scanning which
only needs memory propotional to the string being searched for, but which loses
the previous context.

Of course, parsing implies that you have read and stored a collection of characters in some appropriate container (like a NULL terminated char array), so that you have something to parse. Parsing a single character doesn't make a lot of sense.

I don't know exactly what your problem is. You mention the need for start and end characters. This demo illustrates how to use start- and end-characters. The original post in the Thread shows a more complex system which allows any byte value to be transmitted - including values that are the same as the start- and end-characters.

Once you have received all of the valid data into a char array then it should be straightforward to find particular characters or strings of characters within it. Look up the C++ string (small s) functions.

Of course this all assumes that the array is small enough to fit comfortably in Arduino memory. If not you will have to test characters as they are received. I think that could easily fit into the demo code.

...R

Thank you for all the comments and hints, i have found a solution using your hints and suggestions.

Much appreciated.

Maybe I'm not seeing this correctly but my approach would be to check each character as it comes in. If it matches the first character I'm looking for then check the next character matches my second character, etc., etc. If you're only looking for a matching part of the string, why do you need a buffer? You would only need a buffer if you need the whole string after you've found the match in it or are trying to match several different things. I'm thinking something like this:

If (inputCharacter== "H"){ //look at next char input
    if (inputCharacter =="E"){//look at next char input
          if (inputCharacter =="L"){//look at next char input
                 if (inputCharacter =="L"){//look at next char input
                       if (inputCharacter =="O"){
                            //string "HELLO" found. No need to check remainder of input.
                               do your stuff;
                       }
                }
          }
     }
}
else if(inputCharacter ==NUL) {
Serial.println ("String HELLO not found")
}

Henry_Best:
Maybe I'm not seeing this correctly but my approach would be to check each character as it comes in

The problem I have with this approach is how to deal with the situation where the first 2 characters appear to be the 1st and 2nd in the sequence but the 3rd is not correct for the 3rd place and might actually be the proper start of the sequence. How do you unwind and start again?

...R

Maybe I'm not seeing this correctly but my approach would be to check each character as it comes in.

You are not. Look at the code. If the character is an 'H', what are the chances that the SAME character will also be an 'E', and 'L' AND an 'O'? A snowball on the surface of the sun has better chances.

PaulS:
what are the chances that the SAME character will also be an 'E', and 'L' AND an 'O'?

I had assumed that inputCharacter was pseudo for Serial.read()

...R

Robin2:

PaulS:
what are the chances that the SAME character will also be an 'E', and 'L' AND an 'O'?

I had assumed that inputCharacter was pseudo for Serial.read()

...R

I thought my comments made that clear.

Robin2:

Henry_Best:
Maybe I'm not seeing this correctly but my approach would be to check each character as it comes in

The problem I have with this approach is how to deal with the situation where the first 2 characters appear to be the 1st and 2nd in the sequence but the 3rd is not correct for the 3rd place and might actually be the proper start of the sequence. How do you unwind and start again?

...R

Then it will drop out of the 3rd if statement (and not check subsequent if statements), as it's false, and start again. A flag will be required to avoid getting the next character before the first test is carried out on the current one.

Robin2:

Henry_Best:
Maybe I'm not seeing this correctly but my approach would be to check each character as it comes in

The problem I have with this approach is how to deal with the situation where the first 2 characters appear to be the 1st and 2nd in the sequence but the 3rd is not correct for the 3rd place and might actually be the proper start of the sequence. How do you unwind and start again?

...R

See my reply to this above.
I can now see a problem when you're looking for, say, "HHELLO" and you get H, H, H, E, L, L, O. The test will fail on the third "H" but can't go back to the second one to check it again. Back to the drawing board!
It may, however, work with non-repetitive short strings such as "abcde" or "153470".

Henry_Best:
I can now see a problem when you're looking for, say, "HHELLO" and you get H, H, H, E, L, L, O. The test will fail on the third "H" but can't go back to the second one to check it again. Back to the drawing board!

Bigger problem would be HELGGGGFFFHELHELLO :slight_smile:

...R

The textfinder library might provide info on string identification in streaming characters.

http://playground.arduino.cc/Code/TextFinder

Ha, i was just coming to post that this was the solution that i had found in the end, Really simple to find a word or string in the middle of even the most chaotic data.

Thanks again zoomkat, that's two times today i have thanked you, and thanks for all the other input and guidance