Arduinom MEGA 2560 - using of Serial 1-2-3 ports

Hi, Guys!

I read in more forum themes, that MEGA 2560 microcontroller has 3 additional Serial Ports (RX1-TX1, RX2-TX2, RX3-TX3).

I've found a very good Serial Test from zoomkat, which uses "programmer" Serial Port (0-1).

// ***********************
String readString;

void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}

if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}

This sketch is working very well, but I'd like to use in place of programmer serial port one of the additional serial ports: 1,2,3.

I modified a bit this sketch, like that:

// ***********************
String readString;

void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

while (Serial.available()) // I LEFT Serial, NOT CHANGED TO SERIAL1 {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial1.read();
readString += c;
}

if (readString.length() >0) {
Serial.println(readString);

readString="";
}
}

Nothing happened, the serial communication is dead... Please help me, WHAT did I do wrong?

Thanks: rcph

Please provide a detailed description of what you are trying to accomplish.

rcpilot_hun:

  while (Serial.available())  // I LEFT Serial, NOT CHANGED TO SERIAL1 {

delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial1.read();
    readString += c;
  }

This doesn't make sense. Why would you check if data is available on Serial, and then read from Serial1?

What do you have sending data to serial1?

Also, you are checking for characters on Serial and then trying to read From Serial1. They are separate ports, they're not interchangeable.

You seem to be engaged in a nonsense exercise. There is no more need to "test" serials1,2,3 than there is to test serial0 on pins0,1, and who ever did that? If you just put serial devices on the ports and use them in the normal manner, you will probably find that nothing is dead.

It seems, that the problem is solved.

serial-monitor3.ino (1.18 KB)