serial.readStringUntil('\n') only reading 64 char

string a = Serial.readStringUntil(’\n’) only reading 64 char

unable read more than 24 char

input : This message is for the named addressees’ use only. It may contain NSDL confidential, proprietary or legally privileged information. If you receive this message in error, please immediately delete it.

output:This message is for the named addressees’ use only. It may cont

SERIAL ONLY READING 64 CHAR.txt (9.46 KB)

do you have 4800 baud on sender side too?

There is a 64 byte buffer for the serial input. That's probably the reason.

Without seeing the full code, it's difficult to say exactly what goes wrong.

I suggest that you read the updated serial input basics for ideas how to handle serial input.

Juraj:
do you have 4800 baud on sender side too?

i have used serial monitor but still it is reading 64 char

sterretje:
There is a 64 byte buffer for the serial input. That's probably the reason.

Without seeing the full code, it's difficult to say exactly what goes wrong.

I suggest that you read the updated serial input basics for ideas how to handle serial input.

what is the solution?
i have also uploaded the code.

Sorry, did not realise that the text file was the code. Maybe next time give it a double extension .ino.txt . Would have made it clearer to me :wink:

On a cell phone at the moment so can't really check it.

The approach to the solution might be in the link I provided.

Arduino Serial Port Buffer Size Mod
Whilst developing the software for our Arduino based Serial Graphic TFT Display a problem with transmitting too much data at once occured. When using our TFT display we expected the controlling program to send a big burst of serial data initially to set various configuration settings on the screen and to display a screenfull of data.

The Arduino core code contains a nice little round robin data buffer where you can keep throwing data at it and the arduino code will read the data and process it in order. However, this data buffer is by default only 64 bytes in size. This value is hard coded in the Arduino core source code and applies to all Arduino boards, even those with a vast amount of RAM available.

The 64 byte limit meant that sending a burst of data longer than 64 bytes would cause data to be truncated as the ATmega328 could not process the data sent fast enough.

The solution is easy, increase the buffer size to 256 bytes.

This is easy enough to implement but is a real pain because the Arduino core code which includes this setting is compiled before your actual program code is, so you cannot simply setup a #define or similar function to be able to select buffer size at compile time.

We found a number of convoluted methods on the internet which claim to work, but we thought an easier solution might be of use to others who need to increase the buffer size. The method described below is the option we chose to implement, and while not exactly elegant is easy to use and understand.

Solution

The solution is to create a complete copy of the arduino core code (it’s really not that big and disk space is cheap), modify the buffer size in the new core code and then to create a new board which is listed in the Arduino IDE which uses this new core directory. The steps to follow are as follows…

The whole of the Arduino core code is located in a directory similar to

C:\Program Files\arduino-1.0.1\hardware\arduino\cores\arduino
Make a complete copy of this directory and save it to

C:\Program Files\arduino-1.0.1\hardware\arduino\cores\arduino_256_serialbuf
The hard coded buffer size is stored in a file called HardwareSerial.cpp (or USBAPI.h in more recent versions)

Here is the standard definition located near the top of the file

#define SERIAL_BUFFER_SIZE 64
Edit the HardwareSerial.cpp file in the new directory and modify the buffer size

#define SERIAL_BUFFER_SIZE 256
Now we need to add an option to the boards.txt file to use this new directory. The boards.txt file should be in a directory similar to

C:\Program Files\arduino-1.0.1\hardware\arduino
Below is part of the boards.txt file. The first section is for the standard Arduino Uno. We have added a section below it which will display in the Arduino IDE as Arduino Uno (256 Serial Buffer). You can see the core directory is referenced to our new directory with the modified file (uno256.build.core=arduino_256_serialbuf)

##############################################################

uno.name=Arduino Uno
uno.upload.protocol=arduino
uno.upload.maximum_size=32256
uno.upload.speed=115200
uno.bootloader.low_fuses=0xff
uno.bootloader.high_fuses=0xde
uno.bootloader.extended_fuses=0x05
uno.bootloader.path=optiboot
uno.bootloader.file=optiboot_atmega328.hex
uno.bootloader.unlock_bits=0x3F
uno.bootloader.lock_bits=0x0F
uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L
uno.build.core=arduino
uno.build.variant=standard

##############################################################

