Skip to content

Commit f8bceac

Browse files
committed
Serial: Add basic tests for throughput and latency
1 parent d9768bc commit f8bceac

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
void setup() {
2+
Serial.begin(115200);
3+
}
4+
5+
void loop() {
6+
while (true) {
7+
int in = Serial.read();
8+
if (in != -1) {
9+
Serial.write(in);
10+
}
11+
}
12+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Arduino Duemilanove (168) on OS X 10.9
2+
// with either 115200 or 38400 bps
3+
// on Processing 2.0.3 (cu & tty): 24 ms avg, 35 ms max
4+
// on Processing 2.1b1 (cu & tty): 18 ms avg, 35 ms max
5+
6+
import processing.serial.*;
7+
Serial serial;
8+
int start;
9+
byte out = '@';
10+
int last_send = 0;
11+
byte[] in = new byte[32768];
12+
long num_fail = 0;
13+
long num_recv = 0;
14+
int max_latency = 0;
15+
16+
void setup() {
17+
println(serial.list());
18+
// change this accordingly
19+
serial = new Serial(this, serial.list()[0], 115200);
20+
start = millis();
21+
}
22+
23+
void draw() {
24+
background(255);
25+
if (0 < serial.available()) {
26+
int recv = serial.readBytes(in);
27+
for (int i=0; i < recv; i++) {
28+
if (in[i] == out) {
29+
num_recv++;
30+
int now = millis();
31+
if (max_latency < now-last_send) {
32+
max_latency = now-last_send;
33+
}
34+
last_send = 0;
35+
}
36+
}
37+
}
38+
if (last_send != 0 && 1000 < millis()-last_send) {
39+
num_fail++;
40+
last_send = 0;
41+
println(num_fail+" bytes timed out");
42+
}
43+
if (last_send == 0) {
44+
if (out == 'Z') {
45+
out = '@';
46+
}
47+
serial.write(++out);
48+
last_send = millis();
49+
}
50+
fill(0);
51+
text(((millis()-start)/(float)num_recv+" ms avg"), 0, height/2);
52+
text(max_latency+" ms max", 0, height/2+20);
53+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
void setup() {
2+
Serial.begin(115200);
3+
}
4+
5+
void loop() {
6+
while (true) {
7+
Serial.write('.');
8+
}
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import processing.serial.*;
2+
Serial serial;
3+
int start;
4+
byte[] in = new byte[32768];
5+
long num_ok = 0;
6+
long num_fail = 0;
7+
long num_recv = 0;
8+
9+
void setup() {
10+
println(serial.list());
11+
// change this accordingly
12+
serial = new Serial(this, serial.list()[0], 115200);
13+
start = millis();
14+
}
15+
16+
void draw() {
17+
background(255);
18+
if (0 < serial.available()) {
19+
int recv = serial.readBytes(in);
20+
for (int i=0; i < recv; i++) {
21+
if (in[i] == '.') {
22+
num_ok++;
23+
} else {
24+
num_fail++;
25+
println("Received "+num_fail+" unexpected bytes");
26+
}
27+
num_recv++;
28+
}
29+
}
30+
fill(0);
31+
text(num_recv/((millis()-start)/1000.0), 0, height/2);
32+
}

0 commit comments

Comments
 (0)