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))