hi guys i am having a problem with AT-MEGA 2560 serial receiver buffer.
actually my esp32 is sending nested json data to AT-MEGA 2560 via serial2.
now the problem is i am receiving only 80% of my json data , but not 100% of json data.
this is due to serial receiver buffer size of At-MEGA2560.
Yes I agree. I just provided the information he asked in case rest of the project included long blockages which would hinder his ability to continually check availability
Because in the past I had a similar issue that I solved by increasing buffer size. I also modified the isr routine to inform every 256 byte packet arrival, which was a valid solution in my case with long blockages.
i did this also but it did not work. i mean i only received half json message on.
moreover i need to store the incoming message in a string for further processing.
it is just a simple project. where i need 50 gpios. i already have a mega with me. so instead of buying a multiplexer i just used what i have. and used esp for wifi purpose.
I agree with @J-M-L , your problem is not in the buffer size, it is in the incorrect code handling the incoming messages. Increasing the buffer you are only masking the problem, it will still come out sooner or later
i have studied about serial inputs as you specified.
what i was thinking is
for example it the incoming data is : "apple banana"
now if i use use
void loop()
{
if (Serial3.available()>0) Serial.write(Serial3.read());
}
now if my loop has many functions, then it will print only one byte at a time.
now my other function need the entire string from the serial3, so i used while
while (Serial3.available()>0) Serial.write(Serial3.read());
so this will print all the data that is coming from serial3 and after that it will go to execute next functions.
this is what my understanding in serial input.
if i am doing anything wrong or if there is any better way than please update me.
You never can be sure that your message is received completely. So instead of waiting for receive a 200-300 chars, you must read them by 2-3 chars as quickly as possible and store the text in local variable. The end of the data must be properly terminated to determine when the dta will be ready to process. While you received a terminator you start your text processing.
If you handle serial input this way, you won't need a buffer more than 64 bytes
ESP32 has only one job. the job is to send a json message to AT-MEGA every 1 second VIA serial communication.
now coming to AT-MEGA part there are 3 functions in void loop() function.
the first function job is to read from serial3 receiver buffer (where there is esp32 json data).
the second function needs the json data from serial 3 as input.
the third function is unrelated to above both functions. it takes an execution time of 1second.
now what J-M-L Jackson
suggest me is to use
if (Serial3.available()>0) Serial.write(Serial3.read());
but what i am thinking is if i use if() condition then it will read only one 1 character at a time.
where my 2nd function needs the entire json string so i used
while (Serial3.available()>0) Serial.write(Serial3.read());
by using this while loop() it will read entire json data from serial3 so i can use that json data as input to my second function.
here i am doing any thing wrong or any other problem will come i want to know?
next b707
says that my problem is not with the buffer size. i did not understand this one.
so please explain this with my above problem .
It is not related to the way how you received the json message - at once with a huge buffer or in 2-3 bytes pieces. In any case you should not start processing until you have received the complete message.
In your current program you don't even check whether you received the entire message or not.