Loopback test for Serial1 on Mega 2560

I have to verify if my Serial1 port on Mega 2560 is functional. While its easy to do a loop back on the main Serial port using the Serial Monitor how do we do it for hardware serial ports like Serial1, Serial2 etc ? Will something like this need to be done after Shorting the Tx and Rx of Serial1 ?

Type a string in serial monitor
Read the same into Serial1
Write the same into serial monitor

have a look at

// Arduino Mega serial1 test

// mega Serial1 pin 18 is Tx
//      Serial1 pin 19 is Rx
// for loopback test connect pin 18 to pin 19

// for RS232 shield connect
// Mega pin 18 TXD to TTL/RD232 Tx
// Mega pin 19 RXD to TTL/RS232 Rx
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
// connect GND pins together and VCC to 5V

void setup() {
  Serial.begin(115200);  // initialise serial monitor port
  Serial1.begin(9600);  //115200);  // initialise Serial1
  Serial.write("Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19\n");
  Serial.write("RS232: Mega pin 18 TXD to TTL/RD232 Tx and pin 19 RXD to TTL/RS232 Rx\n");
  Serial.write("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  if (Serial1.available()) {  // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  }
  if (Serial.available()) {  // read from Serial outut to Serial1
    char inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

connect pin 18 to pin 19 to form a loopback
text entered on serial monitor is echoed back to the display

1 Like

Your textual codes make sense and they work well when I have converted them into programming codes and have tested on MEGA. TX1-pin is shorted to RX1-pin.

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
  byte n = Serial.available();
  if ( n != 0)
  {
    char y1 = Serial.read();
    Serial1.print(y1);
    //---------------------------
    byte m  = Serial1.available();
    if ( m != 0)
    {
      char y2 = Serial1.read();
      Serial.print(y2);
    }
  }
}

Output:

Forum
Cooperation
2 Likes

Heh:

with a reconfig of

1 Like

Thanks to all of you who responded ! Problem solved. !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.