CONVERT array of char to string

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tlerdrden
 
Posts: 80
Joined: Fri Nov 30, 2012 4:46 pm

CONVERT array of char to string

Post by tlerdrden »

Hi Adafruit,
I am trying to convert an array of chars that I get from the serial port to an string, so I want to check the strings sent from my computer. This is my code:

Code: Select all


String Message = "Hello World";
char buffer[32];
int pos=0;
char inByte;

void setup()
{
  Serial.begin(9600);
}

void loop ()
{
  while (Serial.available() > 0) 
     { 
       inByte = Serial.read();
       buffer[pos] = inByte;
       pos++;
     }
 String input=String(buffer);
if (input==Message)
     {
//Do something 
       }
}
However, String Message is never the same as String input.... What's wrong?

Thanks.

A.

User avatar
adafruit_support_mike
 
Posts: 67454
Joined: Thu Feb 11, 2010 2:51 pm

Re: CONVERT array of char to string

Post by adafruit_support_mike »

You've run into the basic programming distinction between "a box" and "what's in the box".. that trips everyone at some point or another.

The variables 'input' and 'Message' aren't strings in the way you're thinking about them. They're boxes.. locations in memory where sequences of characters are stored. In formal vocabulary, they're either 'pointers' or 'references'. When you compare them to each other, you aren't comparing the sequences of characters. You're checking to see if the two variables point to the same location in memory.

(aside from not being at all what you wanted, that's generally A Bad Idea anyway)

What you want to do is compare the contents of two boxes to see if you have identical sequences of characters in two different chunks of memory. To do that, you have to locate and compare the characters one at a time.

That's a tremendously common thing to do with strings, so both the C standard library and the Arduino String class have functions that will do the job for you. The standard C function is strcmp(), but it only works on C-style strings: char arrays that have a 0 to mark the end of the character sequence. The Arduino String class has the .equals() method, which is more flexible about the types of input it will accept.

What you want is:

Code: Select all

if ( input.equals( Message )) {
    //  whatever
}

User avatar
tlerdrden
 
Posts: 80
Joined: Fri Nov 30, 2012 4:46 pm

Re: CONVERT array of char to string

Post by tlerdrden »

Ok good,
Is there any way to discard the data remaining in the serial buffer? I've tried to use the SerialEvent example in Examples / Communication and doesn't work...
This is my code:

Code: Select all

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available()>0)
  { 
    char start = Serial.read();
    if (start == '&') 
    {
      int p = Serial.parseInt();
      long X = Serial.parseInt();
      long Z = Serial.parseInt();
      Serial.print("&1");
    }
    else
    {
      Serial.print("#0"); //The problem is here, now I print as characters as the length of the remaining data
    }
  } 
}
Thanks,
A.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: CONVERT array of char to string

Post by adafruit_support_bill »

Your else clause can be empty. (actually, you don't even need the else clause).

If 'start' is not '&', then the while loop will continue reading characters until it is.

User avatar
tlerdrden
 
Posts: 80
Joined: Fri Nov 30, 2012 4:46 pm

Re: CONVERT array of char to string

Post by tlerdrden »

Well... the reason of the 'else' is because i have to send an acknowledge each time the software asks to arduino.... ACK1 means that the command sent by the software has been processed correctly by the arduino, otherwise, arduino doesn't know what the command is, ACK0. But as the code is programmed, i am going to send a lot of 'ACK0'...
The other way is just to do not send anything and "the software that sends commands to arduino" could have a watchdog...

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: CONVERT array of char to string

Post by adafruit_support_bill »

the reason of the 'else' is because i have to send an acknowledge each time the software asks to arduino.
How does the software ask? Without understanding your complete protocol spec, it is hard to offer advice on the code.

User avatar
tlerdrden
 
Posts: 80
Joined: Fri Nov 30, 2012 4:46 pm

Re: CONVERT array of char to string

Post by tlerdrden »

OK
1. A software running on windows sends, using the COM port, a command to Arduino
2. The Arduino reads the command.
3.a If the command read is expected by the Arduino, it will make something and send an "ACK1" to the windows software.
3.b if the command read is not expected by the Arduino, it send an "ACK0" to the windows software.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: CONVERT array of char to string

Post by adafruit_support_bill »

1. A software running on windows sends, using the COM port, a command to Arduino
2. The Arduino reads the command.
What is the format of a command? From your code, I it is supposed to be a '&' followed by 3 integers.
3.a If the command read is expected by the Arduino, it will make something and send an "ACK1" to the windows software.
3.b if the command read is not expected by the Arduino, it send an "ACK0" to the windows software.
What if the software sends something that is not recognizable as a command?

User avatar
tlerdrden
 
Posts: 80
Joined: Fri Nov 30, 2012 4:46 pm

Re: CONVERT array of char to string

Post by tlerdrden »

1. Yeah, command is> '&' followed by 3 integers separated by commas.
2. In that case, send ACK0...

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: CONVERT array of char to string

Post by adafruit_support_bill »

2. In that case, send ACK0...
From that description, your code meets the specification. Since there is no format for a 'non-command', every stray character between the last integer and the next '&' qualifies as a non-command that needs an ACK0.

What you might want to do in the else clause is to send tha ACK0, then flush any characters in the buffer up to but not including the next '&'

Code: Select all

    else
    {
      Serial.print("#0"); //send the NAK
      // Flush anything in the input buffer - but stop if you see a '&'
      while((Serial.available()>0) && Serial.peek() != '&')) 
      {
          Serial.read();  // flush 
      }
    }

User avatar
tlerdrden
 
Posts: 80
Joined: Fri Nov 30, 2012 4:46 pm

Re: CONVERT array of char to string

Post by tlerdrden »

Ohh ok, i didn't know the peek command... thanks guys!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”