Pages: [1]   Go Down
Author Topic: Serial read wildcard?  (Read 408 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 '=='?




Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you are hoping to use something like this:
Code:
    } 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.
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 645
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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....
« Last Edit: February 14, 2013, 03:29:57 pm by stairs » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use a regular expression?

http://www.gammon.com.au/forum/?id=11063
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 645
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right. Like a state machine. Example here:

http://www.gammon.com.au/serial
Logged

Pages: [1]   Go Up
Jump to: