Testing sketch to run on an Arduino Mega to print all output from connected serial ports to the terminal.
Testing sketch to run on an Arduino Mega to print all output from connected serial ports to the terminal.
1/** =========================================================================
2 * @example{lineno} mega_serial_spy.ino
3 * @brief Testing sketch to run on an Arduino Mega to print all output from
4 * connected serial ports to the terminal.
6 * @m_examplenavigation{page_extra_helper_sketches,}
7 * ======================================================================= */
11void changeBauds(void) {
13 for (uint8_t i = 1; i < 4; i++) {
14 Serial.print("Select a baud rate for Serial");
16 Serial.println(" to monitor at:");
18 Serial.println("[1] - 9600");
19 Serial.println("[2] - 57600");
20 Serial.println("[3] - 115200");
21 Serial.println("[4] - 74880");
24 "Enter you selection in the Serial Monitor and press <enter>");
27 String user_input = "";
30 // Wait for user feedback, then parse feedback one byte at a time
31 while ((Serial.peek() != 255) && !selection) {
32 char incoming = Serial.read();
33 if (isDigit(incoming)) {
34 // Append the current digit to the string placeholder
35 user_input += static_cast<char>(incoming);
37 // Parse the string on new-line
38 if (incoming == '\n') { selection = user_input.toInt(); }
47 default: baud = 9600; break;
48 case 2: baud = 57600; break;
49 case 3: baud = 115200; break;
50 case 4: baud = 74880; break;
54 Serial.print("Starting Serial");
58 Serial.println(" baud.");
61 case 1: Serial1.begin(baud); break;
62 case 2: Serial2.begin(baud); break;
63 case 3: Serial3.begin(baud); break;
72 // Wait for the Serial Monitor to open
74 // Delay required to avoid RTOS task switching problems
78 delay(500); // Short delay for cosmetic reasons
80 Serial.println("Serial port Spy\r\n");
81 Serial.println("-----------------------------------------------------------"
82 "--------------------");
87 // From HW UART1 to USB
88 while (Serial1.available()) Serial.write(Serial1.read());
89 // From HW UART2 to USB
90 while (Serial2.available()) Serial.write(Serial2.read());
91 // From HW UART4 to USB
92 while (Serial3.available()) Serial.write(Serial3.read());
94 if (Serial.available()) changeBauds();