Leonardo baud rate for USB CDC

Can someone explain what Serial.begin(9600); does on the new Leonardo board. I ask because I need to know how fast I can read data from the USB port.

It does nothing, as you can see in the source code:

void Serial_::begin(uint16_t baud_count)
{
}

Has anyone done a speed test? I have a Leonardo, but I don't have the right cable.

I don't have the right cable.

What a USB cable?
What do you mean by a speed test, I could do one for you but I am not sure what you want to measure.

Grumpy_Mike:

I don't have the right cable.

What a USB cable?

I don't have a USB cable with the micro connector.

Grumpy_Mike:
What do you mean by a speed test, I could do one for you but I am not sure what you want to measure.

Something like the following. It's not my best work. I need to know how many bytes per second can be transferred through the virtual serial connection.

void setup() {
   int start = micros();
   for (int i=1000; i; --i)
      Serial.write('A');
   int end = micros();
   
   float delta = end-start;
   Serial.print(1000 / delta);
   Serial.println(" bytes/second");
}

void loop() {
}

Well it doesn't produce any output at all.
I don't think Serial.write is supported.

However this code:-

void setup() {
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
    while(digitalRead(2) == HIGH) { 
    // do nothing until pin 2 goes low
    delay(50);
  }
   unsigned int start = millis();
   for (int i=0; i<1000; i++)
     Keyboard.print("A");
   unsigned int end = millis();
   
   unsigned int delta = end-start;
   Keyboard.print(1000000.0 / float(delta));
   Keyboard.print(" bytes/second");
// do nothing:
  while(digitalRead(2) == LOW);
  delay(100);
  while(digitalRead(2) == LOW); // get rid of any bounce
  delay(100);
}

When played into TextEdit on my Mac Book Pro gives an answer of:- 252.59 bytes/second.
Which corresponds well to my timing of it with the second hand on my watch.

The actual speed will depend upon the application and the system you are playing it into.

Changing the print statement to:-

 Keyboard.println("The quick brown fox jumped over the lazy dog");

and only looping 100 times, with changing the calculation to:-
Keyboard.print((45.0 * 100.0 * 1000.0) / float(delta));
I included the LF in the value of 45 characters, gives a report of 254.18 bytes/second.

There is a lot more scrolling going on. I suppose a lot will depend on the speed and size of the input buffer in the receiving application.

Serial.write is well supported by the Leonardo but it's only usable for the (debugging) connection to the PC. The "standard" serial connection (pins 0 and 1) are on Serial1. You have to have the 1.0.1 version of the IDE though.

So when I said:-

I don't think Serial.write is supported.

I was right and it is Serial1.write that is supported.

Sorry, but I was right: Serial.write is supported:

CDC.cpp, line 191:

size_t Serial_::write(uint8_t c)

and line 230:

Serial_ Serial;

So why did nothing come out of the original code?

Because for the Leonardo you have to wait for the Serial to come up:

while (! Serial); // on the Leonardo wait for the Serial to start up

The code I posted was untested. The following works for the Uno and the Leonardo.

void setup() {

#if defined(USBCON)
while (!Serial) ; // wait for a serial connection to be established
#endif

Serial.begin(115200); // does nothing on the Leonardo
uint32_t start = micros();
for (int i=1000; i; --i)
Serial.write('.');
Serial.flush();
uint32_t end = micros();

uint32_t Bps = 1.0e9 / (end-start);
Serial.println("");
Serial.print(Bps);
Serial.println(" bytes/second");
}

void loop() {
}

It is best if you use code tags not quotes, it is the # next to the quote.
Why

Serial.flush();

I can't see it doing anything for you.

Grumpy_Mike:
It is best if you use code tags not quotes, it is the # next to the quote.
Why

Serial.flush();

I can't see it doing anything for you.

Because on the Uno, Serial.write is non-blocking.

It is best if you use code tags not quotes, it is the # next to the quote.
Why

Because the box is smaller, it scrolls, and the forum doesn't scramble your code up so people can copy and paste the code and reproduce your problems.

And mainly because that is what people want around here and it show common decency to comply. After all you are the one asking the questions.

Because on the Uno, Serial.write is non-blocking

Only to the extent of not having a full buffer.

Grumpy_Mike:

Grumpy_Mike:
It is best if you use code tags not quotes, it is the # next to the quote.
Why

Because the box is smaller, it scrolls, and the forum doesn't scramble your code up so people can copy and paste the code and reproduce your problems.

And mainly because that is what people want around here and it show common decency to comply. After all you are the one asking the questions.

Quote within quote confusion... I didn't ask why. I'm not trying to cause a problem.

Grumpy_Mike:

Because on the Uno, Serial.write is non-blocking

Only to the extent of not having a full buffer.

True but not calling Serial.flush wouldn't show how fast bytes were being transferred out of the chip which was my goal.

Ok that last code as poster reports:-

39258 bytes/second

On a Leonardo.

Grumpy_Mike:
Ok that last code as poster reports:-

39258 bytes/second

On a Leonardo.

It seems like the rate is highly dependent on the computer where it wasn't on the Uno.