I'm trying to have a single project that can either send/receive strings via USB serial or Bluetooth. I have a command to switch between Serial and Serial2. It doesn't seem to work the way I expect. When I send a "BTON" command, the print statement prints to Serial, switches to Serial2 and properly prints to Serial2. But when I send the "BTOFF" command immediately after "BTON", both print statements print to Serial when I expected the 1st print command to go to Serial2. It seems as though the "Stream& MySerial = Serial2;" does not persist. Can anyone provide help?
Thanks.
Thanks.
Code:
uint8_t argCount;
uint8_t thisArg = 0;
char *argv[30];
bool BT = false;
Stream& MySerial = Serial;
void setup() {
Serial.begin(115200);
Serial2.begin(115200);
}
void loop() {
static char buffer[80];
if ((readline(Serial.read(), buffer, 80) > 0) || (readline(Serial2.read(), buffer, 80) > 0)) {
char tmpBuffer[sizeof(buffer)-2];
strcpy(tmpBuffer, &(buffer[2]));
cmd_parse(buffer);
if (!strcmp(argv[0],"BTON")) {
MySerial.println("Bluetooth Turning on"); // This properly prints to Serial
Stream& MySerial = Serial2;
MySerial.println("Bluetooth is turned on"); // This properly prints to Serial2
}
else if (!strcmp(argv[0],"BTOFF")) {
MySerial.println("Bluetooth Turning off"); // THIS DOES NOT PRINT TO SERIAL2
Stream& MySerial = Serial;
MySerial.println("Bluetooth is turned off"); // This prints to Serial
}
}
}