Receiving Data


Getting Received Data Size

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.

Peeking a Byte

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.

Reading a Byte

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.

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) {

        // reads the next byte of incoming serial data
        int value = port.read(); 
        Serial.print("read : ");
        Serial.println(value);

    }
    delay(1);
}