Increase size of Serial buffer on mac?

Is there a way to increase the size of the Serial buffer, working on a mac computer? I have a mega board, with Serial1 connected to a Sparkfun DB-9 and then to a packet radio controller. The loop (posted below) reads serial data until encountering a newline, then parses, saves, and displays to a TFT. It works fine, but the number of functions called during the loop is increasing (I want to recall and display saved data), and the serial buffer may fill before the loop runs again. There's plenty of memory available still (sketch is about 80K with TFT and touch functions enabled). Can I increase the size of the serial buffer to say, 1024 or 2048 bytes? Thanks for any help.

void loop() {
while (Serial1.available()) {
char ch = Serial1.read();
if (ch == '\n') { //reached end of line so parse, display, and save to card
packet[buflen] = 0;
parse_packet();
buflen = 0;
} else if ((ch > 31 || ch == 0x1c || ch == 0x1d || ch == 0x27) && buflen < BUFLEN) {
// Mic-E uses some non-printing characters
packet[buflen++] = ch;
}
}
//check touchedWhat here for change to log or live
} //end loop

Sure, poke around in your Arduino core files, there's one named serial.cpp, maybe serial.h, that has the buffer sizes in it.

Arduinos do not have memory in the conventional sense. They have memory for the program("prom") and memory (Sram) for the data. The IDE gives you the amount of program memory in use. It tells you nothing at all about SRAM usage.

Mark

Keep in mind that the Mega has 4 hardware serial ports, so if you change the the size of the buffer, all 8 buffers get larger - th3 4 incoming and the 4 outgoing.

You have 8K of memory. Even a 1024 buffer will be too large. In general, writing smarter code (non-blocking) is better than making the buffers larger.

Your title seems to mean that you want to increase the serial buffer within the Mac, not within the Mega.

As @PaulS says, write smarter code.

I find it hard to imagine how you can't consume data faster than it arrives in the buffer - whether on the Arduino or on the Mac.

...R

The file that specifies the buffer size is HardwareSerial.cpp and is located off your Arduino directory at:

hardware\arduino\cores\arduino

For the Mega, the buffer size is 64. If you go messing around with the buffer size, keep in mind what PaulS says, since the change affects more than one buffer.

Remember too, to go back and restore the buffer size when you're done developing, unless you want all your future sketches to inherit this "advantage".

Thanks for all the replies so far. Simply increasing the buffer size may not be the best way, I understand. Would it work to attach a hardware interrupt to the pin that Serial1 is on and read that into a char array? Can one make an array into a 'ring' buffer such that the parse function could read single char's until encountering a newline, without disturbing Serial1 adding to the end of the array? Or is there some other way?

Would it work to attach a hardware interrupt to the pin that Serial1 is on and read that into a char array?

Serial data is already buffered, using interrupts.

Can one make an array into a 'ring' buffer

The serial buffer is already a ring buffer.

such that the parse function

Since we haven't seen your parse function, we have no idea what it currently does. Hint, hint.

until encountering a newline, without disturbing Serial1 adding to the end of the array?

That's already guaranteed, due to the use of interrupts.

Or is there some other way?

Yes. No. Maybe. See previous comments (and hints).

I understand that the serial data goes into a ring buffer. My question was whether it would be practical to feed that into a ring buffer within the sketch, so that I can make the buffer larger without quadrupling the memory requirements. The parse function takes a char array, packet[BUFLEN], and splits it into other char arrays, integers, and longs. It works fine.

My question is how can I ensure that the Serial1 buffer doesn't overflow while packet is being parsed, displayed and saved to SD card, in a way other than simply enlarging the ring buffer? If I already knew a smarted code method, I wouldn't pose the question. Thanks for all help.

I mention the mac simply because the programming environment is sometimes different than on windows, files in different places, for example. It is the Serial1 buffer on the mega board that is my concern.

You still haven't said clearly if this is a Mac problem or an Arduino problem. Your more recent posts suggest it is an Arduino problem (contrary to what your title implies).

So let's go back to basics. Why do you think the buffer isn't big enough?

This problem can be solved by writing your code more effectively.

Post your code (preferably a short piece that just illustrates the problem).

...R