user233: I have the following circuit and want to use the Arduino Mega 2560 as an debugger for an other Microcontroller.

So I am looking for the code to redirect the UART input from "P17 (RX2)" to the serial USB and the data coming from the serial USB to the output of "P16 (TX2)"

user1585: A very simple sketch to pass serial through looks like this:
void setup(){
  Serial1.begin(/*BAUD*/);
  Serial2.begin(/*BAUD*/);
}

void loop(){
  if(Serial1.available()){
    Serial2.print((char)Serial1.read());
  }
  if(Serial2.available()){
    Serial1.print((char)Serial2.read());
  }
}

I would advise both run at the same baud rate, otherwise you may need to do some extra buffering yourself if the arduino starts running out of buffer space. If you want to use the mega's USB connection instead of "Serial1" like your schematic, just switch "Serial1" to "Serial"

233: <p>I have the following circuit and want to use the Arduino Mega 2560 as an debugger for an other Microcontroller.
<img src="https://i.stack.imgur.com/uEZTh.png" alt="circuit"></p>

<p>So I am looking for the code to redirect the UART input from "P17 (RX2)" to the serial USB and the data coming from the serial USB to the output of "P16 (TX2)"</p>
 1585: <p>A very simple sketch to pass serial through looks like this:</p>

<pre><code>void setup(){
  Serial1.begin(/*BAUD*/);
  Serial2.begin(/*BAUD*/);
}

void loop(){
  if(Serial1.available()){
    Serial2.print((char)Serial1.read());
  }
  if(Serial2.available()){
    Serial1.print((char)Serial2.read());
  }
}
</code></pre>

<p>I would advise both run at the same baud rate, otherwise you may need to do some extra buffering yourself if the arduino starts running out of buffer space. If you want to use the mega's USB connection instead of "Serial1" like your schematic, just switch "Serial1" to "Serial"</p>
