[solved] Serial data becomes inconsistent above 9600 baud

I'm trying to send and receive data between two Arduino Mega at 115200 baudrate, that's the goal at least, but data is "missing" on any baudrate above 9600, code is simple, either I'm missing something very basic or it's hardware failure
sender board writes bytes 1-8 to Serial, which is connected to Serial of receiver board (alongiside with grounds of arduinos connected), receiver simply reads Serial and writes it to Serial1, which is hooked up to USB-TTL adapter and connected to PC.
sender:

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


void loop()
{
 unsigned char data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
 Serial.write(data, 8);
}

receiver:

void setup()
{
 Serial.begin(115200);
 Serial1.begin(115200); // connected to pc 
}


void loop()
{
 while(Serial.available()) Serial1.write(Serial.read());
}

Output (on PC via Serial1 from receiver code):

1
2
3
4
7
1
2
3
6
1
2
3
5
8
...

I couldn't find pattern, it's random from my perspective.
But if I connect TX/RX pins to USB-TTL instead of going to second Arduino, output looks just as expected. if hardware isn't capable of communicating at these speeds, why is it even an option?

edit: problem was serial port, it was damaged, using other hardware serial port worked fine

What is the length of the cable? What kind of cable is used?

while(Serial.available()) Serial1.write(ESP.read());

Pls clarify: You're checking if serial data is available on Serial but are reading from "ESP"?

Railroader:
What is the length of the cable? What kind of cable is used?

length is only 20cm, diameter 0.22mm. not soldered because it's Arduino Mega

That distance ought be okey provided that no sparking motor is close to the wires.

Blackfin:

while(Serial.available()) Serial1.write(ESP.read());

Pls clarify: You're checking if serial data is available on Serial but are reading from "ESP"?

my bad, i simplified code and missed some variable names, i had ESP defined as Serial, edited original post.

Communicating at these speeds is normal procedure. All you need is to do it in the normal manner.

Serial1.begin(115200); // connected to pc

If this is really your intention, rather than a typo, I'm surprised you get anything. I don't dare guess where the PC output comes from, but the random nature is unsurprising. As it stands, the receiver code is either fanciful or incomplete, possibly both, and doesn't compile, so you should get nothing on the output. You might try swapping the ports around. Also check here for some examples of serial comms.

Serial1 connects to the PC, it says. What boards are you using?

Nick_Pyner:
I don't dare guess where the PC output comes from,

Especially since the sketch only writes to Serial1 and we know those bytes come from the sender board.

I'm using Mega.

sender board writes bytes 1-8 to Serial, which is connected to Serial of receiver board, receiver simply reads Serial and writes it to Serial1, which is hooked up to USB-TTL adapter and connected to PC.

if i connect sender board Serial directly to USB-TTL, output looks just fine

sphalelashvili:
I'm trying to send and receive data between two Arduinos at 115200 baudrate,

Exactly what two Arduinos?

...R

Hi,
How many wires do you have between the Arduinos?
You should have three, TX to RX RX to TX and GND to GND.

What model Arduinos are you using?

Tom.. :slight_smile:

How are you reading the data on the PC ?

I'm using 2 Arduino Mega.

You should have three, TX to RX RX to TX and GND to GND.

I didn't know about gnd, but even with ground connected to other arduino's ground I'm still getting same result.

How are you reading the data on the PC ?

receiver reads Serial and writes it to Serial1, which is hooked up to USB-TTL adapter and connected to PC.

Railroader:
That distance ought be okey provided that no sparking motor is close to the wires.

There don't have to be sparks to induce voltage on the cable, just a strong/fast enough changing field.

GoForSmoke:
There don't have to be sparks to induce voltage on the cable, just a strong/fast enough changing field.

no other magnetic fields around cable

@GFS
Yes, that's correct.

sphalelashvili:
no other magnetic fields around cable

It's just something to consider, with long wires it's more like a real issue.

You should be able to get 57600 baud though perhaps corrosion on one or more contacts is causing this problem.

Railroader --- I'm down to my physics classes on that. I saw one project with a similar problem, it had the serial wires coiled to save space or look neater and every time the nearby relay switched that data garbaged.

sphalelashvili:
length is only 20cm, diameter 0.22mm. not soldered because it's Arduino Mega

Is this just a programming exercise, or is there a reason you have 2 Mega units so close yet need two?
Have you twisted the two signal wires together?
Can you post a picture of your project please so we can see your component layout?
Thanks.. Tom... :slight_smile:

I haven't tried this on my setup here but thinking out loud...

At 115200 a complete byte is transmitted by the sender in 86.8uS and that's all the sender is doing. There are no pauses of consequence, it just streams out a byte every ~86.8uS.

The receiver has to receive these bytes but also has to process and send out every byte. So effectively, for every one thing the sender does, the receiver does two.

It wouldn't surprise me if, all else being equal, the sender was able to send data to the receiver faster than it can process the characters, resulting in RX buffer overflow and missed characters.

Lower baud rates (9600 is one char in 1.04mS) gives the receiver more breathing room to deal with RX and TX at the same time.