peek()


Description

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(). The peek() inherits from the Stream utility class.

Syntax

port.peek()

Parameters

none

Returns

the first byte of incoming serial data available, or -1 if no data is available (int)

Example

#include <PhpocExpansion.h>
#include <Phpoc.h>
#define BUFFER_SIZE 100  // read and write buffer size, reduce it if memory of Arduino is not enough

byte spcId = 1;

ExpansionSerial port(spcId);

byte rwbuf[BUFFER_SIZE];  // read and write buffer

void setup() {
    Serial.begin(9600);
    while(!Serial)
        ;

    Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
    Expansion.begin();
    port.begin("115200N81N");
}

void loop() {
    int txfree = port.availableForWrite();

    // gets the size of received data
    int rxlen = port.available(); 

    if(rxlen > 0) {

        // gets the next byte of incoming serial data without removing
        int value = port.peek(); 
        Serial.print("1st peek : ");
        Serial.println(value);

        // gets the next byte of incoming serial data without removing
        value = port.peek(); 
        Serial.print("2nd peek : ");
        Serial.println(value);

        int len = port.read();

    }
    delay(1);
}