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.
port.peek()
none
the first byte of incoming serial data available, or -1 if no data is available (int)
#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 len = port.available();
Serial.print("available length before peek(): ");
Serial.println(len);
char value = port.peek();
Serial.print("peek() value: ");
Serial.println(value);
len = port.available();
Serial.print("available length after peek(): ");
Serial.println(len);
Serial.println("----------");
// remove one byte from the buffer
char data = port.read();
}
delay(1);
}