Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: KevinBra on Jul 19, 2019, 07:54 pm

Title: HELP!! need to talk to PC!
Post by: KevinBra on Jul 19, 2019, 07:54 pm
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;
    }
  }

}
Title: Re: HELP!! need to talk to PC!
Post by: sterretje on Jul 19, 2019, 10:03 pm
"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 (https://forum.arduino.cc/index.php?topic=396450.0) for approaches without te use of String.
Title: Re: HELP!! need to talk to PC!
Post by: Power_Broker on Jul 20, 2019, 02:25 am
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()
Title: Re: HELP!! need to talk to PC!
Post by: PaulS on Jul 22, 2019, 01:31 pm
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.
Title: Re: HELP!! need to talk to PC!
Post by: KevinBra on Jul 22, 2019, 03:21 pm
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'?

Title: Re: HELP!! need to talk to PC!
Post by: groundFungus on Jul 22, 2019, 03:49 pm
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 (https://www.arduino.cc/reference/en/language/variables/data-types/string/) (null terminated character array) not String (https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/) (object).  To compare strings use the strcmp() (http://www.cplusplus.com/reference/cstring/strcmp/) function from the string,h library (http://www.cplusplus.com/reference/cstring/) (the string.h library is automatically included in an Arduino sketch).

You should post the whole program.  Snippets often always leave out important information.
Title: Re: HELP!! need to talk to PC!
Post by: PaulS on Jul 22, 2019, 04:11 pm
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.
Title: Re: HELP!! need to talk to PC!
Post by: KevinBra on Jul 22, 2019, 04:57 pm
YEA!!!
The strcmp command worked!!!

Thanks again to all who responded!