Help with Serial.Read() getting string.

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");
}

/ifDword==“M1”){
//Do…
}*/
}

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

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.

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 ==)
}

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

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

strcmp

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.

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");
  }

}

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?

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.