Variable for Descriptor

Hi,

This involves an ESP32-WROOM-32d.

Trying to write a single set of Serial Read/Writes that can be used for either a Bluetooth or hardware Serial connection after whichever is tested to exist.

Don't want to write duplicate code, one using "Serial.println;" and another "SerialBT.println;"...

Is there a way to make a variable to call whichever Serial connection exists, which can then be used in a single set of serial read/writes???

"serialvariable.println;" My first thought was an array, but can't seem to get it working.

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial


#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;

//int SerVar[] = {Serial, SerialBT};  //HOW TO DECLARE ARRAY OF THESE TWO COMMANDS ???
int SerNo;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name  
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {                 //This WORKS but requires DUPLICATE code for each type of Serial connection.
  if (Serial.available()) {   //UART connection
    Serial.println("Test");
    delay(10);
  }
  if (SerialBT.available()) { //Bluetooth Serial connection
    SerialBT.println("Test");
    delay(50);
  }

}

void Loop_alternative() {     //This attempt is based on an array.
  if (SerialBT.available()) {   // Tests for Bluetooth Serial connection
    SerNo = 1;
  } else {                      // Uses UART if no Bluetooth Serial connection
    SerNo = 0;
  }
  
  SerVar[SerNo].println("Test");  //BT Serial is priority and UART is secondary.
  //Add more universal Serial Read and Write coding for both UART and Bluetooth.
  //Duplicate code not necessary.
}

Serial is inherited from Stream class. If BluetoothSerial is also inherited from Stream class, you can make an array of Stream

BluetoothSerial SerialBT;
Stream *serials[] = {&Serial, &SerialBT}

void setup()
{
   serials[0]->begin(9600);
   serials[1]->begin(9600);
}

arduino_new:
Serial is inherited from Stream class. If BluetoothSerial is also inherited from Stream class, you can make an array of Stream

BluetoothSerial SerialBT;

Stream *serials = {&Serial, &SerialBT}

void setup()
{
  serials[0]->begin(9600);
  serials[1]->begin(9600);
}

arduino_new:
Serial is inherited from Stream class. If BluetoothSerial is also inherited from Stream class, you can make an array of Stream

BluetoothSerial SerialBT;

Stream *serials = {&Serial, &SerialBT}

void setup()
{
  serials[0]->begin(9600);
  serials[1]->begin(9600);
}

Thank you for responding. When I tried your recommendation it gave the error for both lines: "'class Stream' has no member named 'begin'" Any thoughts?

Hmm, Stream doesn't have begin().

Try this:

...
void setup()
{
   Serial.begin(9600);
   SerialBT.begin("ESP32Test");

   //check serial buffer
   if (serials[0]->available()) {
      serials[0]->read();
   }

   check Bluetooth buffer
   if (serials[1]->available()) {
      serials[1]->read();
   }
}

This works without error... and something new for me. So thank you for that.

I do have a question about the "Stream *serials = {&Serial, &SerialBT};"

what does the '*" and "&" mean?

arduino_new:
Hmm, Stream doesn't have begin().

No, because not all classes that inherit from Stream need one.

Bwanna:
This works without error... and something new for me. So thank you for that.

I do have a question about the "Stream *serials = {&Serial, &SerialBT};"

what does the '*" and "&" mean?

Google search for tutorials on C / C++ pointers. Here's one.

After that comes Polymorphism which is the technique @arduino_new's code took advantage of.

gfvalvo:
No, because not all classes that inherit from Stream need one.

Agree.

Update: Thank you for the guidance on this. The links are very interesting and something I'll continue to review. Here is the working code. It'll save a lot of duplication and allow me to connect via BT (phone) or direct UART (PC) when interfacing with the unit. This is why I enjoy this site. Great folks, great help!

//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial


#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;

Stream *serials[] = {&Serial, &SerialBT};

int SerNo;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name  
  Serial.println("The device started, now you can pair it with bluetooth!");
}


void loop () {
  if (SerialBT.available()) {   // Tests for Bluetooth Serial connection
    SerNo = 1;
  } else {                      // Uses UART if no Bluetooth Serial connection
    SerNo = 0;
  }
  
  serials[SerNo]->println("Test");  //BT Serial is priority and UART is secondary.

  //Add more universal Serial Read and Write coding for both UART and Bluetooth.
  //Duplicate code not necessary.
}