How to read part of a serial string?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
geno
 
Posts: 4
Joined: Tue Jan 03, 2012 2:47 pm

How to read part of a serial string?

Post by geno »

Hi,

Just starting out and having trouble determining the best way to look for a set of characters in a serial string and perform a function when they are received.

An example string that would be sent to Arduino via a serial connection:
RING 4 08:08:0e:10:00:0b SCO

When a string that ends with "SCO" as above is encountered, I need to assert a pin high and then return it to low state. (I think I have that part figured out).

A push in the right direction or some example code snippets would be greatly appreciated.

Thanks in advance,
Geno

User avatar
jasonwebb
 
Posts: 111
Joined: Sat Sep 10, 2011 2:15 pm

Re: How to read part of a serial string?

Post by jasonwebb »

The Arduino framework contains a variety of functions for string manipulation. Here are two that should help you quite a bit:

http://arduino.cc/en/Tutorial/StringStartsWithEndsWith
http://arduino.cc/en/Tutorial/StringSubstring

User avatar
geno
 
Posts: 4
Joined: Tue Jan 03, 2012 2:47 pm

Re: How to read part of a serial string?

Post by geno »

Thankyou very much for the reply,

I have looked into both the functions you referenced. I ended up with more questions than when I started.


// startsWith() checks to see if a String starts with a particular substring:
String stringOne = "HTTP/1.1 200 OK";
Serial.println(stringOne);
if (stringOne.startsWith("HTTP/1.1")) {
Serial.println("Server's using http version 1.1");
}

// you can also look for startsWith() at an offset position in the string:
stringOne = "HTTP/1.1 200 OK";
if (stringOne.startsWith("200 OK", 9)) {
Serial.println("Got an OK from the server");
}

In the examples above the first line: String stringOne= This seems to define the entire string. What if the string I need to look for has variables?


Thanks,
Geno

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”