Easy way to select Serial, Serial1, Serial2, etc....

Can I assign the different Serial Objects to a variable to use?

like

SomeGeneric mySerial;
mySerial = Serial2;

mySerial.write();

You can use either a pointer or a reference:

HardwareSerial *MySerial = &Serial;
...
MySerial->print("Hello\n");

or:

HardwareSerial &MySerial = Serial;
...
MySerial.print("Hello\n");

Regards,
Ray L.

Thanks. HardwareSerial is an actual Class/Type?

HardwareSerial is the class defined in HardwareSerial.{h,cpp} - explore your Arduino source code...

My question regarding Serial1 if you have a mega, will you please upload this code and hook pin 0 to pin 18 and hook pin 1 to pin 19

boolean printonce=1;
void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
   if (printonce==1)
   {
      Serial1.println(F("Ready"));
      printonce=0;
  }
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

if you change Serial1.println(F("Ready")); to Serial.println(F("Ready")); it works as you would expect and continuously prints Ready.

However, if you use Serial1.println(F("Ready")); nothing ever prints to the serial monitor...
I need to know if the same thing happens on another Mega to see if it's just my board? If not, what else could cause the sketch to not work properly?

Does Serial1,Serial2,and Serial3 not work? Or is there something in my code that I need to address?

Explain what you are expecting to happen with the code you posted with the described wiring.

Edit, this question is posted in two different discussions, probably best to stick to one.

mistergreen:
Can I assign the different Serial Objects to a variable to use?

Yes, and they don't even have to be hardware serial. The Stream class is used by a lot of things such as SoftwareSerial, I2C and even LCD libraries. So your variable might hold the value of Serial1 or it might be an LCD screen, so you can easily switch from the serial monitor to the LCD.

//PortArray is an array of structures, which is defined elsewhere. 
//The struct contains a member called serial which points to the serial stream for that port
     PortArray[0].serial = &Serial;
     PortArray[1].serial = &Serial1;
     PortArray[2].serial = &Serial2;
     PortArray[3].serial = &Serial3;     
     PortArray[4].serial = &SerialUSB;

Thanks, the reason I asked was I have this class that uses SoftwareSerial but SoftwareSerial pins are limited and nonexistent if you have you have a ethernet card on that takes up all of the SPI pins.

So I wrote another overloading constructor accepting different hardware serials.

.ino
kSeries K_30(19,18, Serial1); //x,y,z  x=rx, y=tx, z=Serial type

.h
kSeries(uint8_t Rx, uint8_t Tx);
kSeries(uint8_t Rx, uint8_t Tx, HardwareSerial &inserial);
HardwareSerial* _hSerial; //hardware serial

.cpp
kSeries :: kSeries(uint8_t Rx,uint8_t Tx,HardwareSerial &inserial)
{
    
    _hSerial = &inserial;
    _hSerial->begin(9600);
    Serial.println("hardware serial init");
    hardware = true;
}

Explain what you are expecting to happen with the code you posted with the described wiring.

Edit, this question is posted in two different discussions, probably best to stick to one.

it's been three days and I haven't gotten a response on that post though. I can't move farther on my project until I figure this out. I thought maybe to OP would try it out on his mega and tell me if he got the same results so I can figure out if it's just my board. I posted a link to my original post when I posted that question so its not like I was trying to hide anything.

Only

Thomas499:
it's been three days and I haven't gotten a response on that post though. I can't move farther on my project until I figure this out. I thought maybe to OP would try it out on his mega and tell me if he got the same results so I can figure out if it's just my board. I posted a link to my original post when I posted that question so its not like I was trying to hide anything.

Only Serial prints to Serial monitor I think. I've tried Serial1.print() and it doesn't show up in the serial monitor.

Only Serial prints to Serial monitor I think. I've tried Serial1.print() and it doesn't show up in the serial monitor.

Yes, but I connected the rx of Serail1 to the tx of Serial and the tx of Serial1 to the rx of Serial. So shouldn't Serial1 send its text to Serial?

if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}

 // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }

This looks like an infinite loop for me. Nothing is coming through the Serial monitor.

Yes, but I connected the rx of Serail1 to the tx of Serial and the tx of Serial1 to the rx of Serial. So shouldn't Serial1 send its text to Serial?

Just trying to do some mind reading, are you wanting the final text to appear in the serial monitor? If so, you will need to connect pin 18 to pin 1 instead of pin 0.

On the MEGA, UNO and Due Programming Port, Tx (pin 1) is the serial monitor. You could hook any other serial Tx up to that pin and see it on the monitor, so long as you never call Serial.begin() on the primary serial. If you did that you would effectively have two outputs fighting each other.

If you did that you would effectively have two outputs fighting each other.

If it were necessary to do so, a slight delay could be put in the code between the receiving and re-transmission of what was received. I think electrically one tx can pull another connected tx low enough such that the also connected rx would still get the needed low condition.

Just trying to do some mind reading, are you wanting the final text to appear in the serial monitor? If so, you will need to connect pin 18 to pin 1 instead of pin 0.

according to the arduino Mega pin layout page

Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX);

So you are saying that I need to connect RX to Rx and Tx to Tx? I thought Rx always connects to Tx and Tx always connects to Rx... when I tried it your way, neither Serial.print or Serial1.print work..

