Go Down

Topic: HELP!! need to talk to PC! (Read 795 times) previous topic - next topic

KevinBra

I have worked out the routines but there is still a problem.

The Arduino code I have will receive the commands but the Arduino code I have will not 'sort' the commends.

I my snippet of code i try to 'reconize' the 'abc' and send a response back, but it doesn't work!

Any help?

Thanks


Code: [Select]
//serial routine ***************************************************
  if (byteComplete) {
    //Serial.print(rcvByte);
    if (rcvByte="abc") {
      Serial.print("error");
    }
    rcvByte = "";
    byteComplete = false;
  }
}
void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    rcvByte += inChar;
    if (inChar == '\n') {
      byteComplete = true;
    }
  }

}

sterretje

"abc" is not the same as "abc\n"

Why do you call a variable that contains a String somethingByte?

Please forget that Sting (capital S) exists in the Arduino world; memory fragmentation can be a result.

Read Serial Input Basics for approaches without te use of String.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Power_Broker

Code: [Select]
if (rcvByte="abc")

Nothing about this line of code is right:

1.) rcvByte is not a byte, but a String. Please name your variables accurately
2.) You need to use "==", not "=", when doing an "equals to" comparison in an if statement
3.) You CAN NOT do "if(String == "something")". If you must compare one string to another, use String.equals()
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

PaulS

Quote
3.) You CAN NOT do "if(String == "something")". If you must compare one string to another, use String.equals()
If one of the operands is a String, then the String class's == operator is used and you CAN compare a String to a string that way.
The art of getting good answers lies in asking good questions.

KevinBra

OK, Thanks to all those that responded.

But here is a different situation.
I read sterretje's example and downloaded the code and it works fine.
The trouble I am having is when I add in the code
Code: [Select]
void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
       
        if(receivedChars=="Option10") {   //code added
          Serial.println("opt 10");       //code added
        }                                 //code added
       
        newData = false;
       
    }
}


it doesn't recognize the 'Option10' input and respond with the 'opt 10' , it just outputs the 'Option10'?


groundFungus

#5
Jul 22, 2019, 03:49 pm Last Edit: Jul 22, 2019, 03:54 pm by groundFungus
Code: [Select]
if(receivedChars=="Option10") {
If you are using the code from Robin2's serial input basics tutorial, the data type of receivedChars is string (null terminated character array) not String (object).  To compare strings use the strcmp() function from the string,h library (the string.h library is automatically included in an Arduino sketch).

You should post the whole program.  Snippets often always leave out important information.

PaulS

Code: [Select]
        Serial.print("This just in ... ");
        Serial.println(receivedChars);

With this code, you can't tell whether receivedChars contains any non-printing characters, such as line feeds or carriage returns.

Code: [Select]
        Serial.print("This just in ... [");
        Serial.print(receivedChars);
        Serial.println("]");

clearly shows whether receivedChars contains any non-printing characters.

Parsing unknown data is not easy. Making the data KNOWN is not that hard.
The art of getting good answers lies in asking good questions.

KevinBra

YEA!!!
The strcmp command worked!!!

Thanks again to all who responded!

Go Up