Go Down

Topic: Save a string came from Serial (Read 2190 times) previous topic - next topic

tuxduino

Did you actually copy all of the code from my post ? It seems to me you started to copy from "private:" downwards, but there's more before that line :)

crocoscore

Oh god, I didn't see  :smiley-sweat:
That's work but it's not what I wan't...

But I think the last code I have posted is good but I have a just a problem with my array or with string. I was testing it on Gobetwino when I see "is not a well formed command string" for all I wrote on my program... Can you help me ?
"A person who never made a mistake never tried anything new", Albert Einstein.

PeterH


Can you help me ?


If you want help with Gobetwino, then you need to enable logging within Gobetwino so that you can see what strings it received from the Arduino and what messages it displayed, and post the actual log output from Gobetwino (in its entirety).
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
If you want help with Gobetwino, then you need to enable logging within Gobetwino so that you can see what strings it received from the Arduino and what messages it displayed, and post the actual log output from Gobetwino (in its entirety).

The serial data sent to GoBetwino IS displayed, with no need to enable logging it.

crocoscore

I was just saying that my problem is probably either my table or value chains of nature, or display on the console. Finally it is revealed that Gobetwino, I think.

In this code, what is not good?
http://arduino.cc/forum/index.php/topic,140381.msg1055282.html#msg1055282
"A person who never made a mistake never tried anything new", Albert Einstein.

Arrch

Code: [Select]

char readString;
...
void serialEvent()
{
  //creation of the string
  while (Serial.available())
  {
    ...
    readString += c;
   
  }
}


Based on the fact that you're calling it readString, I assume you think the += is concatanating c in your code? It's not. += works as concatenation for String objects, but not for chars. What you are doing is summing the characters into a single character. So if you were to send it '2' and '2', the result in readString will be 'd'.

What you need to an array of chars:

Code: [Select]
const int maxLength = 50;
char readString[maxLength];


You also need an index variable (either global or static) that will keep track of where in the array you are.

Code: [Select]
int strIndex = 0;

And lastly, you need a character that will signal that you are done transmitting your string. This will depend on what program is sending you the string. The Arduino's Serial Monitor, for example can append '\r' and/or '\n' to the end of each transmission.

Code: [Select]
#define END_OF_STRING '\n'

From there you just need to read a character as you receive it and decide whether to parse the data and reset the array (if it's the END_OF_STRING marker):

Code: [Select]

// Do Something with the string
readString[0] = '\0'; // clear the array
strIndex = 0; // reset the index to 0


or add it to the array:

Code: [Select]
readString[strIndex] = inChar; // put the new char in the array
strIndex++; // increment our index for the next char
readString[strIndex] = '\0'; // null terminate our array to make it a proper string

tuxduino

Arrch thanks for explaining my code :)

tuxduino

Quote
Based on the fact that you're calling it readString, I assume you think the += is concatanating c in your code? It's not. += works as concatenation for String objects, but not for chars


Wrong. Operator += is overloaded and has a version that takes a char:

Code: [Select]
String & operator += (char c)

/hardware/arduino/cores/arduino/WString.h

PaulS

Quote
Wrong. Operator += is overloaded and has a version that takes a char:

No. You are wrong. The String class has a += operator. The char type does NOT.

tuxduino

I'm sorry, I didn't notice he declared readString as char.  :smiley-roll-sweat:

tuxduino

Code: [Select]
[quote author=crocoscore link=topic=140381.msg1055398#msg1055398 date=1357224754]
Oh god, I didn't see  :smiley-sweat:
That's work but it's not what I wan't...


I guess it does. But I admit understanding other people's code is not as fun as rolling your own.

crocoscore

Thank you all ! But if my command is a number, how can I convert the string ?
"A person who never made a mistake never tried anything new", Albert Einstein.

Arrch


Thank you all ! But if my command is a number, how can I convert the string ?

atoi()

crocoscore

Ok I will try. I'm back in a few houres  :P
"A person who never made a mistake never tried anything new", Albert Einstein.

Go Up