I use T4.0 for data acquisition and having a problem to steam >4000 floats to PC via USB. First I used a simple code:
When nstep>~4000 the program literally hangs (my understanding is that Serial.availableForWrite() is returning 0 and program waits for flush timeout nstep-4000 times). I can avoid hangout of T4.0 by breaking the loop after 1 millisecond:
but it does not solve the problem of a proper data transfer (I need to transfer ~20000 points). On the PC end I use a Labview code which constantly reads bytes from the Serial port. When T4.0 hangs PC is waiting for data to appear in the serial stream (meaning no unread bytes in the FIFO). I can perform multiple transfers of <4000 data points if I wait a few seconds between the streams. Any suggestions what could be the source of the problem?
Code:
float fdac;
unsigned char const * bfdac = reinterpret_cast<unsigned char const *>(&fdac);
for ( int i=0; i<nstep; i++ ) {
fdac = mbuff[i];
if ( Serial.availableForWrite()>3 ) Serial.write(bfdac,4);
else {
Serial.flush();
Serial.write(bfdac,4);
};
}
Code:
elapsedMicros usec;
for ( int i=0; i<nstep; i++ ) {
fdac = mbuff[i];
if ( Serial.availableForWrite()>3 ) Serial.write(bfdac,4);
else {
usec = 0;
do
delayMicroseconds(10);
while ( (Serial.availableForWrite()>3) || (usec > 1000) );
if ( Serial.availableForWrite()>3 ) Serial.write(bfdac,4);
else break;
};
}