Pages: [1]   Go Down
Author Topic: Help with Serial.Read() getting string.  (Read 41717 times)
0 Members and 2 Guests are viewing this topic.
Canada, Alberta
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int Dword;
void setup(){

  Serial.begin(9600);
  Serial.write("Power On");
}

void loop(){
Dword=Serial.read();

if(Dword=='1'){
  Serial.write("\nMotor One -> Selected\n");
}
if(Dword=='2'){
  Serial.write("\nMotor Two -> Selected\n");
}

/*if*Dword=="M1"){
//Do.....
}*/
}


How can i get it so i can type strings? instead of single chars,
Logged

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

There are a couple of ways to go about it.

You could read a byte (that's what Serial.read returns; not an int), and make some decision based on that character, then read again.

Or, you could read all the available data, and store it in an array of characters.

Code:
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void loop()
{
   while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
   {
       if(index < 19) // One less than the size of the array
       {
           inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
   }
   // Now do something with the string (but not using ==)
}
Logged

Canada, Alberta
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I understand that. But how can I check if the Value of the string is = to something else.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26219
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But how can I check if the Value of the string is = to something else
strcmp
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Canada, Alberta
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probally a tad better way of doing this But, Hey it does exactly what i wanted it to do.


Turned what you gave me before into a funtion that returns 1 or 0 depending if its a match.

I'm usally good with c++. Sigh if only i could have used cin>>var; Haha, Guess thad be to easy.

Thnx for help.

Code:

char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void setup(){

  Serial.begin(9600);
  Serial.write("Power On");
}





char Comp(char* This){

  while(Serial.available() > 0) // Don't read unless
    // there you know there is data
  {
    if(index < 19) // One less than the size of the array
    {
      inChar = Serial.read(); // Read a character
      inData[index] = inChar; // Store it
      index++; // Increment where to write next
      inData[index] = '\0'; // Null terminate the string
    }
  }

  if(strcmp(inData,This)  == 0){
    for(int i=0;i<19;i++){
      inData[i]=0;
    }
    index=0;
    return(0);

  }
  else{
    return(1);


  }
}


void loop()
{
  if(Comp("m1 on")==0){
    Serial.write("Motor 1 -> Online\n");
  }
  if(Comp("m1 off")==0){
    Serial.write("Motor 1 -> Offline\n");
  }

}  






Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code hangs if anything other than the specified commands are entered. If a typographical error is made, a restart is required. Any ideas for a solution?
Logged

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

Quote
The code hangs if anything other than the specified commands are entered.
Hangs where? This isn't very robust code. It assumes that the end of the serial data is the end of the packet. Since serial data is transmitted relatively slowly, that may not be a valid assumption.

That aside, though, the Comp() function is called to read the serial data, and see if it is a given string. This is not a very good design.

Much better would be to separate the functionality of Comp() into two parts. One part would collect the serial data into an array. The other part would compare that serial data to a string, and return true or false if the strings matched.

That way, the collect function could be called. Then, the compare function could be called for any number of strings.
Logged

Pages: [1]   Go Up
Jump to: