USBSerial speed

I bumped into some unexpected latency when pulling bytes from the Photon over USBSerial so I decided to measure the speed. What I found was that regardless of the baud rate setting the throughput seems to max out around 196Kbits/sec. I’m guessing this lines up with the max bit serial baud rate setting of 230400 minus buffer management overhead.

Am I missing something or does this sound about right?

Thanks,
Kevin

Here’s the code:

char buffer[64];

void setup() {
	Serial.begin();
	for (int i = 0; i < sizeof(buffer) - 2; i++) {
		buffer[i] = random(32, 127);
	}
	buffer[sizeof(buffer) - 2] = '\r';
	buffer[sizeof(buffer) - 1] = '\n';
}

void loop() {
	while (Serial.availableForWrite() >= sizeof(buffer)) {
		Serial.print(buffer);
	}
}
import serial
import datetime

ser = serial.Serial('/dev/ttyACM0', baudrate=230400)

nbytes = 0
totalReadSeconds = 0
startTime = datetime.datetime.now()
while True:
    try:
        readStart = datetime.datetime.now()
        buf = ser.readline()
        readStop = datetime.datetime.now()
        if buf == b'':
            raise RuntimeError('serial connection broken')
        deltaRead = readStop - readStart
        totalReadSeconds += deltaRead.total_seconds()
        nbytes += len(buf)
    except BaseException as e:
        print(str(e))
        break

stopTime =  datetime.datetime.now()
ser.close()

deltaTime = stopTime - startTime
speed = nbytes / deltaTime.total_seconds()

print('startTime: %s' % (startTime.strftime('%Y-%m-%dT%H:%M:%S.%f')))
print('stopTime:  %s' % (stopTime.strftime('%Y-%m-%dT%H:%M:%S.%f')))
print('totalTime: %.2f' % (deltaTime.total_seconds()))
print('totalRead: %.2f' % (totalReadSeconds))
print('nbytes:    %d' % (nbytes))
print('xfer rate: %.2f bytes/sec, %.2f bits/sec' % (speed, speed * 8))

Can you rerun the test in MANUAL mode?
Does that give you the same result?
USB Serial also allows for a bigger buffer.
https://docs.particle.io/reference/device-os/firmware/photon/#acquireserialbuffer-
Does that allow for higher transer rates?
How about bulk-writes?
https://docs.particle.io/reference/device-os/firmware/photon/#write-

Serial.write(buf, len);
1 Like

@ScruffR Thanks for the suggestions, I will explore some more and report back.

My bad, I mistakenly assumed that the USB serial driver on my RPi3/Jessie would not be the bottleneck.

On RPi3/Jessie I typically see:

startTime: 2019-03-15T14:45:55.279015
stopTime: 2019-03-15T14:46:53.568330
totalTime: 58.29
totalRead: 57.94
nbytes: 976187
xfer rate: 16747.27 bytes/sec, 133978.17 bits/sec

whereas on my MacBook/Mojave the rates are significantly faster:

startTime: 2019-03-15T14:47:52.123236
stopTime: 2019-03-15T14:49:29.388340
totalTime: 97.27
totalRead: 96.58
nbytes: 7573637
xfer rate: 77865.92 bytes/sec, 622927.38 bits/sec

Looking at USB signaling on the scope it's pretty obvious that the RPi is idle a significant percentage of the time. I wish I knew why, but so be it.

2 Likes