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
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.
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
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.
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
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
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.
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