How do I increase the serial receive buffer

@ptillisch this is slightly different from the original question, but if I can ask how do I increase the Serial Receive Buffer to make it 128 or 256 bytes. I'm using IDE 2.x version. Thank you :blush:

Hi @elmero_01. Please tell us which boards you want to increase the buffer for.

The procedure typically involves modifying the code of the Arduino core and different cores are used when compiling for boards of different architectures (e.g., AVR vs. ESP32). So the answer to your question might depend on the boards you are using.

1 Like

If you use the techniques in Robin2's serial input basics tutorial you can set the input buffer as large as you wish without messing with any library. The methods receive a character at a time and puts that character into a user defined character array. The array becomes available once the entire message has arrived. The array can be as large as the processor memory allows.

With one drawback - the original serial buffer in the Arduino sits almost totally unused, because it's deliberately kept empty. So you 'throw away' those bytes. A small issue for some...

1 Like

@ptillisch many thanks, at the moment I'm using Mega and it's working ok, also I'm using HC-06 bluetooth module to send message to arduino. The only thing I wanted is to extend the number of characters to maybe up to 100 characters. Thank you for your prompt reply to my question.

@groundFungus awesome thank you for sharing this, will give it go, will let you know.

@camsysca thank you for your comment.

The approach suggested by groundFungus is better than the approach of modifying the Arduino core code as I suspected was your original goal.

In addition to the procedure for modifying the core being somewhat complex (due to the need to find the file that needs to be modified), you also need to redo that work after every time you update to a new version of the "Arduino AVR Boards" platform. So if you can find a suitable solution within your sketch code, that will be much better.

If you do end up decide that you need to go with the crude approach of modifying the core for some reason, just let us know and we'll provide instructions you can follow to do that.

@ptillisch the code that @groundFungus has suggested doesn't work as I have tried it. I think the problem is in the function:

void recWithStartEndMarkers()
static boolean recvInProgress = false; was initialized and within the function there's a condition
if (recvInProgress == true)
{
statements.....
}
This condition will never be true because it's been intialized as false within the function. Please correct me if I'm wrong?

@groundFungus have you tried this code? It doesn't work and I think the problem is this:

void recWithStartEndMarkers()
static boolean recvInProgress = false; was initialized and within the function there's a condition
if (recvInProgress == true)
{
statements.....
}
This condition will never be true because it's been intialized as false within the function. Please correct me if I'm wrong?

You're wrong :wink: The variable is static which indicates that it's only initialised once (when declared) and keeps the last value. So once a receive is in progress, it will be remembered that it is in progress.

The below part will set it to true

        else if (rc == startMarker) {
            recvInProgress = true;
        }

I have used that code in each of those examples dozens of times with no issues. Did you run the code unmodified? If not, please post the code that is giving you trouble and tell us what you changed.

@sterretje yup I see thank you :blush:

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