read()


Description

Reads incoming serial data. The read() inherits from the Stream utility class.

Syntax

port.read()

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

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

    }
    delay(1);
}