6 Replies Latest reply on Dec 2, 2014 7:02 AM by Intel_Peter

    Serial.readBytes and Serial.readBytesUntil - Eat processor

    KurtE

      Not sure if question and/or suggestion, but as part of a sketch I am trying to read a line of text in from the terminal, so I have a line like:

            int cbRead =  Serial.readBytesUntil(13, abBuf, sizeof(abBuf));

       

      I found this is eating lots of CPU speed.  Why?  They both call Stream::timedRead, which is written like:'

      int Stream::timedRead()

      {

        int c;

        _startMillis = millis();

        do {

          c = read();

          if (c >= 0) return c;

        } while(millis() - _startMillis < _timeout);

        return -1;     // -1 indicates timeout

      }

       

      So this code is going to hard spin, until it times out or gets the number of characters or the terminator character or times out...   This is fine on small atmega chips, but probably not the best for a linux box such as this.

       

      Suggest at a minimum add a yield or small delay.