You can get the received data size from the serial port by using available() function.
port.available()
This function returns the data size (bytes in integer) which can be read from the serial port.
You can peek the first byte in the receive buffer by using peek() function.
port.peek()
The byte returned by this function remains in the buffer.
You can read the first byte in the receive buffer by using read() function.
port.read()
The byte returned by this function removed from the buffer.
#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("115200N81T");
}
void loop() {
int txfree = port.availableForWrite();
// gets the size of received data
int rxlen = port.available();
if(rxlen > 0) {
// reads the next byte of incoming serial data
int value = port.read();
Serial.print("read : ");
Serial.println(value);
}
delay(1);
}