Created
April 24, 2020 03:59
-
-
Save nanase/6cc57e14a572196baefd893ade67d32e to your computer and use it in GitHub Desktop.
Arduinoでシリアル通信の転送速度を測定します。PCへの送信のみです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// このスケッチは以下の投稿にあるものを改良したもの | |
// https://forum.arduino.cc/index.php?topic=575333.0 | |
char text[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQR" | |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQR" | |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQR" | |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQR"; | |
void setup() { | |
Serial.begin(250000); | |
while (!Serial) ; | |
} | |
void measure(size_t length, uint32_t *sent, uint32_t *elapsed) { | |
uint32_t starttime, eltime, datasent; | |
starttime = micros(); | |
datasent = 0; | |
while (1) { | |
if (micros() - starttime > 1000000) | |
break; | |
Serial.write(text, length); | |
datasent += length; | |
} | |
Serial.flush(); | |
eltime = micros() - starttime; | |
Serial.println(); | |
Serial.print(F("size: ")); | |
Serial.print(length, DEC); | |
Serial.print(F(", sent: ")); | |
Serial.print(datasent, DEC); | |
Serial.print(F(", elapsed: ")); | |
Serial.print(eltime); | |
Serial.print(F(", rate: ")); | |
Serial.print((datasent * 8e6) / eltime); | |
Serial.println(); | |
*sent = datasent; | |
*elapsed = eltime; | |
} | |
uint32_t sent[256]; | |
uint32_t elapsed[256]; | |
void loop() { | |
Serial.print(F("Press any key to start!")); | |
while (!Serial.available()) ; | |
while (Serial.read() < 0) ; | |
for (size_t i = 0; i < 256; i++) | |
measure(i + 1, &sent[i], &elapsed[i]); | |
for (size_t i = 0; i < 256; i++) { | |
Serial.print(i, DEC); | |
Serial.print(F(", ")); | |
Serial.print(sent[i]); | |
Serial.print(F(", ")); | |
Serial.println(elapsed[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment