USA
Offline
Full Member
Karma: 0
Posts: 232
|
 |
« on: June 04, 2012, 10:13:28 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 71
Posts: 3486
|
 |
« Reply #1 on: June 04, 2012, 10:52:57 am » |
It does nothing, as you can see in the source code: void Serial_::begin(uint16_t baud_count) { }
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Full Member
Karma: 0
Posts: 232
|
 |
« Reply #2 on: June 04, 2012, 01:46:13 pm » |
Has anyone done a speed test? I have a Leonardo, but I don't have the right cable.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #3 on: June 04, 2012, 03:21:35 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Full Member
Karma: 0
Posts: 232
|
 |
« Reply #4 on: June 04, 2012, 04:03:27 pm » |
I don't have the right cable. What a USB cable? I don't have a USB cable with the micro connector. 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() { }
|
|
|
|
« Last Edit: June 04, 2012, 04:22:15 pm by mkwired »
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #5 on: June 04, 2012, 04:46:28 pm » |
Well it doesn't produce any output at all. I don't think Serial.write is supported.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #6 on: June 04, 2012, 05:09:26 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #7 on: June 04, 2012, 05:17:45 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 71
Posts: 3486
|
 |
« Reply #8 on: June 04, 2012, 06:01:20 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #9 on: June 04, 2012, 06:07:09 pm » |
So when I said:- I don't think Serial.write is supported. I was right and it is Serial1.write that is supported.
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 71
Posts: 3486
|
 |
« Reply #10 on: June 04, 2012, 06:12:16 pm » |
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;
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #11 on: June 04, 2012, 06:16:10 pm » |
So why did nothing come out of the original code?
|
|
|
|
|
Logged
|
|
|
|
|
Switzerland
Offline
Faraday Member
Karma: 71
Posts: 3486
|
 |
« Reply #12 on: June 04, 2012, 06:26:10 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Full Member
Karma: 0
Posts: 232
|
 |
« Reply #13 on: June 05, 2012, 10:37:30 am » |
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() { }
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 299
Posts: 26031
Solder is electric glue
|
 |
« Reply #14 on: June 05, 2012, 03:46:32 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|