SerialUSB.println() Buffer Limitation

A call to SerialUSB.println() [or print()] seems to hang if you pass a buffer with a string length greater than 247. Additionally, the characters past the 240 index seem to get messed up during transmission.

Here is some example code which demonstrates this:

void setup() 
{
  while(!SerialUSB);
  char buffer1[512];

  //Strangely the limit seems to be 247 characters?
  //But it seems to start to fail after 240?

  //This works
  memset(buffer1, 'a', sizeof(buffer1));
  buffer1[247] = 0;
  SerialUSB.println("Attempting to print a 247 character buffer: ");
  SerialUSB.println(buffer1);

  //But this hangs
  memset(buffer1, 'a', sizeof(buffer1));
  buffer1[248] = 0;
  SerialUSB.println("Attempting to print a 248 character buffer: ");
  SerialUSB.println("Individual characters:");
  for (int i=0; i<248; i++)
    SerialUSB.print(buffer1[i]);
  SerialUSB.println();
  SerialUSB.println("In a single call to SerialUSB.println():");
  SerialUSB.println(buffer1);
}

void loop() 
{
}

very interesting, thank's for pointing that out.

maybe we get some "official" answer (regarding that issue) from the arduino-team sometime ...

I feel a slight decline in community support from the arduino side (regarding the ZERO) within the past weeks :confused:
and the documentation and libraries are obviously not up to date for the ZERO yet (?)
(even though the product has been announced for more than one year now...)

Maybe you should open an issue in GitHub

I believe this is the issue:

In samd21_device.c on line 38 the send buffer is declared with a size of 128:

__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_in_cache_buffer[4][128];

Later in the send method it copies the data to be sent into that buffer without any size checks:

uint32_t UDD_Send(uint32_t ep, const void* data, uint32_t len)
{
	memcpy(&udd_ep_in_cache_buffer[ep], data, len);
...

This means that it will have trouble with anything over 128 bytes in length.