1
votes

Can I have Serial and Serial1 working at the same time?

void setup() {
  Serial.begin(9600);
  while (!Serial);                        // while not open, do nothing
  Serial1.begin(9600);
}

void loop() {
  while (Serial.available() > 0) {        // if at least one char is available
    /* CODE */
    //Serial.write(Serial.read());
  }

  while (Serial1.available() > 0) {
    /* CODE */
    //Serial.write(Serial1.read());
  }
}

When I open the Serial Monitor the first while works good but if I have both, the second one is printing results in a endless loop. How can I solve this?

»» The Arduino Leonardo board uses Serial1 to communicate via RS232 on pins 0 (RX) and 1 (TX). Serial is reserved for USB CDC communication.

Can I use Serial1 exactly like Serial? What kind of signals are read on Serial1?

1
you didn't do while (!Serial1);thejh
didn't solve, serial1 keeps in loopmafap
Is the lower one really Serial.write, too? Not Serial1.write?thejh

1 Answers

0
votes

I'll have to guess here, but I think that the reason is that there's nothing connected to Serial1 that consumes the data you write. Therefore, your code is stuck in the write call.

To verify whether this is indeed the case, you could change Serial1.write(Serial1.read()); (in the second loop) to Serial.write(Serial1.read());. If this doesn't block, my guess was probably right.

http://arduino.cc/en/Guide/ArduinoLeonardo#toc6 says what Serial1 is:

To use the hardware serial port (pins 0 and 1, RX and TX), use Serial1. (See the Serial reference pages for more information.)

So, use Serial1 when you want to communicate over the RX and TX ports / 0 and 1 pins that you can see in the corner of the board.