Go Down

Topic: NewSoftSerial (or SoftSerial on 1.0) - no peek()? (Read 790 times) previous topic - next topic

tampapoet

Hello all.  This is my first time posting.

The Serial library has a peek() function, but the NewSoftSerial doesn't.  Is this true?  And are there any workarounds?

Any help would be greatly appreciated.  Thanks!

Morris Dovey

You could try something as complex as (untested):

Code: [Select]
unsigned char peek(unsigned char *where)
{  return *where;
}
There's always a better way!

tampapoet

Here is the definition of the peek function from the Serial Lib:

Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer. That is, successive calls to peek() will return the same character, as will the next call to read(). peek() inherits from the Stream utility class.

This is the functionality that I'm looking for in NewSoftSerial (now called SoftSerial in Arduino 1.0).

Thanks!

ajofscott

If the object you wish to peek at is a subclass of the Stream class, then peek is a public function. See Below:
Code: [Select]
class Stream : public Print
{
  public:
    virtual int available() = 0;
    virtual int read() = 0;
    virtual int peek() = 0;
    virtual void flush() = 0;
};

Taken from Stream.h

ajofscott

HardwareSerial has a peek member, SoftwareSerial does not and didn't in 0023 either as it was 0023 that I opened to look for peek(). But, here was the peek function in HardwareSerial:
Code: [Select]
int HardwareSerial::peek(void)
{
  if (_rx_buffer->head == _rx_buffer->tail) {
    return -1;
  } else {
    return _rx_buffer->buffer[_rx_buffer->tail];
  }
}

ajofscott

The reason for no peek is due to the lack of buffer, the read function assembles the byte returned on the fly.

mikalhart

Lots of stuff to clear up here...

First, there is a peek in SoftwareSerial in Arduino 1.0.  It's also in NewSoftSerial 12, which is essentially identical.  (NewSoftSerial was incorporated into the Arduino core in version 1.0 and renamed "SoftwareSerial".  NewSoftSerial should only be used in pre-release Arduino 0023 and earlier.)

Lastly, there is a buffer in the (new) SoftwareSerial.

Mikal

Go Up