Pages: [1]   Go Down
Author Topic: NewSoftSerial (or SoftSerial on 1.0) - no peek()?  (Read 710 times)
0 Members and 1 Guest are viewing this topic.
Tampa, FL
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could try something as complex as (untested):

Code:
unsigned char peek(unsigned char *where)
{  return *where;
}
Logged

There's always a better way!

Tampa, FL
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the object you wish to peek at is a subclass of the Stream class, then peek is a public function. See Below:
Code:
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
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int HardwareSerial::peek(void)
{
  if (_rx_buffer->head == _rx_buffer->tail) {
    return -1;
  } else {
    return _rx_buffer->buffer[_rx_buffer->tail];
  }
}
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: