ExpansionSerial()


Description

This function creates a new instance of the ExpansionSerial class that represents a serial port. All the member functions of ExpansionSerial class inherit from the Stream utility class.

Syntax

ExpansionSerial(sid)

Parameters

sid - slave ID

Returns

none

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;

// Creates a new instance of the ExpansionSerial Class
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(F("115200N81N"));
}

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

    if(rxlen > 0) {
        if(rxlen <= txfree) {
            int rwlen; // read and write length

            if(rxlen <= BUFFER_SIZE) 
                rwlen = rxlen;
            else
                rwlen = BUFFER_SIZE;

            // receive data
            rwlen = port.readBytes(rwbuf, rwlen);

            // send data
            port.write(rwbuf, rwlen);

            // print data to serial monitor of Arduino IDE
            Serial.write(rwbuf, rwlen);
        }
    }

    delay(1);
}