What I want is to use Serial1 hardware as the rx and tx of a HC-06 bluetooth chip so that two arduinos can talk to each other. Right now, I am trying to debug why my code didn't work, so I simplified it because I think the tx on Serial1 doesn't work properly.

I need to be able to use the serial monitor to display the text so I know what is going on, but I also need to be able to use the bluetooth chip for communication. When I use port 1 and port 0 for the bluetooth chip, it works, but I can't use the serial monitor for the rest of the code with that setup. When I use Serial1 rx and tx for the bluetooth chip, it doesn't work correctly for some reason as explained below.

The orignal code was this

const int ledPin=32;
const int ledPin2=33;
boolean ledState=LOW;
String readString;
unsigned long previousMillis=0;
const int interval=2000;

void setup() 
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial1.begin(9600);
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
Serial.println("Setup Finished");
}

void loop() // run over and over
{blinkwithoutDelay();
checkBluetooth();
}
void blinkwithoutDelay(){
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;   
    ledState=!ledState;
    if (ledState==HIGH)
    Serial1.println(F("led2 is on"));
    else
    Serial1.println(F("led2 is off"));
    digitalWrite(ledPin2, ledState);
  }
}
void checkBluetooth()
{
  if (Serial1.available())
  {
    Serial.println(F("something available"));
    Serial.print(F("Number of bytes available: "));
    Serial.println(Serial1.available());
    Serial.print(F("The first Byte is a: "));
   // Serial.println(Serial1.read());
    char c =Serial1.read();
    readString+=c;
    Serial.write(c);
    Serial.println();
    Serial.print(F("The readString is: "));
    Serial.println(readString);
    Serial.println(F("==========end of this loop============"));
    Serial.println();
    if (readString.endsWith("On"))
    {
      digitalWrite(ledPin,HIGH);
      Serial.println(F("led1 is high"));
    }
    else if (readString.endsWith("Off"))
    { digitalWrite(ledPin,LOW);
      Serial.println(F("led1 is low"));
    }
  }
}

This was the serial readout

something available
Number of bytes available: 1
The first Byte is a:
The readString is:
==========end of this loop============

The first Byte was a '/n' I think... but then nothing came after it. I am trying to figure out why it only said there was one Byte, when there should have been 6? So i hooked up the setup and code that I previously posted.

How about not connecting the Serial pins together.

do like
if(Serial1.available>0) {
Serial.print(Serial1.read());
}

In the previous post, I didn't have the Serial pins connected to each other. Only when that didn't work, did I start debugging and attached the Serial pins together and wrote the code posted on response #3 on this thread to see if it was the bluetooth chip, the code, the wires, or the arduino that was the culprit. If you follow load the code on #3 and follow the directions, you will see Serial.print(F("ready")) works and prints to the serial monitor correctly, but if you change it to Serial1.print(F("ready")) nothing prints.

So you are saying that I need to connect RX to Rx and Tx to Tx? I thought Rx always connects to Tx and Tx always connects to Rx... when I tried it your way, neither Serial.print or Serial1.print work..

An important concept to understand is what is connected to the mega TX0 and RX0 pins. TX0 is connected to the mega TX pin and the onboard USB/serial chip RX pin. RX0 is connected to the mega RX pin and the onboard USB/serial chip TX pin. So any communication to the serial monitor from the mega must go via the TX0 pin. This could result in two TX pins connected to TX0, the normal mega TX pin, and any other TX pin (via a wire jumper) that needs to directly send to the serial monitor. There are various setups and mods, but understanding the TX0 and RX0 basic connections is needed for some projects.

An important concept to understand is what is connected to the mega TX0 and RX0 pins. TX0 is connected to the mega TX pin and the onboard USB/serial chip RX pin. RX0 is connected to the mega RX pin and the onboard USB/serial chip TX pin. So any communication to the serial monitor from the mega must go via the TX0 pin. This could result in two TX pins connected to TX0, the normal mega TX pin, and any other TX pin (via a wire jumper) that needs to directly send to the serial monitor. There are various setups and mods, but understanding the TX0 and RX0 basic connections is needed for some projects.

So TX0--->TX--->onboard USB/serial chip RX
RX0--->RX--->onboard USB/serial chip TX
It's not clicking why that would help me...

Heres the results I got using this code

boolean printonce=1;
void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
   if (printonce==1)
   {
      Serial1.println(F("Ready"));
      printonce=0;
  }
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

Experiment 1:
Tried two ways,
way 1: hooked Rx0 to Rx1, Tx0 to Tx1
way 2: hooked Rx0 to Tx1, Tx0 to Rx1

Results: Nothing prints to Serial monitor

Experiment 2:
changed Serial1.println(F("Ready")); to Serial.println(F("Ready"));
Tried two ways,
way 1: hooked Rx0 to Rx1, hooked Tx0 to Tx1
way 2: hooked Rx0 to Tx1, hooked Tx0 to Rx1

Results:
using way 1: Nothing prints to Serial monitor
using way 2: Ready prints again continuously in an infinite loop

Question.... Why is it impossible to get Serial1.println(F("Ready")); to print to Serial monitor by attaching wires to rx0, tx0, rx1, tx1.... ?