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);
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
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.
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:
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...
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.