Best way to Change SERIAL_BUFFER_SIZE?

I am trying to increase the SERIAL_BUFFER_SIZE to accommodate a device that outputs large data packets at 921.6 Kbaud.

This is on a Seeed Wio Terminal (SAMD51 w/ 192KB RAM) so I can afford the memory space.

Serial ports depend on Uart.h, which in turn depends on Ringbuffer.h for rxBuffer and txBuffer. Ringbuffer.h has the following lines in it, which suggest that the creator intended for users to be able to override the default 256 byte buffer size.

#ifndef SERIAL_BUFFER_SIZE
#define SERIAL_BUFFER_SIZE 256
#endif

However inserting #define SERIAL_BUFFER_SIZE 2048, even at the very beginning of the .ino file has no effect, other than generating a warning that it has previously been defined.

Presumably the RingBuffer.h file is being processed before the .ino file.

I could change the define in the Ringbuffer.h file manually but then I have to keep track of a customized file deep in the Arduino system and make sure it survives updates to the IDE and board libraries.

Is there any way to ensure that my define of the SERIAL_BUFFER_SIZE is actually applied?

Ideally it would be possible to apply different buffer sizes to different serial ports and different sizes to rxBuffer and txBuffer separately but I (so far) have enough memory that it isn't a big issue.

Thanks

Can you #undef the macro and #define again with a new value ?

I tried #undef before the #define. It still picks up the default value from RingBuffer.h. I think the system includes Arduino.h, which includes RingBuffer.h before it processes any of the text in the .ino file.

Some compilers allow you to add defines in the command line, which might fix this problem but I don't know if there is a way to do that with Arduino.

Are you trying to read the entire packet into the serial buffer all at once? Why not read it byte-by-byte and store it into a sufficiently large array? That way the serial buffer won't overflow.

1 Like

I am essentially reading the packet that is arriving at 921.6K and retransmitting it as fast as I can on another port at 115.2K. There is enough time between packets to retransmit it all but I will need to buffer a substantial fraction of the largest possible packet while the slow output catches up with the fast input. As you say, i could create a separate large array for this purpose, but if I can get the serial ports to do the work using interrupt handlers it is more efficient and less code I have to write and debug.

SERIAL_BUFFER_SIZE is used by source code other than the .ino file, so "of course" changes to the value in the .ino file have no effect :frowning:

You could arrange to have a -DSERIAL_BUFFER_SIZE=2048 in the compile lines, perhaps by defining a new board type, but I think that's a deeper and more mysterious change than just editing RingBuffer.h

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