Serial Port receiving buffer size

I have a Spark core and I’m developing a communication program via Serial1, but i have a problem. The length of the Rx buffer (64 bytes) is not enought for my needs.
I tried to recompile the firmware after changing the
#define SERIAL_BUFFER_SIZE 64
to
#define SERIAL_BUFFER_SIZE 128

in spark_wiring_usartserial.h.

But, after flashing the new firmware, the limitation is still there :frowning:

Is any solution for my problem?
Thank you!

can’t you just keep parsing into a buffer… and look for the endpoint of the chunk of data?

I tried.
The process is like this.
I write on the serial1 a command,

        Serial1.write(commando, 6);

The client reaction is to send a block of something like 84-99 bytes (based on the command I send).
Immediatelly after writting the command, I start to read.

       while(!Serial1.available()){} ;
       while(Serial1.available()){
            readByte=Serial1.read();
            risposta[leggi]=readByte;
            leggi++;
        }

I have no delay between the 2 pieces of code…

you may be reading it faster than it is being sent and exiting the while() loop.

you could try hanging around there for a little while more waiting for the end of message byte

char myEndTXmarker = '#'; //for example
while (Serial1.available() && readByte != myEndTXmarker) 
{
  readByte = Serial1.read();
  risposta[leggi] = readByte;
  leggi++;
  delay(2); //slow it down a bit, allowing your next byte time to arrive
}

I tried your solution but it does not work.
Anyway, there is any possibility to change the buffer size, at least to 96? Or there is a hardware limitation?

yeah, not knowing your capabilities, I kind of led you down the garden path there. More like this:

char myEndTXmarker = '#' //for example
if (Serial1.available())
{
  do {
    readByte = Serial1.read();
    risposta[leggi] = readByte;
    leggi++;
    delay(2); //slow it down a bit, allowing your next byte time to arrive
  } while (readByte != myEndTXmarker); /* && leggi < EXPECTED_BUFFER_LENGTH);*/  // if you need to prevent overflow you can add that condition
}

or something close to that may work for you.

1 Like

Man! You’re a genius!!! Thank you very much!

Hi marengo, I have read your issue and I have a question.

I am interested in change the buffer size, how could you access to the internal serial libraries? Where could you find the .h file? I am using both Paritcle Build and Particle Dev to compile.

I appreacite any advice you can give me.

Thank you!

@mmarcano, you can only change the buffer size if you compile locally (full local toolchain) with a copy of the latest repo. The file describing the buffer size is usart_hal.h and is set as #define SERIAL_BUFFER_SIZE 64.

Hi @peekay123, Thank you so much for your quick response. I will install the dependencies to compile locally. Thanks for the tip to find the buffer size variable.

1 Like

Hi @mmarcano Did you have any luck with increasing the buffer size. I am in desperate need to do the same.

@rocky, increasing the buffer is often not the only way out :wink:
What’s your actual trouble?

@ScruffR. Thanks for asking. We have an external UART device that is capable of sending data in 32, 64 , 128 and larger chunks. The overhead of going back and forth with smaller chunks of data is HUGE. For example the total time to transfer 20 KB (that is what I have tested) almost twice as much is a 32KB chunk as compared to a 64KB chunk.
The Electron is transmitting this data over HTTP as it is received hence it is online.
This is a battery powered application and I want to minimize these times as much as I can.

I tried 265KB chunks using another micro-controller was hoping to achieve something close to that on the Electron.

Thanks

Could you use the particlesoftserial library to address this issue? You would have to fork the library and change the buffer size in the .h file. I wonder if this would work.

I’m having a similar issue trying to send 200 characters (easily) between two Particle devices and have trouble with the Serial1 buffer being limited to 64 bytes.

@ctmorrison, the trick to get around serial buffering issues is to sample the data availability more often. This means instead of using if(Serial1.available()) in loop() where there could be significant (milliseconds) delay between these checks, use while(Serial1.available()) to read the entire receive buffer every time the check is done. The former code is typical to Arduino where loop() runs at “full” speed. This is not the case for Particle devices.