[SOLVED] Serial print capture: is it possible?

Hello,

Is it possible to read & capture "Hello world" string produce by Serial.print?

String serialReceive;

void setup() {
   Serial.begin(115200);
   Serial.println(""); 
}

void loop() {

  Serial.println("Hello world"); 
  
  if( Serial.available() > 0 ){ 
      if(Serial.readString().indexOf("Hello") > -1){        
          serialReceive = Serial.readString();
      }
  }

}

Tried with this code:

void setup() {
   Serial.begin(115200);
   Serial.println(""); 
}

void loop() {

  Serial.println("Hello world"); 
  Serial.println(Serial.available());
  Serial.println("Hello world"); 
  Serial.println(Serial.read());
  Serial.println("Hello world"); 
  Serial.println(Serial.readString());

}

The results:

  • Serial.println(Serial.available()) = 0
  • Serial.println(Serial.read()) = -1
  • Serial.println(Serial.readString()) = blank

Is it possible capture Serial.print?

How? :frowning:

No, you can't do this. I guess you could loopback Serial into a SoftwareSerial port, but why? That would just be ridiculous unless it was just to test the wiring or something.

The real question is why? Why? You are in control of the program sending the data. Why would you need to read back the data to know what you sent?

char* myString = "Hello World!";

Serial.print(myString);

And now you have the pointer myString that points to what was printed.

Delta_G:
No, you can't do this. I guess you could loopback Serial into a SoftwareSerial port, but why? That would just be ridiculous unless it was just to test the wiring or something.

The real question is why? Why? You are in control of the program sending the data. Why would you need to read back the data to know what you sent?

char* myString = "Hello World!";

Serial.print(myString);




And now you have the pointer myString that points to what was printed.

Yes, I know I am in control of the program.

Just curious maybe could be loop back again.

Thanks a lot for the answer.

when you "Serial.print()" something, the associated ASCII representation of your data goes away on the Serial port through the Tx pin (D1). When you try to read something on Serial, your arduino scans what’s coming on pin Rx (D0).

Challenge to see anything is that pin 0 and 1 are mapped to your Serial console through the USB cable

if you have a MEGA, connect a jumper wire between pins 16 and 17 (Serial2 Tx2 to Serial2 Rx2) and you could try the following (untested) code

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  Serial2.print("Hello World");
}

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

The code sends on Tx2 some data, and because of the wire, this data will arrive into Rx2. The loop() just listens on this Rx2 to see if something is coming in and when something is there, it prints it to the basic Serial (the one you can see on the Arduino Serial Monitor). if all goes fine, you should see “Hello World” in the Arduino monitor (if you set it at 115200 bauds)

if you don’t have a MEGA, as Delta_G suggests, you can use Sowftare Serial… but as he also explains, there is very limited value in doing this (besides playing around)…

don’t connect Rx to Tx and have USB Serial console plugged in at the same time…