Serial and Serial1

+2 votes
59 views
asked Mar 15 in Arduino by Manoj
Sorry for my stupid question, but I cannot understand what is going on. Can anyone help me understand please?

This is my code written for Mega

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

void loop() {
  if (Serial.available()) {    Serial.write("From Serial");  }
  if (Serial1.available()) {    Serial1.write("From Serial-1");  }
}

I am only seeing the Serial output and nothing on Serial1 output.

What am I doing wrong here? Sorry I'm all new to this.

1 Answer

+1 vote
answered Mar 15 by Rockey
Dear Ardinon, dont worry about it. Infact it is what expected and almost everyone will be confused with these initially.

This is because that on an Arduino board the primary Serial is used for USB communication with the computer, and is the only thing you can see as output on serial monitor. Note it says Serial monitor and NOT Serial1 monitor :)

For seeing the output of Serial1, you can just write the output to serial whenever a data happens on Serial1. THats it. Dont be confused. Same thing is what happening with the Software serial if you happen to use an Arduino UNO board.

Hope it helped. Code pasted below for you to check.

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

void loop() {
  // 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);
  }
}

Related questions

0 votes
1 answer 89 views
asked Aug 5, 2016 in General by Elliot
–1 vote
0 answers 98 views
–1 vote
0 answers 28 views
asked Apr 6 in Modules by larrystanley (120 points)
0 votes
1 answer 176 views
0 votes
0 answers 84 views
asked Dec 6, 2016 in Laravel by Jinu
0 votes
3 answers 226 views
...