I posted in another thread: http://forum.pjrc.com/threads/18852-Teensy-3-0-to-PC-communication-via-USB-serial-(-gt-256000-baud)
, but now that I have some test code this might be better to start a thread on this. I cannot get more than 128 kilobytes/second, when transferring. It's transferring a buffer in RAM as fast as possible for a 3MB transfer. It always takes about 20 - 30 seconds, regardless of buffer size, etc. Has anyone managed to get faster transfer rates?
teensy 3.0 sketch
Processing Sketch
Here's a shell script to make it easier to launch the java applet on linux. You'll need to put the processing sketch in a directory called "serialReceiver". I probably should have done this in Processing IDE, but I did it in eclipse. I'll try in Processing if someone says that will be helpful. You'll need to download Processing for this to work, and change the location of the various entries in the shell script to match your libraries, and classpath if need be.
Ok, I just tested it and removed the 1st line that listed a package. You can now copy the Processing sketch into Processing and save it as "UsbReceiver", and it will run.
, but now that I have some test code this might be better to start a thread on this. I cannot get more than 128 kilobytes/second, when transferring. It's transferring a buffer in RAM as fast as possible for a 3MB transfer. It always takes about 20 - 30 seconds, regardless of buffer size, etc. Has anyone managed to get faster transfer rates?
teensy 3.0 sketch
Code:
uint8_t *data;
const int buf_length=512;
//a 3MB file
const int chunks=3000320/buf_length;
long startTime=-1, elapsedTime=-1;
void setup() {
Serial.begin(2000000);
for (int i=0;i<8;i++) {
delay(1000);
Serial.print(i);
Serial.println(" seconds");
Serial.flush();
}
data = (byte*)malloc(buf_length);
memset(data,1,buf_length);
startTime=micros();
for (int i=0;i<chunks;i++) {
Serial.write(data,buf_length);
}
// Serial.println();
//Serial.print("elapsedTime: ");
//Serial.println(micros()-startTime);
//Serial.flush();
// Serial.end();
}
void loop() {
}
Processing Sketch
Code:
import java.nio.ByteBuffer;
import processing.core.*;
import processing.serial.Serial;
public class UsbReceiver extends PApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
UsbReceiver usbReceiver = new UsbReceiver();
Serial myPort = usbReceiver.setupSerial();
usbReceiver.receiveFile(myPort, "usbTest.out");
}
public Serial setupSerial() {
Serial serialPort;
// List all the available serial ports:
println(Serial.list());
// Open the port you are using at the rate you want:
System.out.println("serial length: " + Serial.list().length);
serialPort = new Serial(this, Serial.list()[0], 2000000);
System.out.println("myPort: " + serialPort.available());
return serialPort;
}
public void receiveFile(Serial serial, String sOutputFilename) {
int nBytes = -1, totalBytes = 0;
int chunkSize = 512, fileSize=3000320;
byte [] buffBytes = new byte[chunkSize];
ByteBuffer tmpBuff = ByteBuffer.wrap(buffBytes);
//try & wait until we have chunkSize available
delay(50);
System.out.println("avail: " + serial.available());
long startTime=System.currentTimeMillis();
for (int i=0;totalBytes<(fileSize-chunkSize);i++) {
if (serial.available() >= chunkSize) {
nBytes = serial.readBytes(buffBytes);
totalBytes+=nBytes;
//System.out.println("read" + i + " nBytes: " + nBytes + " totalBytes: " + totalBytes);
tmpBuff.rewind();
} else {
//System.out.println("sleep: " + i);
delay(1);
}
}
delay(50);
//read last chunk
if (serial.available() > 0) {
nBytes = serial.readBytes(buffBytes);
totalBytes+=nBytes;
//System.out.println(" last read, nBytes: " + nBytes + " totalBytes: " + totalBytes);
tmpBuff.rewind();
} else {
System.out.println("nothing avail on last read");
}
System.out.println("Time elapsed: " + (System.currentTimeMillis() - startTime));
System.out.println("done sending: " + totalBytes);
}
}
Here's a shell script to make it easier to launch the java applet on linux. You'll need to put the processing sketch in a directory called "serialReceiver". I probably should have done this in Processing IDE, but I did it in eclipse. I'll try in Processing if someone says that will be helpful. You'll need to download Processing for this to work, and change the location of the various entries in the shell script to match your libraries, and classpath if need be.
Code:
#!/bin/sh
LIBRARY="/usr/local/processing/modes/java/libraries/serial/library/linux64"
CLASSPATH="/usr/local/processing/core/library/core.jar:/usr/local/processing/modes/java/libraries/serial/library/RXTXcomm.jar:/usr/local/processing/modes/java/libraries/serial/library/serial.jar:."
echo "java -Djava.library.path="$LIBRARY" -cp "$CLASSPATH" serialReceiver.UsbReceiver"
java -Djava.library.path="$LIBRARY" -cp "$CLASSPATH" serialReceiver.UsbReceiver
Ok, I just tested it and removed the 1st line that listed a package. You can now copy the Processing sketch into Processing and save it as "UsbReceiver", and it will run.
Last edited: