Go Down

Topic: How to read a long string from serial (Read 7000 times) previous topic - next topic

JorgeMola90

I got to have this string in some varible inside the Arduino --->        >REV001626474042+1101932-0748513100000012;ID=sys<
I want to modify this string, so...I've tried with :

frame = String(Serial.read());

But nothing happen... :~


How do I do? ... some help?

bld

You haven't shown much of your code, so it is difficult to give a precise answer, but I think you should do something like this:
Code: [Select]
String readString;
  while (Serial.available())
  {
    delay(1);
    if (Serial.available() >0)
    {
      char c = Serial.read();
      readString += c;
    }
  }


readString should then contain what you send to the arduino.
captain-slow.dk | non contagious!

JorgeMola90

Thaks for answer.

What I want to do is modify the String (this one  >REV001626474042+1101932-0748513100000012;ID=sys<) to obtain this:
+1101932-0748513100000012

My code is this, but I dont know why doesnt work

Code: [Select]
String frame, newFrame;
void setup()
{
Serial.begin(9600); 
}

void loop()
{
int indicator = 0;
 
    while (Serial.available())
  {
    delay(1);
    if (Serial.available() >0)
    {
      char c = Serial.read();
      frame += c;
     
    }
   indicator = 1;
  }
// So far I get the original frame (>REV001626474042+1101932-0748513100000012;ID=sys)
  if (indicator != 0 )
  {
    int a = frame.indexOf('+');
    int b = frame.indexOf(';');
   
    for (int i = a; i == b - 1; i++)
        {
          char x = frame[i];
          newFrame += x;
        }
//Until here, I get the modyfied frame (+1101932-0748513100000012)...but does't show anything       
  Serial.println(newFrame);
  }
}

bld

for (int i = a; i == b - 1; i++) shouldn't that be for (int i = a; i < b; i++) ?
captain-slow.dk | non contagious!

JorgeMola90

Yes, that was the problem.

Thanks you very much.

This code receive this frame from a GPS >REV001626474042+1101932-0748513100000012;ID=sys<, and remove what is not coordinate, so leave only :  +1101932-0748513100000012

Here the code, just in case.
Code: [Select]
String frame, newFrame;
void setup()
{
Serial.begin(9600); 
}

void loop()
{
int indicator = 0;
 
    while (Serial.available())
  {
    delay(1);
    if (Serial.available() >0)
    {
      char c = Serial.read();
      frame += c;
     
    }
   indicator = 1;
  }

  if (indicator != 0 )
  {
    int a = frame.indexOf('+');
    int b = frame.indexOf(';');
   
    for (int i = a; i < b; i++)
        {
          newFrame += frame[i];
        }
       
  Serial.println(newFrame);
  }
}


Thanks.

JorgeMola90

I've realized that I have to erase the "frame" and "newFrame" because otherwise happen this:

+1101932-0748513100000012                                                                                   //at the first time
+1101932-0748513100000012+1101932-0748513100000012                                           // at the second time
+1101932-0748513100000012+1101932-0748513100000012+1101932-0748513100000012   // 3..and etc

If somebody knows how to erase a String, please post it here. Meanwhile I am doing this after the "Serial.println(newFrame);".. Is not perfect, but it works..

frame = ' ';
newFrame = ' ';

So I'm getting this effect :
+1101932-0748513100000012  //at the first time
+1101932-0748513100000012 // at the second and etc (whit a space at the beginning)

I hope somebody could tell me how to erase a String...meanwhile I'm doing that..is not perfect but it works for the cause.



graynomad

#6
Apr 25, 2011, 03:47 am Last Edit: Apr 25, 2011, 03:59 am by Graynomad Reason: 1
The way you are doing it creates a string consisting of a single space (followed by the null terminator), that's not the same as an empty string

Try

Code: [Select]
frame = '\0';
newFrame = '\0';


or maybe

Code: [Select]
frame = "";
newFrame = "";


There may also be a way with the String class but I'm not familiar with that class.

Note that with normal C strings (ie an array of chars) this sort of technique doesn't really "erase" the string in the sense that the storage is still used, but it will make the functions think there's nothing there.

With the String class this may cause deallocation of memory which is then reallocated next time you make the strings = to something else, and, depending on how well the alloc libraries are written, this is a potential memory leak waiting to happen. As I said I'm not that familiar with the String class, in general I avoid using it on embedded systems for these reasons.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

JorgeMola90

Thats a good way, thanks!

I was trying with
Code: [Select]
frame = ''; //no space
newFrame = ''; // no space

But thats an error...thats why I was doing that thing with
Code: [Select]
frema = ' '; //a single space I know thats no erase, thats why I aked here

I wasnt thought about try with those -> "" , it works very good. Thanks you very much!

JM

Go Up