What happens when Serial1 is directly connected to Serial2/3 on Mega 2560?

Hi guys!

Just for sake of curiosity, what happens if you directly connect Serial1 to Serial2/Serial3 on a Mega 2560? I thought since they all work with TTL signals, no level converter is needed for them to talk to one another, right? But when I tested this theory on the actual hardware, it looks like the receiving serial port will always receive a char that has an extended-ascii value of 152, no matter what you sent to it.

Interesting...

I'm eager to hear any idea or explanation from all of you :slight_smile:

BTW, I did make sure that the TX pin of one port is connected to the RX pin of another port.

Easy. Go to the bathroom and look in the mirror. Talk to your heart's content. What good does it do? Your family will think you have lost your mind.

SurferTim:
Easy. Go to the bathroom and look in the mirror. Talk to your heart's content. What good does it do? Your family will think you have lost your mind.

I think it's called 'academic curiosity'.

On the Mega, it is a four port hardware serial device. Just like in the bathroom mirror, it will work, but you will only hear yourself talk. :roll_eyes:

But when I tested this theory on the actual hardware

With actual software?

"...it will work, but you will only hear yourself talk."

Actually, the problem is that it will NOT work (didn't work for me at least), all I got was a meaningless char. My point is, if Serial1-3 all talk in TTL, why wiring them up directly won't work? What's missing here?

What's missing here?

Your software

Okay, so the insane code may look like this:

void setup() {
  // initialize serial:
  Serial.begin(9600); // For monitoring on PC
  Serial1.begin(9600); // Sender
  Serial3.begin(9600); // Receiver
}

void loop() {
  Serial1.println("Doesn't matter what you say here."); // Send from Serial1 to Serial3, so wire them up accordingly, directly, without anything like a MAX232
  delay(2000);
  
  if (Serial3.available()) {
    // get the new byte:
    char inChar = (char)Serial1.read();
    Serial.println(inChar); // Send to PC what Serial3 received
  }
}

To simplify the situation, I just sent Serial1.println('A'); during testing

Try reading your sketch again.
Carefully.

I do not like talking to myself in public, but this works. It prints a 't' every second.

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial3.begin(9600);  
}

void loop()
{
  Serial1.write("t");
  delay(1000);
  Serial.write(Serial3.read());
}

AWOL:
Try reading your sketch again.
Carefully.

Could you give me a clue? Since I'm really clueless. Tks!

Which port do you check to see if there is a character available?
Which port do you read that character from?

You are checking the wrong serial port for available. And use "while", not "if". Read it all, not just one character.

  while (Serial3.available()) {
    // get the new byte:
    char inChar = Serial3.read();
    Serial.print(inChar); // Send to PC what Serial3 received
  }

I seem to be a minute behind AWOL. :slight_smile:

AWOL:
Which port do you check to see if there is a character available?
Which port do you read that character from?

:sweat_smile: yeah, that's a really stupid mistake... Thanks a lot! So, what was I reading then? Something random in Serial1's buffer?

@SurferTim, thanks a lot too! You're totally right, it works.

I am a little puzzled that it isn't reading -1 (empty buffer), but I don't have a Mega, so can't try it.

I seem to be a minute behind AWOL.

Almost three

AWOL:
I am a little puzzled that it isn't reading -1 (empty buffer), but I don't have a Mega, so can't try it.

I do, and it does read -1 when empty. I don't know why he was not getting the "funny y".

edit: I do if the OP did not have the tx/rx lines connected the other way. That would leave the Serial1 rx line floating.

This is the exact code that was producing the funny y on my board (and it still is):

void setup() {
  // initialize serial:
  Serial.begin(9600); // For monitoring on PC
  Serial1.begin(9600); // Sender
  Serial3.begin(9600); // Receiver
}

void loop() {
  Serial1.print('A'); // Send from Serial1 to Serial3, so wire them up accordingly, directly, without anything like a MAX232
  delay(2000);
  
  if (Serial3.available()) {
    // get the new byte:
    char inChar = (char)Serial1.read();
    Serial.println(inChar); // Send to PC what Serial3 received
  }
}

I've even power off/reset the board several times by now.

This is the exact code that was producing the funny y on my board (and it still is):

And it should!

edit: You are checking Serial3 for characters, then trying to read Serial1. Nothing there. It is in Serial3's buffer.

This is the exact code that was producing the funny y on my board (and it still is):

Yup, that's -1.
Why did you say it was 152 (0x98)?

Why not print out the hex value, rather than the ASCII code?

 Serial.println(inChar, HEX); // Send to PC what Serial3 received

AWOL:

This is the exact code that was producing the funny y on my board (and it still is):

Yup, that's -1.
Why did you say it was 152 (0x98)?

Why not print out the hex value, rather than the ASCII code?

 Serial.println(inChar, HEX); // Send to PC what Serial3 received

The hex value is FFFFFFFF