Go Down

Topic: Clear String variable.. (Read 391 times) previous topic - next topic

AnyPopins

Feb 09, 2019, 02:43 am Last Edit: Feb 09, 2019, 02:50 am by AnyPopins
Hi, is it possible to clear/empty (whatever) a variable String besides to do variable = ""; ?
ask this because using the sim800l module i receive sms that change value in my string password, but if i keep using the sistem without a restart, keeps using the old value...if i restart shows perfect the new one, it really store the new value but i don't know why doesnt recognize it..
may be its the module, or the serial, or ram etc..thanks

any..

edir:

works this?

Code: [Select]
while(Serial.available())
  Serial.read();

econjack

Why are you using the String class? Use a char array instead.

demkat1

dont "stick" to what you think its wrong.
post your code using the "code tags" so someone can understand

UKHeliBob

Code: [Select]

while(Serial.available())  //while serial data is available
  Serial.read();  //read it and throw away what has been read


Note the comments that I have added.  The code does not change the value of any variables
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AnyPopins

this is the code:

Code: [Select]
else  if (Admin_tel >= 0)
  {
      Remitente_SMS();
      if ( Remitente == parametros.Admin) { 
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"" + Remitente + "\"");
      delay(1000);
      SIM900.print("Ud. YA es el Admin.");
      delay(100);
      SIM900.println((char)26); //Comando de finalización ^Z
      delay(100);
      SIM900.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      Remitente = "";
      mensaje = ""; //Bórralo para la próxima vez
      SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
    }
    else {

      Remitente.toCharArray(parametros.Admin, 15);
      salvaAdmin();
      //delay(1000);
      SIM900.print("AT+CMGF=1\r");
      delay(1000);
      SIM900.println("AT+CMGS=\"" + Remitente + "\"");
      delay(1000);
      SIM900.print("Ud. es el nuevo Admin.");
      delay(100);
      SIM900.println((char)26); //Comando de finalización ^Z
      delay(100);
      SIM900.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      Remitente = "";
      mensaje = "";
      SIM900.println("AT+CMGD=1,4"); // eliminar todos los mensajes
      //resetFunc();
    }
  }

AnyPopins

i got this code from here:


https://www.baldengineer.com/when-do-you-use-the-arduinos-to-use-serial-flush.html

How do you clear the incoming Serial buffer?
Since the flush() method only clears the transmit buffer, how do you empty the receive (or incoming) buffer?  This code will do it, which you can put into a function like "flushReceive()" or something equally amusing.

Code: [Select]
while(Serial.available())
  Serial.read();


it also talks about flush, but i don't know what it is or how to use it..

demkat1

#6
Feb 09, 2019, 03:29 pm Last Edit: Feb 09, 2019, 03:30 pm by demkat1
Quote from: AnyPopins
Code: [Select]
while(Serial.available())
  Serial.read();



As mentioned already in response #3...
this clears (empties and throws away) the receive buffer

PaulS

#7
Feb 10, 2019, 03:04 am Last Edit: Feb 10, 2019, 03:04 am by PaulS
Quote
it also talks about flush, but i don't know what it is or how to use it..
What talks about flush()?

Did you bother to look at the documentation? Blocking until the outgoing serial buffer is empty hardly seems like a useful thing to do, and has NOTHING to offer as a solution to your vaguely stated problem.

Why can't you POST ALL OF YOUR CODE? Why can't you grow up and stop using the String class. A c string would have none of the issues you are supposedly seeing.
The art of getting good answers lies in asking good questions.

AnyPopins

#8
Feb 10, 2019, 11:00 pm Last Edit: Feb 10, 2019, 11:03 pm by AnyPopins
Hi PaulS, maybe you're right i'm 27..it's time not olny for grow up jaja..

can i ask you for  "A c string" example? compares with a String class... i realize i don't know to much of C# C++ (arduino)..maybe with an example i could be able to practice and look for info/documentation...would be great..thanks anyway, advice taken..


could be this  an example?

Code: [Select]
char charBuf[50];
stringOne.toCharArray(charBuf, 50)

PaulS

Quote
could be this  an example?
No. That is an example of extracting the data from the String instance that you should not have.

Look at Serial Input Basics - updated to learn how to store data in an array, with a NULL terminator (also known as a string), and how to parse the string.
The art of getting good answers lies in asking good questions.

Go Up