You just can't write any buffer length in SerialUSB.write without issues.
You should be able to. But indeed, I am having problems getting anything more than about 250 bytes to work.
The THEORY is that a usb data exchange takes some fixed time Tt, plus some time based on the amount of data in the exchange Td - usually the Tt is much larger than Td, so it's to your advantage to use a large chunk of data (ie a large buffer) in each exchange. At least, up to the point where the buffer becomes larger than the amount you're allowed to send in each exchange.
In PRACTICE, there are a lot of factors involved in the overall throughput. There is the USB driver in the Due, the way that the Arduino serial code interacts with it, the USB driver in the host, the Serial port driver in the host, and the way that the host application interacts with all that. I'm getting about 800ms to upload 10000*6 values (code attached), regardless of whether I use "println" for every line, or a more complicated buffering scheme
Your code has other problems...
int drm[20000][5];
An Arduino Due does not have that much RAM memory. (400kBytes!)
int drm[20000];
A due doesn't even have THAT much contiguous RAM memory (80k bytes. Due has a 64k chunk and a 32k chunk.)
i don't see how you get your code to even compile...
for(int i=0;i<=20000;i++)
the "<=" should just be a "<"; otherwise you access the 20001st element of your array. (this will compile, but writing beyond the end of the array can cause .... indeterminate behavior.)
My test code:
#include <stdio.h>
#define DRMLEN 10000
#define BUFSIZE 300
#define USE_WRITE 1
#define USE_SMALL_PRINT 0
uint8_t drm[DRMLEN][5];
void setup() {
// put your setup code here, to run once:
SerialUSB.begin(9600);
while (!SerialUSB)
;
SerialUSB.print("Program starting");
SerialUSB.println();
delay(5000);
SerialUSB.println("Here we go!");
}
uint32_t totdata;
void loop() {
// put your main code here, to run repeatedly:
uint32_t starttime, elapsedtime;
for (int i = 0; i < DRMLEN; i++)
{
drm[i][0] = digitalRead(3);
drm[i][1] = digitalRead(4);
drm[i][2] = digitalRead(5);
drm[i][3] = digitalRead(6);
drm[i][4] = digitalRead(7);
}
#if USE_WRITE
SerialUSB.println("Dumping output using write");
#else
SerialUSB.println("Dumping output using print");
#endif
starttime = millis();
totdata = 0;
dumpData();
elapsedtime = millis() - starttime;
SerialUSB.print("Elapsed Time is ");
SerialUSB.print(elapsedtime);
SerialUSB.print(" ms");
SerialUSB.print(" Total data bytes: ");
SerialUSB.print(totdata);
SerialUSB.print(" (");
SerialUSB.print(((float)totdata) / (elapsedtime) * 1000.0);
SerialUSB.print(" bytes/s)");
SerialUSB.println();
while (SerialUSB.read() == -1) {
delay(1000);
}
}
void dumpData() {
int totLen = 0;
int len;
char buffer[BUFSIZE]; // long-ish buffer
for (int i = 0; i < DRMLEN; i++) {
#if USE_SMALL_PRINT
SerialUSB.print(i); SerialUSB.print(": ");
SerialUSB.print(drm[i][0]); SerialUSB.print(" ");
SerialUSB.print(drm[i][1]); SerialUSB.print(" ");
SerialUSB.print(drm[i][2]); SerialUSB.print(" ");
SerialUSB.print(drm[i][3]); SerialUSB.print(" ");
SerialUSB.print(drm[i][4]); SerialUSB.print("\r\n");
#else
len = sprintf(&buffer[totLen], "%d: %d %d %d %d %d\r\n", i, drm[i][0], drm[i][1], drm[i][2], drm[i][3], drm[i][4]);
totLen += len;
totdata += len;
#if USE_WRITE
if (totLen > (sizeof(buffer) - 50)) { // if buffer is close to full
SerialUSB.write(buffer, totLen); // write the buffer to USB
totLen = 0; // reset to beginning of buffer
}
#else
SerialUSB.print(buffer); // use print
totLen = 0; // immediately reset buffer
#endif
#endif // USE_SMALL_PRINT
} // end for loop
SerialUSB.write(buffer, totLen); // write last buffer
}