From 95bac1d04c132ed3fd5e89af45e2fb26fc683a4f Mon Sep 17 00:00:00 2001 From: gohai Date: Tue, 19 Nov 2013 16:37:14 -0800 Subject: [PATCH] Serial: Add basic tests for throughput and latency --- .../serial_latency/arduino/serial_latency.ino | 12 +++++ .../test/serial_latency/serial_latency.pde | 53 +++++++++++++++++++ .../arduino/serial_thoughput.ino | 9 ++++ .../serial_throughput/serial_throughput.pde | 32 +++++++++++ 4 files changed, 106 insertions(+) create mode 100644 java/libraries/serial/test/serial_latency/arduino/serial_latency.ino create mode 100644 java/libraries/serial/test/serial_latency/serial_latency.pde create mode 100644 java/libraries/serial/test/serial_throughput/arduino/serial_thoughput.ino create mode 100644 java/libraries/serial/test/serial_throughput/serial_throughput.pde diff --git a/java/libraries/serial/test/serial_latency/arduino/serial_latency.ino b/java/libraries/serial/test/serial_latency/arduino/serial_latency.ino new file mode 100644 index 000000000..d4711fe64 --- /dev/null +++ b/java/libraries/serial/test/serial_latency/arduino/serial_latency.ino @@ -0,0 +1,12 @@ +void setup() { + Serial.begin(115200); +} + +void loop() { + while (true) { + int in = Serial.read(); + if (in != -1) { + Serial.write(in); + } + } +} diff --git a/java/libraries/serial/test/serial_latency/serial_latency.pde b/java/libraries/serial/test/serial_latency/serial_latency.pde new file mode 100644 index 000000000..17e0a2756 --- /dev/null +++ b/java/libraries/serial/test/serial_latency/serial_latency.pde @@ -0,0 +1,53 @@ +// Arduino Duemilanove (168) on OS X 10.9 +// with either 115200 or 38400 bps +// on Processing 2.0.3 (cu & tty): 24 ms avg, 35 ms max +// on Processing 2.1b1 (cu & tty): 18 ms avg, 35 ms max + +import processing.serial.*; +Serial serial; +int start; +byte out = '@'; +int last_send = 0; +byte[] in = new byte[32768]; +long num_fail = 0; +long num_recv = 0; +int max_latency = 0; + +void setup() { + println(serial.list()); + // change this accordingly + serial = new Serial(this, serial.list()[0], 115200); + start = millis(); +} + +void draw() { + background(255); + if (0 < serial.available()) { + int recv = serial.readBytes(in); + for (int i=0; i < recv; i++) { + if (in[i] == out) { + num_recv++; + int now = millis(); + if (max_latency < now-last_send) { + max_latency = now-last_send; + } + last_send = 0; + } + } + } + if (last_send != 0 && 1000 < millis()-last_send) { + num_fail++; + last_send = 0; + println(num_fail+" bytes timed out"); + } + if (last_send == 0) { + if (out == 'Z') { + out = '@'; + } + serial.write(++out); + last_send = millis(); + } + fill(0); + text(((millis()-start)/(float)num_recv+" ms avg"), 0, height/2); + text(max_latency+" ms max", 0, height/2+20); +} diff --git a/java/libraries/serial/test/serial_throughput/arduino/serial_thoughput.ino b/java/libraries/serial/test/serial_throughput/arduino/serial_thoughput.ino new file mode 100644 index 000000000..dc950ce5a --- /dev/null +++ b/java/libraries/serial/test/serial_throughput/arduino/serial_thoughput.ino @@ -0,0 +1,9 @@ +void setup() { + Serial.begin(115200); +} + +void loop() { + while (true) { + Serial.write('.'); + } +} diff --git a/java/libraries/serial/test/serial_throughput/serial_throughput.pde b/java/libraries/serial/test/serial_throughput/serial_throughput.pde new file mode 100644 index 000000000..477fdf3bc --- /dev/null +++ b/java/libraries/serial/test/serial_throughput/serial_throughput.pde @@ -0,0 +1,32 @@ +import processing.serial.*; +Serial serial; +int start; +byte[] in = new byte[32768]; +long num_ok = 0; +long num_fail = 0; +long num_recv = 0; + +void setup() { + println(serial.list()); + // change this accordingly + serial = new Serial(this, serial.list()[0], 115200); + start = millis(); +} + +void draw() { + background(255); + if (0 < serial.available()) { + int recv = serial.readBytes(in); + for (int i=0; i < recv; i++) { + if (in[i] == '.') { + num_ok++; + } else { + num_fail++; + println("Received "+num_fail+" unexpected bytes"); + } + num_recv++; + } + } + fill(0); + text(num_recv/((millis()-start)/1000.0), 0, height/2); +}