Serial communication comes back to sender

Hi folks!

I have an issue with 2 arduinos Mega using Serial bi-directional communication.

The setting
Arduino A and Arduino B are connected : TX.A with RX.B, TX.B with RX.A, and GND.A with GND.B.
There is a 10 meters distance, with possible interference so the Serial speed is set to 1200bps.
I added a 10k pull-up resistor to both RX pins to avoid floating datas when the other arduino is disconnected.

The issue
When Arduino A writes, Arduino B can read perfectly.
When Arduino B writes, Arduino A doesn't read anything BUT Arduino B reads the bytes it just sent.
If i disconnect RXa or TXa, then Arduino B does not receive his own bytes back. So i guess the bytes do go through Arduino A somehow.

What i tested
Both Arduino use Serial1. I tried to use Serial3 on Arduino A, and then the bug was inverted ! Arduino B can read what Arduino A sent, but Arduino A reads itself.

When i made test at the beginning of my project, it worked fine. So maybe the issue comes from the distance but i would not know why it would have anything to do.
I could put the 2 Arduinos closer to test again, but they are not easy to move now so i would rather not, except if nobody can help me with that !

But, you have not posted your codes!

TTL version of the asynchronous data/signal can travel upto 50' (feet) without much appreciable distortion. 10 meter is about 32 feet; so, distance should not be a practical problem. Please, post your codes with code tags (</>).

My code is quite long and complicated, so i ran this small test program and had the same issue :

byte incByte;
byte seng=0;

void setup() {
 Serial.begin(115200);
 Serial1.begin(1200);
}

void loop() {
 if (Serial1.available()>0) {
   incByte=Serial1.read();
   Serial.print("Input=");
   Serial.println(incByte,DEC);
 }
 if (millis()%5000==0) { // write every 5 seconds
   delay(1);
   Serial1.write(sent);
   Serial.print("Output=");
   Serial.println(sent,DEC);
   sent++;
 }
}

One of the arduinos keeps receiving informations from the other one AND from itself by the same Serial port.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

@OP

1. Use short jumper (not your 10m long wires) and connect two MEGA (MEGA-A and MEGA-B) by Serial1 Ports (RX1A ----> TX1B; TX1A <------RX1B; GND <----->GND).

2. Upload the following sketches:
(1) MEGA-A Codes:

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

void loop() 
{
   byte n = Serial.available();
   if (n !=0)
   {
      char x = Serial.read();
      Serial1.print(x);
   }

   byte n1 = Serial1.available();
   if (n1 !=0)
   {
      char y = Serial1.read();
      Serial.print(y);
   }
}

[b](2)[/b]  MEGA-B Codes:
[code]void setup() 
{
   Serial.begin(9600);
   Serial1.begin(9600);
}

void loop() 
{
   byte n = Serial.available();
   if (n !=0)
   {
      char x = Serial.read();
      Serial1.print(x);
   }

   byte n1 = Serial1.available();
   if (n1 !=0)
   {
      char y = Serial1.read();
      Serial.print(y);
   }
}

3. Bring in Serial Monitors for both MEGA at Bd 9600.

4. From the InputBox of MEGA-A, enter P and click on the Send Button. Check that P has appeared on the OutputBox of MEGA-B.

5. From the InputBox of MEGA-B, enter Q and click on the Send Button. Check that Q has appeared on the OutputBox of MEGA-A.

The above test proves that you have a working UART network and now add other hardware piece and and associated software codes; test it and proceed this way until the whole project is tested.

Thank you both for you comments. However i have used this kind of serial communication in previous projects and had no problem. I finaly moved one of the arduinos closer to make a direct connection with the other, and now they communicate as expected. This proves i think the code is not the issue.

So there must be a harware issue but i am very confused by the fact it works in one direction at a time, and this direction changes depending the port i use. If nothing worked, or if there was no "return to the sender" behaviour i would know what to look for, but not there.

Even more confusing : i tried to change an Arduino Mega in case it was damaged, and now it changes the working direction. The original Arduino Mega was able to write but not read, the spare Arduino Mega is able to read but not write...

Post a carefully drawn diagram showing exactly how everything is connected. (By careful, I mean technically accurate rather than high-quality art :slight_smile: )

Also post one of two photographs so we can see the physical setup.

My wild guess is that there is interference between the connecting wires - cross-talk.

Another possibility is that you are just at the edge of the limiting distance so that very minor differences in performance by the chips can cause success or failure.

...R
Simple Image Guide