Serial.find() e buffer delucidazioni


salve tutti, eccomi di nuovo chiedere 'lumi' ai più esperti, giocherellando con l'esp8266 mi sono imbattuto in sketch dove veniva usata la funzione serial.find() che come descritto   nel reference ricerca una stringa nel buffer e restituisce un valore booleano, il mio dubbio è, dopo aver richiamato questa funzione il buffer si svuota o no? nel caso non lo facesse come posso svuotare il buffer? grazie mille per le risposte  :)

non è scritto da nessuna parte che svuoti il buffer (... e, se guardi nel core il sorgente nella classe stream, che è quella ereditata dalla serial, vedi che non effettua alcuno svuotamento). per svuotare il buffer ... devi leggere dati (... se non ti servono, un loop stretto di serial.read() e passa la paura) visto che la serial.flush() (... che molti credono svuoti il buffer) in realtà fa tutt'altro ;)

per completezza ...

code: [select]
// find returns true if target string found
bool  stream::find(char *target)
{
  return finduntil(target, strlen(target), null, 0);
}

// reads data stream until target string of given length found
// returns true if target string found, false if timed out
bool stream::find(char *target, size_t length)
{
  return finduntil(target, length, null, 0);
}

// find search ends if terminator string found
bool  stream::finduntil(char *target, char *terminator)
{
  return finduntil(target, strlen(target), terminator, strlen(terminator));
}

// reads data stream until target string of given length found
// search terminated if terminator string found
// returns true if target string found, false if terminated or timed out
bool stream::finduntil(char *target, size_t targetlen, char *terminator, size_t termlen)
{
  if (terminator == null) {
    multitarget t[1] = {{target, targetlen, 0}};
    return findmulti(t, 1) == 0 ? true : false;
  } else {
    multitarget t[2] = {{target, targetlen, 0}, {terminator, termlen, 0}};
    return findmulti(t, 2) == 0 ? true : false;
  }
}


guglielmo


Arduino Forum > International > Italiano > Software (Moderator: leo72) > Serial.find() e buffer delucidazioni


arduino

Comments