4

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

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)"

kimliv
  • 561
  • 1
  • 4
  • 17

1 Answers1

4

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"

BrettFolkins
  • 4,441
  • 1
  • 15
  • 26