Serial1 prints garbage or integers between two boards

I'm trying to make two arduino mega2560 communicate between them by using Serial and Serial1 in this way:

Arduino1:

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial1.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:

    Serial1.println("hello world");
  
   delay(500); 
}

and Arduino 2:

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial1.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial1.available() > 0) {
    Serial.print("I received: ");
    Serial.println(Serial1.read());
  }
  else {
    Serial.println("Nothing received at all!");
    }
   delay(500); 
}

The problem is that I only receive this:

I received: 104
I received: 101
I received: 108
I received: 108
I received: 111
I received: 32
I received: 119
I received: 111
I received: 114
I received: 108
I received: 100
I received: 13
I received: 10

which I guess it is the conversion from string to DEC for "hello world\n". Is it correct?
What should i do in order to prints the ASCII string?

If you want to print as char you have to do this Serial.println( (char)Serial1.read() );

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial1.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {


 while (Serial1.available() > 0)
 {
   Serial.println( (char)Serial1.read() );
 }


}

It prints always and only integers.

Should I use a buffer to read from keyboard and then send the buffer to Serial1?

How do you connect both Arduinos? How many wires?

Rx1 to Tx1
Tx1 to Rx1
GND to GND

I can receive the data in the second board, but they are not in ASCII, but in DEC.

This happens only when i use the keyboard to send the strings to the second board. If I hard code the value of the string, then I'm able to receive the full string in ASCII on the second board.

For example, if I send the data in this way:

Serial1.println("hello world");

then I receive it in ASCII on the second board while if I send the same string by typing it in the serial monitor as

Serial1.println(Serial.read());

then I receive the same data but in DEC format.

A string is printed as such, but Serial.read() returns single characters that should be passed on by Serial.write() without translation of the character codes into decimal numbers.

should become
Serial1.write(Serial.read());

Should I loop on the serial.read() by reading each character separately and by storing them into a buffer and then print the whole string?

I tried it too but it results always in DEC format.

Try
Serial1.write((char)Serial.read());

1 Like

Yes, it works!
Obviously, it reads just a single char, so now I'm going to use a buffer to store more characters and prints the final string all in once.

Thank you!