I tried your code above (the full program) and it worked for me. I put that code in main.py and then ran the following code on my PC:
Code: Select all
import serial
import time
ser = serial.Serial('/dev/ttyACM0', baudrate=115200, interCharTimeout=1)
buf = bytearray(64)
start = time.time()
for i in range(128):
ser.write(buf)
ret = ser.read(2)
print(ret[0], ret[1])
buf[0] += 1
buf[1] += 1
end = time.time()
print(end - start, 128 * len(buf) / (end - start))
This will send 64 byte blocks, 128 times, and tell you the data rate at the end. I also modify the buffer that's being sent so it's not always all 0's.
I get 1.28s, which is 6400 bytes/sec.
That's rather slow (but not 200s long). To make it faster change the 64-byte buffer to 128 bytes (and also change the receiver to read in 128 bytes at a time). Then I get 0.37s, which is 43000 bytes/sec.
USB sends data in 64 byte packets, so probably reading 64 byte chunks means you hit some strange edge case where it has to wait for the next USB packet (which is empty to indicate that there was only 64 bytes to read) before returning the 64 bytes.
Also, doing usb.read(64) won't always give you 64 bytes. It might give you less. Use usb.recv(64, timeout) and adjust the timeout to try and get a full 64-byte buffer returned.