Pages: 1 [2]   Go Down
Author Topic: Save a string came from Serial  (Read 1905 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

France
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

"A person who never made a mistake never tried anything new", Albert Einstein.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

France
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"A person who never made a mistake never tried anything new", Albert Einstein.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3384
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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:
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:
#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:
// 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:
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
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arrch thanks for explaining my code smiley
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
String & operator += (char c)

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
[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.
Logged

France
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"A person who never made a mistake never tried anything new", Albert Einstein.

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3384
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

France
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I will try. I'm back in a few houres  smiley-razz
Logged

"A person who never made a mistake never tried anything new", Albert Einstein.

Pages: 1 [2]   Go Up
Jump to: