Go Down

Topic: Serial read wildcard? (Read 646 times) previous topic - next topic

stairs

Hello all.  I have the following code on my Arduino Uno (1.03).  I want to know if I can send x, y coordinate values, such as (4,8), from the serial port to the Arduino (Uno) and have the arduino recognize that it is getting xy coordinates so they can be parsed out. 

Code: [Select]

void serialEvent() {
  while (Serial.available()) {
    // get the new byte
    char inChar = (char)Serial.read();
    // add it to the inputString
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it
    if (inChar == '\n')
      stringComplete = true;
  }
}

void loop() {
  if (stringComplete) {
    if (inputString == "valve\n") {
      cycleValve();
      Serial.write("R");   

    } else if (inputString == "(*,*)\n") {
      //code to parse inputString to get numbers out as x and y
      Serial.write("R");
    }
  }
}



Is there some way to put in a wildcard, such as '*' to be used in a comparison statement with '=='?





PaulS

Quote
I want to know if I can send x, y coordinate values, such as (4,8), from the serial port to the Arduino (Uno) and have the arduino recognize that it is getting xy coordinates so they can be parsed out. 

You can send the coordinates. The Arduino can read and store them, then parse the packet, when it has a complete packet. Knowing that the packet represents coordinates, without any identifying information in the packet is not possible.

Quote
Is there some way to put in a wildcard, such as '*' to be used in a comparison statement with '=='?

What does this mean? You can compare a String (ugh!) to '*'. But, why?

stairs

Basically I want the Arduino to know it is receiving coordinates (some numbers between parentheses separated by a comma) in the structure I laid out in my code above without having to re-write it all (as I have already written quite a bit of code in this structure).

PaulS

So, you are hoping to use something like this:
Code: [Select]
    } else if (inputString == "(*,*)\n") {
to match (4,8) and (3,2) and (174,982)?

No, that won't work. If coordinates are always sent between parentheses, and nothing else is, that is sufficient to identify a packet as coordinates.

Delta_G

It is possible for ardui o to receive the parenthesis and the two numbers and the comma.  But the processor has absolutely no idea what coordinates are or what they look like.  If you want them treated as coordinates or to do anything else with them after they are received then you will have to do that in code.  

stairs

#5
Feb 14, 2013, 09:21 pm Last Edit: Feb 14, 2013, 09:29 pm by stairs Reason: 1
PaulS,

I know it won't work, but am stuck trying to figure out how to make something work.  It doesn't need to be the *,*...I just need it to know it is getting coordinates, and they won't always be the same.  

Maybe I can do something like this?

Code: [Select]

stringOne = "(0,0)";
if (stringOne.compareTo(inputString) > 0 ) {
 // parse out x and y from inputString here
 // and always have the coordinates be greater than 0,0  
}    




Or maybe simpler:

Code: [Select]

stringOne = "(0,0)";
if (inputString >= stringOne ) {
 // parse out x and y from inputString here
 // and coordinates greater than or equal to 0,0 will work  
}



Problem now is that sometimes I'll want negative coordinates....

stairs

Looks like I'm figuring it out...this will probably help: http://arduino.cc/en/Tutorial/StringStartsWithEndsWith.

I'll stop bothering you guys.  Thanks for your quick replies!

Nick Gammon

Use a regular expression?

http://www.gammon.com.au/forum/?id=11063
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Delta_G

Read along until you get a (.  Then read in and save what's next until you get the comma.  Then read in and save the next bit to a different variable until you hit the ).  Now look at the two pieces you saved.  If they are both numbers (made up of digits and within whatever bounds you've set) then you've got a set of coordinates, go ahead and do what you need to do with them.  If they're not numbers or if you don't find the comma or the ) then it was either not coordinates or was corrupted so throw it out.

Nick Gammon

Right. Like a state machine. Example here:

http://www.gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up