Serial com between arduino mega 2560 and atmega 328p(arduimu v3)

Hi , I plan to get gyroscope and accelerometer data from arduimu v3 to mega 2560.
So I do a small test.

I have connected the hardware as follow
arduimu v3 5v to 5v of mega 2560
arduimu v3 gnd to gnd of mega 2560
arduimu v3 tx to rx1 of mega 2560

my arduimu coded as follow:

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

void loop()
{
 Serial.print("abcd");
}

mega 2560 coded as follow

char tag[4];

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

void wait(byte num)
{
 while(!Serial1.available()< num);
}

void loop()
{
 if(Serial1.available())
 {
 tag[0] = Serial1.read();
 wait(3);
 
   if(tag[0] == 'a')
{
 tag[1] = Serial1.read();
 tag[2] = Serial1.read();
 tag[3] = Serial1.read();
 Serial.println(tag);// to track what is happening in serial 1
 }
 else
 {
 Serial.print("Not match -> ");
 Serial.println(tag);//to track what is happening in serial 1
 }
 }

}

but most of the result is not what I want, did i do anything wrong in the delay?
Please advice, thanks!

The usual problem with Serial.available.

AWOL:
The usual problem with Serial.available.

I see, do you have any solution or suggestion?
thanks!

Jack

but most of the result is not what I want

Well, tell us what you want, and how that differs from what you have.

You claim to have the TX and RX pins of the 328 and the Mega connected, and then you try to read from Serial1, which apparently has nothing connected to it. Why is that?

PaulS:
You claim to have the TX and RX pins of the 328 and the Mega connected, and then you try to read from Serial1, which apparently has nothing connected to it. Why is that?

oh ya, you remind me of not declaring port.
my bad, it is a typo, it is tx connected to rx1, I go change now.

#define rx 19

void setup(){
  Serial.begin(38400);
  Serial1.begin(38400);
  pinMode(rx, INPUT);
}

This is what I get...

#define rx 19

  Serial1.begin(38400);
  pinMode(rx, INPUT);

You told the Serial1 instance that it could use the pin. Then you diddle with the pin. Why?

You are sending data to the 328's serial port faster than the mega can read it and print it. After a while the buffer fills up, and data gets lost.

PaulS:

#define rx 19

Serial1.begin(38400);
  pinMode(rx, INPUT);



You told the Serial1 instance that it could use the pin. Then you diddle with the pin. Why?

You are sending data to the 328's serial port faster than the mega can read it and print it. After a while the buffer fills up, and data gets lost.

I am really blur @@
Is there any way I can deal with the data handling?
Please help, thanks!

Is there any way I can deal with the data handling?

yesthereisyoucan'treadthiseasilybecauseIhavenotputinthecuesthattellyouwherewordsbeginandend.
But if you slip in some simple whitespace and punctuation, things are a lot easier.
Computers don't read junk either.

You need to be sure not to send data faster than the receiver can handle that. The point of sending "abcd" over and over as fast as possible escapes me.

You seem to have been trying to detect whether there was a problem with doing that, and you seem to have proven that there is.

Now, what do you intend to do to fix the problem? Or, is the problem that you don't know how to fix the problem?

AWOL:
yesthereisyoucan'treadthiseasilybecauseIhavenotputinthecuesthattellyouwherewordsbeginandend.
But if you slip in some simple whitespace and punctuation, things are a lot easier.
Computers don't read junk either.

You need to be sure not to send data faster than the receiver can handle that. The point of sending "abcd" over and over as fast as possible escapes me.

You seem to have been trying to detect whether there was a problem with doing that, and you seem to have proven that there is.

Now, what do you intend to do to fix the problem? Or, is the problem that you don't know how to fix the problem?

guys, really thanks a lot!
I will slip in the "junk" & try lower down the baud rate.

try lower down the baud rate.

There's no real need that I can see to do this.
Junkisjunkatanyspeed.