uno256.name=Arduino Uno (256 Serial Buffer)
uno256.upload.protocol=arduino
uno256.upload.maximum_size=32256
uno256.upload.speed=115200
uno256.bootloader.low_fuses=0xff
uno256.bootloader.high_fuses=0xde
uno256.bootloader.extended_fuses=0x05
uno256.bootloader.path=optiboot
uno256.bootloader.file=optiboot_atmega328.hex
uno256.bootloader.unlock_bits=0x3F
uno256.bootloader.lock_bits=0x0F
uno256.build.mcu=atmega328p
uno256.build.f_cpu=16000000L
uno256.build.core=arduino_256_serialbuf
uno256.build.variant=standard

##############################################################

i will try this

arduino-ide-serial-buffer.jpg

I have a project that needs a larger serial buffer so I just created a separate copy of the Arduino IDE in which I changed the buffer size. Now if I want to use the standard buffer size I can use the regular version of the IDE to compile the program and if I want the larger buffer I compile with the other version of the IDE.

However I strongly suspect that there is no need to increase the buffer for your project if you try the code in Serial Input Basics as suggested in Reply #2. Just change the value of numChars from 32 to whatever you need.

...R

the right way to set a new buffer size is to define a custom board in hardware folder and set build.extra_flag
for example:
unoX.build.extra_flags=-DSERIAL_RX_BUFFER_SIZE=256

no need to copy the core or IDE. it is easy to setup this.. create the subfolders in hardware folder and boards.tx file. ready.
here is an example how to define custom board definitions: GitHub - jandrassy/my_boards: My Arduino boards custom variants. You can use it as starting point for your boards specialities.

I really see no need whatsoever to increase the serial buffer size. You are reading serial into a String, then immediately converting it to a null-terminated char array, so just use the methods detailed in Updated Serial Input Basics to read the text directly into a char array. It is also a good idea to avoid the String type anyway, since it often leads to memory corruption in an arduino.

Juraj:
no need to copy the core or IDE. i

I agree, but copying the IDE is a lot faster :slight_smile:

...R

Robin2:
I agree, but copying the IDE is a lot faster :slight_smile:

...R

I am not sure about that.

It is:

  • create folder structure /hardware/my_boards/avr
  • in avr folder create empty boards.txt.
  • copy the lines for Uno from official boards.txt into new /hardware/my_boards/avr/boards.txt
  • add "arduino:" prefix to build.core, build.variant and upload.tool (arduino:arduino, arduino:standard, arduino:avrdude)
  • add uno.build.extra_flags=-DSERIAL_RX_BUFFER_SIZE=256
  • restart IDE

readStringUntil() is blocking - if no more characters or the \n come in it's stuck until timeout.

Use something like this instead for strings of up to 500 characters, no blocking, no String class used:

char buf[500];
uint16_t counter = 0;

void loop() {
  if (Serial.available()) {
    buf[counter] = Serial.read();
    if (buf[counter] == '\n') {
      handleComplete();
      counter = 0;
    }
    else {
      counter++;
      if (counter == 500) {
        handleOverflow();
      }
    }
  }
  // rest of loop().
}

wvmarle:
Use something like this instead for strings of up to 500 characters, no blocking, no String class used:

That's essentially the same as the second example in Serial Input Basics which failed to catch the OP's attention when mentioned in Reply #2

...R

I have tried all the possible I could but still my code is reading only 64 char

wvmarle:
readStringUntil() is blocking - if no more characters or the \n come in it's stuck until timeout.

Use something like this instead for strings of up to 500 characters, no blocking, no String class used:

char buf[500];

uint16_t counter = 0;

void loop() {
  if (Serial.available()) {
    buf[counter] = Serial.read();
    if (buf[counter] == '\n') {
      handleComplete();
      counter = 0;
    }
    else {
      counter++;
      if (counter == 500) {
        handleOverflow();
      }
    }
  }
  // rest of loop().
}

not working...thanks for replying

Robin2:
That's essentially the same as the second example in Serial Input Basics which failed to catch the OP's attention when mentioned in Reply #2

...R

I have tested the 2nd example it is taking and printing 63 char only

rahulbaruri:
I have tried all the possible I could but still my code is reading only 64 char

The snippet I gave you is how it’s done.

Of course it won’t compile like that, you have to fill in the blanks and make it a complete sketch.

If you’re unable to do that, it’s time to step back and start learning the Arduino basics. You have to learn how to swim before you dive.

the other function takes time and the data from serial buffer are not read so the buffer overflows. comment out XProcess() from loop() for a test

rahulbaruri:
I have tested the 2nd example it is taking and printing 63 char only

You need to post the exact program that you tried.

...R