Arduino ATMEGA 2560 serial communication with another ATMEGA2560

hi,
i am facing an issue while doing serial communication with two ATmegas.
the code of 1st AT MEGA IS

int MESP_ID_Time_AvgAT_v=0;

int MESP_ID_Time_AvgRH_v=34;
void senddata();
void setup() 
{
  Serial.begin(115200);
  Serial2.begin(9600);
  Serial.println("welcome...");
}

void loop() 
{
  String str="";
  while(Serial2.available())
  {
    
    str=Serial2.readString();
    Serial.print("DISPLAY DATA: ");Serial.println(str);
    if(str=="as\r\n")
    {
      MESP_ID_Time_AvgAT_v=1;
      Serial.println("SENDING DATA TO 2nd ATMEGA");
    }
    else if(str=="ap\r\n")
    {
      MESP_ID_Time_AvgAT_v=0;
      Serial.println("cut DATA TO 2nd ATMEGA");
    }
  }
  delay(100);
  senddata();
  delay(100);
}
void senddata()
{
  if (MESP_ID_Time_AvgAT_v > 0)
  {

    Serial.println("INSIDE SENDDATA FUN");
    Serial2.print("at.txt=\"");
    Serial2.print(MESP_ID_Time_AvgAT_v);
    Serial2.print("\"");
    Serial2.write(0xff);//Serial2.write(0x03ff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    delay(100);
    Serial2.print("rh.txt=\"");
    Serial2.print(MESP_ID_Time_AvgRH_v);
    Serial2.print("\"");
    Serial2.write(0xff);
    Serial2.write(0xff);
    Serial2.write(0xff);
    delay(100);
  }
}

the code of 2nd AT MEGA IS

String serial2read(void)
{
  
  String str="";
  while(Serial2.available())
  {
    Serial.println("READING DATA FROM SERIAL 2 ");
    str=Serial2.readStringUntil('\n');
    Serial.println("done READING DATA to SERIAL 2 ");
  }
  if(str!=NULL)Serial.println(str);
  
  return str;
}

void Serial2write(void)
{
  while(Serial.available())
  {
    Serial.println("WRITTING DATA FROM SERIAL 2 ");
    Serial2.write(Serial.read());
    Serial.println("done WRITTING DATA to SERIAL 2 ");
  }
}
void setup() 
{
  Serial.begin(115200);
  //Serial.setDebugOutput(true);
  Serial2.begin(9600);
  Serial.println("HELLO");
  Serial2.write("AT\r\n");
}

void loop() 
{
  serial2read();
  delay(100);
  Serial2write();
  delay(100);
}

the out put of 2nd AT MEGA IS

22:38:28.636 -> WRITTING DATA FROM SERIAL 2 
22:38:28.636 -> done WRITTING DATA to SERIAL 2 
22:38:29.951 -> READING DATA FROM SERIAL 2 
22:38:55.719 -> done READING DATA to SERIAL 2 

when ever my 1st AT MEGA is sending data to 2nd AT MEGA, my 2nd MEGA is always doing Serial2.readString() not printing until and unless i am disconnecting my 1st ATMEGA.
more over if i remove all

Serial2.write(0x0ff);
Serial2.write(0x0ff);
Serial2.write(0x0ff);

still same issue
i dont understand why?
can any one please help me.

I would suggest to study Serial Input Basics to handle this


you posted in an unsuitable category, I moved your post. Please make sure you post in a suitable category in the future.

means am i doing anything wrong?
please share me where to study Serial Input Basics.

click on the link, it will take you there

you use blocking code functions with a timeout for a process that is asynchronous. Second guessing the timing of an asynchronous task is a recipe for failure.

1 Like

From your second Mega:

You are reading until you get a newline, but nowhere in your First Mega code do you every send a newline. Typically, this is done with a Serial2.println() function which tacks on a trailing newline

1 Like

you use blocking code functions with a timeout for a process that is asynchronous. Second guessing the timing of an asynchronous task is a recipe for failure.

i did not understand can you please elaborate about the timeout part.

i change it to

str=Serial2.readString();

still same issue

you use readStringUntil() to expect an '\n' but you are not sending any, so the function will read whatever is coming and then terminate after a 1s timeout (by default as documented, see specs)

but it is not happening like that.
when i send "as" my ATMEGA starts sending some data and my other ATMEGA is stuck at line and doing nothing until i disconnect the ATMEGA then it is printing what ever it has read.
i changed it to Serial2.readString() still same result

try to rephrase this, it's very unclear

  • call your Arduino MEGA1 and MEGA2
  • document which code runs on which
  • clarify how you hooked them up (GND connected ?)
  • tell us what you type into the Serial monitor of which arduino and how the Serial monitor is configured
  • tell us exactly what you see on the other side

MEGA 1 TX PIN IS CONNECTED TO RX PIN OF MEGA2.
MEGA 1 RX PIN IS CONNECTED TO TX PIN OF MEGA2.
MEGA 1 GND CONNECTED TO MEGA2 GND.

MEGA 2 sends AT message to MEGA 1 via serial2. MEGA 1 is receiving the message displaying on serial monitor here there is no problem.

when MEGA 2 sends "as\r\n" then MEGA 1 received the message then stated sending all the data in senddata() function but my MEGA 2 is not receiving it and when remove MEGA 2 usb cable from pc then MEGA 2 is printing all data it has read from MEGA 1.

that mean MEGA 2 is always reading [Serial2.readString()] or we can say MEGA 2 is stuck at Serial2.readString() line.
how i know this this is

String str="";
  while(Serial2.available())
  {
    Serial.println("READING DATA FROM SERIAL 2 ");
    str=Serial2.readStringl();
    Serial.println("done READING DATA to SERIAL 2 ");
}

out put is

3:28:46.694 -> done WRITTING DATA to SERIAL 2 
23:28:46.694 -> WRITTING DATA FROM SERIAL 2 
23:28:46.742 -> done WRITTING DATA to SERIAL 2 
23:28:50.756 -> WRITTING DATA FROM SERIAL 2 
23:28:50.756 -> done WRITTING DATA to SERIAL 2 
23:28:50.756 -> WRITTING DATA FROM SERIAL 2 
23:28:50.756 -> done WRITTING DATA to SERIAL 2 
23:28:50.756 -> WRITTING DATA FROM SERIAL 2 
23:28:50.756 -> done WRITTING DATA to SERIAL 2 
23:28:50.756 -> WRITTING DATA FROM SERIAL 2 
23:28:50.756 -> done WRITTING DATA to SERIAL 2 
23:28:52.073 -> READING DATA FROM SERIAL 2

see the last message

yes you are right.
it is true that when you use Serial.readString() function there must be a gap of 1000 milliseconds otherwise it will continuously reads the data from serial input buffer.
thanks.

another example of why

1 Like

do you have any website link to learn about the timing, asynchronous and synchronous tasks.

You could look at

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

do you have any best way to send and receive json format via serial.
the challenge i am facing is, i dont know when mega 1 will send json to mega 2.
i dont know when mega 2 will send json to mega 1.
we must synchronous the timing?
we should not miss the data.
how to deal with this situation?

Json or whatever, as I said, I would suggest to study Serial Input Basics to handle this

the code listens asynchronously to what's coming on the Serial port (byte by byte, in a non blocking way) and upon detection of the end marker takes an action

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.