Teensy 4.0 USB transfers per second

Status
Not open for further replies.
D

DeletedUser

Guest
Hi, I am trying to read sensor data from a Teensy 4.0 to my PC as fast as possible. I used the following code to check the transfers per second:
Code:
void setup() {
  Serial.begin(115200);
}

void loop() {
  byte test = 4;
  Serial.println(test);
  delayMicroseconds(500);
}
I got a range from 1000 to 67 transfers per second. I know the transfers per second depend on the OS and hardware, but I was surprised to see nearly constant 1000 transfers/second during some tests and nearly constant 67 transfers/second during other tests, but I did not change the connected hardware, OS or PC load. I have read here: https://www.pjrc.com/teensy/td_serial.html that the USB host controller chip of my PC sets the transfer per second rate. Is there a way to constantly get more than 500 transfers/second? I thought of maybe setting the Teensy USB connection to isychronous or interrupt for example, if that is even possible. At first I thought I needed many Mbits/sec, but what I actually need are only some bytes, but as fast as possible.
 
Hi,
At what rate do you want to transfer data, and how are you receiving it on your PC? I have an application
, for example,
that transmits serial data from a Teensy 4.0 over USB at roughly 200 Kbytes/s - that's no problem. The Teensy 4.0 and the USB conection are not the limiting factor - the main problems occur at the PC software end if you want to receive data reliably at high speeds. What I would advise you to do first is change your test program so that the value you transmit is incremented each time, and check what values the software on your PC is receiving.
 
You haven't said what software you're using to receive the data, or even which operating system. Those details matter quite a lot. Poorly written software on the PC side can cause this sort of terrible performance.

As a first sanity check, just to make sure your computer is working properly, I recommend you run this benchmark program.

https://github.com/PaulStoffregen/USB-Serial-Print-Speed-Test/blob/master/usb_serial_print_speed.ino

On your PC, use the Arduino Serial Monitor to view the data. Make sure you select from "Teensy Ports" in Arduino's Tools > Ports menu. If you select from the "Serial" part of that menu, Arduino's normal serial monitor is used. Selecting from the Teensy ports uses an improved serial monitor that is able to receive faster. (also be aware this test can put quite a load on slower computers, especially if using MacOS)

What number do you see in the lines/sec after it has run for several seconds?
 
Thank you for your replies. When using the test program, I get up to 858286 lines/sec with both the Arduino IDE and my python test program. My program looks like this:
Code:
import serial.tools.list_ports
import time

        serial_port = serial.Serial('COM6', baudrate=115200)
        start_time_s = time.time()
        while True:
            print(serial_port.readline().decode('ascii'))
            print_time_ms = (time.time() - start_time_s) * 1000
            print(print_time_ms)
The problem is that my program only receives data packages about every 15 ms. I do not need many lines/sec, I need many data transfers / sec. So in my python program the printed time has always a time gap of about 15 ms. for example:
Code:
4010.450839996338
count=80912601, lines/sec=630489

4026.071548461914
count=80912602, lines/sec=630489

4026.071548461914
count=80912603, lines/sec=630489

4026.071548461914
count=80912604, lines/sec=630489

4026.071548461914
count=80912605, lines/sec=630489

4026.071548461914
count=80912606, lines/sec=630489

4026.071548461914
count=80912607, lines/sec=630489

4026.071548461914
count=80912608, lines/sec=630489

4026.071548461914
count=675131

4026.071548461914
count=81347931, lines/sec=675131

4026.071548461914
count=81347932, lines/sec=675131

4026.071548461914
count=81347933, lines/sec=675131

4026.071548461914
count=81347934, lines/sec=675131

4026.071548461914
count=81347935, lines/sec=675131

4026.071548461914
count=81347936, lines/sec=675131

4026.071548461914
count=81347937, lines/sec=675131

4026.071548461914
count=81347938, lines/sec=675131

4026.071548461914
count=81347939, lines/sec=675131

4026.071548461914
count=81347940, lines/sec=675131

4026.071548461914
count=81347941, lines/sec=675131

4026.071548461914
count=81347942, lines/sec=675131

4026.071548461914
count=81347943, lines/sec=675131

4026.071548461914
count=81347944, lines/sec=675131

4026.071548461914
count=81347945, lines/sec=675131

4026.071548461914
count=81347946, lines/sec=675131

4041.6927337646484
count=81347947, lines/sec=675131
It should look more like this:
Code:
4010.450839996338
count=80912601, lines/sec=630489

4011.071548461914
count=80912602, lines/sec=630489

4012.071548461914
count=80912603, lines/sec=630489

4013.071548461914
count=80912604, lines/sec=630489

4014.071548461914
count=80912605, lines/sec=630489

4015.071548461914
count=80912606, lines/sec=630489

4016.071548461914
count=80912607, lines/sec=630489

4018.6927337646484
count=81347947, lines/sec=675131
 
Last edited:
Hi,
Try adding
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Serial.setTimeout(1);
after Serial.begin.

[/FONT]
 
Hi,
Try adding
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Serial.setTimeout(1);
after Serial.begin.

[/FONT]

The output still has time gaps of 15ms, unfortunately. But if I add a delay of 1 milisecond to my python program, too, there is only one printout with the same time stamp. But the time stamp is still 15 ms different between each sample. I think this behaviour is weird, maybe the python clock is not reliable. In the end I will probably use Matlab to gather the data from the serial port, I just wanted to quickly check how many samples per second I can get.
 
Maybe I got this wrong, but from what I gathered:

- You run a loop on the Teensy which does "Serial.println" followed by a delay of 0.5ms. So you are "printing" on the virtual Teensy serial port
- You run a tool on the PC which monitors the virtual serial port, reads a line, prints time on the console (?)

If the above is right, you are introducing way too many factors which may cause delay.

Things to try:
Teensy
- reduce or comment out the delay()
- replace the Serial.println() with a Serial.write (our you may use the USB lib straight away and avoid the serial-usb layer: https://www.pjrc.com/teensy/rawhid.html ) . If you need that '\n' you have to write it explicitely of course
PC
- instead of printing to console, log to file (buffered write, of course). Don't know how to do that in Python
 
Status
Not open for further replies.
Back
Top