Problem transferring data between 2 arduinos using Serial

Hi there..
First of all, forgive me in advance for my lack of knowledge.. Im new to arduino though not new to C/CPP..
So, I'm trying to learn how to communicate between 2 arduinos as I need that in a project im working on.
The first arduino is Arduino mega 2560 and the second is Arduino due.
Im using Serial connection.
Here is the code I wrote (for both devices) in order to understand how this works:

int rx = 19;
int tx = 18;

char buffer[64];
char character;

void setup() {
	Serial.begin(9600);
	while (!Serial);
	pinMode(rx, INPUT);
	pinMode(tx, OUTPUT);
	Serial.println("Trying to connect..");
	Serial1.begin(9600);
	while (!Serial1);
	Serial.println("Connection established.");
	Serial1.flush();
	Serial.flush();
}
void loop() {
	if (Serial.available() > 0) {
		Serial1.write(Serial.read());
		Serial1.write("\0");
	}
	if (Serial1.available() > 0) {
		Serial1.readBytesUntil('\0', buffer, 64);
		Serial.println(buffer);
	}
}

Both devices seems to send data though only the due receives it.
The Mega does'nt seem to receive any data.

What am I doing wrong?

Regards.

while (!Serial1);

This has no meaning on any Arduino. Remove it. It's only relevant to the Arduinos which have "native" USB serial and only on the USB serial port.

pinMode(rx, INPUT);
pinMode(tx, OUTPUT);

After you call Serial.begin() you should never touch the status of the serial pins. You told the serial subsystem that it could start and then you said "hang on, I have a different idea for those pins." Serial.begin() already knows which pin to make an input or output. (Actually, I think you are doing this before calling Serial1.begin() so these lines are useless.)

Serial1.readBytesUntil('\0', buffer, 64);

This is a relic of the early days of Arduino. It would be better if this function did not exist. Try to write your program not to use any of the readUntil, parseInt or related functions. See Serial Input Basics.

I don't know how you can know that a device is sending if the other one isn't receiving. Please describe in more detail exactly how you wired it up and what the result was on each end.

Did you connect the grounds?

Hi and thank you for your notes. They are most appreciated.
I (believe I) know that both are transmitting as Serial.send() returns non-zero value on both devices. Maybe that means nothing?
Ive connected:
18pin->19pin
19pin->18pin
gnd->gnd
on both devices.
When i'm sending data from the mega to the due: tx on mega blink and rx on due blinks.
When i'm sending data from the due to the mega: only the tx light on the due blink.

I've tried connecting other ports (17,16) and other jumper cables but nothing seems to work.
Might there be some problem with on of them?

Thanks!

engeloded:
When i'm sending data from the mega to the due: tx on mega blink and rx on due blinks.
When i'm sending data from the due to the mega: only the tx light on the due blink.

Tx and Rx are only linked to the primary hardware serial. The programming port. Those lights are actually controlled by the helper chip which makes the programming port work. If you are seeing Tx flash then it means you sent something other than what you thought you were sending to Serial1.

I can't figure from all this if your problem is with your hardware or with software. if it is software Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Hello to all.
After trying to transmit from each device to its own (Serial1 to Serial2/Serial2 to Serial3) I believe that the mega isn't working probably. Some hardware problem probably.

Thank you. :slight_smile:

It is rare, but not impossible, to burn out a single pin and leave the rest of the device untouched. I have done stupid things like hooking up 12V backwards on more than one occasion.