Skip to content

Instantly share code, notes, and snippets.

@nanase
Created September 16, 2019 15:49
Show Gist options
  • Save nanase/14dde0d8c3e14a3713adcf2f92a2ec5f to your computer and use it in GitHub Desktop.
Save nanase/14dde0d8c3e14a3713adcf2f92a2ec5f to your computer and use it in GitHub Desktop.
Arduino MKR ZERO Serial Speed Test
//#define Serial SerialUSB
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial)
;
}
void wait()
{
int c;
do
{
while (!Serial.available())
delay(1);
c = Serial.read();
} while (c != 0x42);
Serial.write(0x53);
Serial.flush();
}
void loop()
{
static uint8_t buffer[1024];
wait();
while (!Serial.available())
delay(1);
int size = Serial.read();
if (size < 0)
return;
size++;
digitalWrite(LED_BUILTIN, HIGH);
for (int i = 0; i < size; i++)
Serial.write(buffer, 1024);
Serial.flush();
digitalWrite(LED_BUILTIN, LOW);
}
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApp
{
internal class Program
{
private static void Test(SerialPort serial, byte size)
{
int response;
do
{
serial.DiscardInBuffer();
serial.Write(new byte[] { 0x42 }, 0, 1);
serial.BaseStream.Flush();
response = serial.ReadByte();
} while (response != 0x53);
serial.Write(new [] { size }, 0, 1);
serial.BaseStream.Flush();
Thread.Sleep(1);
var totalReadSize = 0;
var goalReadSize = (size + 1) * 1024;
var sw = new Stopwatch();
sw.Start();
do
{
var buffer = new byte[1024];
var readsize = serial.Read(buffer, 0, 1024);
if (readsize < 0)
break;
totalReadSize += readsize;
} while (totalReadSize < goalReadSize);
sw.Stop();
var time = (double)sw.ElapsedTicks / Stopwatch.Frequency;
Console.WriteLine($"{size},{totalReadSize},{time}");
}
private static void Main()
{
const int speed = 115_200;
using (var serial = new SerialPort("COM3", speed))
{
serial.Open();
for (var i = 0; i < 256; i++)
{
Test(serial, (byte)i);
}
}
}
}
}
@nanase
Copy link
Author

nanase commented Sep 16, 2019

Result

キャプチャ

Transfer size(KB) Elapsed Time(s) Transfer Rate(KB/s) Transfer Rate(Mbps)
1 0.001 1009.6 7.89
2 0.002 875.8 6.84
3 0.004 680.6 5.32
4 0.006 687.9 5.37
5 0.008 629.5 4.92
6 0.009 676.4 5.28
7 0.012 597.5 4.67
8 0.013 610.3 4.77
9 0.014 629.4 4.92
10 0.016 607.2 4.74
11 0.018 606.4 4.74
12 0.020 599.0 4.68
13 0.022 604.0 4.72
14 0.024 582.5 4.55
15 0.025 596.9 4.66
16 0.027 596.9 4.66
17 0.029 580.1 4.53
18 0.031 586.6 4.58
19 0.033 581.1 4.54
20 0.034 580.4 4.53
21 0.037 574.3 4.49
22 0.038 579.8 4.53
23 0.040 578.2 4.52
24 0.042 578.2 4.52
25 0.043 582.2 4.55
26 0.044 585.4 4.57
27 0.047 580.5 4.53
28 0.048 579.5 4.53
29 0.050 577.0 4.51
30 0.051 589.7 4.61
31 0.053 581.0 4.54
32 0.055 579.4 4.53
33 0.057 581.8 4.55
34 0.059 577.3 4.51
35 0.061 575.6 4.50
36 0.063 572.6 4.47
37 0.063 584.8 4.57
38 0.066 575.0 4.49
39 0.068 574.7 4.49
40 0.070 571.2 4.46
41 0.070 582.8 4.55
42 0.072 586.7 4.58
43 0.073 586.4 4.58
44 0.076 576.2 4.50
45 0.077 581.2 4.54
46 0.080 574.9 4.49
47 0.081 582.3 4.55
48 0.083 581.1 4.54
49 0.085 576.8 4.51
50 0.087 575.2 4.49
51 0.089 574.1 4.49
52 0.090 576.8 4.51
53 0.091 581.6 4.54
54 0.092 585.3 4.57
55 0.095 581.7 4.54
56 0.097 576.3 4.50
57 0.099 578.5 4.52
58 0.101 572.1 4.47
59 0.103 574.5 4.49
60 0.105 573.8 4.48
61 0.106 573.0 4.48
62 0.107 577.4 4.51
63 0.109 579.4 4.53
64 0.111 576.0 4.50
65 0.112 578.0 4.52
66 0.114 579.1 4.52
67 0.117 573.0 4.48
68 0.120 567.2 4.43
69 0.118 583.5 4.56
70 0.134 522.7 4.08
71 0.125 568.7 4.44
72 0.126 569.7 4.45
73 0.127 575.1 4.49
74 0.128 577.2 4.51
75 0.130 575.2 4.49
76 0.132 577.5 4.51
77 0.133 579.9 4.53
78 0.135 576.6 4.50
79 0.137 576.8 4.51
80 0.138 581.2 4.54
81 0.141 575.8 4.50
82 0.143 574.9 4.49
83 0.145 572.0 4.47
84 0.146 576.3 4.50
85 0.147 578.0 4.52
86 0.148 582.3 4.55
87 0.151 574.3 4.49
88 0.153 574.9 4.49
89 0.153 580.1 4.53
90 0.157 574.8 4.49
91 0.158 576.4 4.50
92 0.160 573.6 4.48
93 0.161 576.3 4.50
94 0.163 578.4 4.52
95 0.165 574.8 4.49
96 0.167 576.0 4.50
97 0.170 571.1 4.46
98 0.171 571.8 4.47
99 0.172 575.5 4.50
100 0.173 579.3 4.53
101 0.175 575.5 4.50
102 0.177 577.4 4.51
103 0.180 572.8 4.48
104 0.181 573.7 4.48
105 0.182 577.2 4.51
106 0.185 572.9 4.48
107 0.185 577.2 4.51
108 0.187 576.3 4.50
109 0.189 576.7 4.51
110 0.191 575.3 4.49
111 0.193 575.5 4.50
112 0.196 570.3 4.46
113 0.196 576.4 4.50
114 0.198 577.1 4.51
115 0.198 581.5 4.54
116 0.203 570.9 4.46
117 0.201 580.9 4.54
118 0.205 575.3 4.49
119 0.206 577.2 4.51
120 0.208 575.6 4.50
121 0.210 576.6 4.50
122 0.213 573.0 4.48
123 0.214 575.2 4.49
124 0.217 572.1 4.47
125 0.219 572.0 4.47
126 0.220 571.6 4.47
127 0.222 572.7 4.47
128 0.223 572.9 4.48
129 0.225 573.2 4.48
130 0.227 573.1 4.48
131 0.229 571.0 4.46
132 0.229 576.0 4.50
133 0.231 575.0 4.49
134 0.233 576.0 4.50
135 0.236 572.7 4.47
136 0.237 573.8 4.48
137 0.239 573.6 4.48
138 0.236 583.9 4.56
139 0.242 575.3 4.49
140 0.243 575.3 4.49
141 0.246 574.1 4.49
142 0.248 573.6 4.48
143 0.248 576.4 4.50
144 0.251 574.2 4.49
145 0.253 572.3 4.47
146 0.254 574.8 4.49
147 0.257 572.8 4.47
148 0.257 574.9 4.49
149 0.261 571.9 4.47
150 0.261 575.4 4.50
151 0.262 575.3 4.49
152 0.265 572.9 4.48
153 0.265 577.4 4.51
154 0.266 578.0 4.52
155 0.268 577.4 4.51
156 0.271 575.5 4.50
157 0.274 573.5 4.48
158 0.275 573.5 4.48
159 0.276 576.8 4.51
160 0.279 574.1 4.49
161 0.282 571.4 4.46
162 0.284 570.1 4.45
163 0.283 575.5 4.50
164 0.285 574.9 4.49
165 0.289 570.8 4.46
166 0.291 570.2 4.45
167 0.292 571.4 4.46
168 0.294 571.9 4.47
169 0.294 574.0 4.48
170 0.296 573.5 4.48
171 0.297 575.8 4.50
172 0.299 574.7 4.49
173 0.302 572.9 4.48
174 0.305 569.8 4.45
175 0.302 579.6 4.53
176 0.308 572.1 4.47
177 0.308 575.4 4.50
178 0.311 571.5 4.46
179 0.313 572.6 4.47
180 0.314 572.6 4.47
181 0.315 574.9 4.49
182 0.316 576.5 4.50
183 0.319 573.6 4.48
184 0.320 575.8 4.50
185 0.323 572.3 4.47
186 0.324 574.2 4.49
187 0.327 571.7 4.47
188 0.328 572.9 4.48
189 0.331 570.2 4.45
190 0.333 571.3 4.46
191 0.332 575.3 4.49
192 0.336 571.5 4.46
193 0.342 564.7 4.41
194 0.341 569.4 4.45
195 0.341 571.0 4.46
196 0.345 568.1 4.44
197 0.348 566.8 4.43
198 0.345 574.0 4.48
199 0.348 571.7 4.47
200 0.350 571.9 4.47
201 0.352 570.3 4.46
202 0.354 570.5 4.46
203 0.354 573.1 4.48
204 0.355 574.6 4.49
205 0.360 569.0 4.45
206 0.360 572.6 4.47
207 0.362 572.5 4.47
208 0.366 569.1 4.45
209 0.367 569.5 4.45
210 0.367 572.2 4.47
211 0.371 568.5 4.44
212 0.369 573.8 4.48
213 0.372 572.4 4.47
214 0.374 572.9 4.48
215 0.375 572.8 4.47
216 0.378 571.1 4.46
217 0.381 570.0 4.45
218 0.381 571.8 4.47
219 0.380 576.1 4.50
220 0.385 572.2 4.47
221 0.386 572.6 4.47
222 0.389 571.0 4.46
223 0.388 574.4 4.49
224 0.390 574.2 4.49
225 0.391 575.5 4.50
226 0.395 572.5 4.47
227 0.395 574.4 4.49
228 0.397 574.4 4.49
229 0.399 573.2 4.48
230 0.402 572.4 4.47
231 0.402 575.3 4.49
232 0.403 576.2 4.50
233 0.407 573.1 4.48
234 0.408 573.0 4.48
235 0.410 572.5 4.47
236 0.412 573.1 4.48
237 0.417 567.8 4.44
238 0.411 578.4 4.52
239 0.417 572.7 4.47
240 0.418 573.6 4.48
241 0.421 572.9 4.48
242 0.424 571.2 4.46
243 0.446 545.4 4.26
244 0.424 575.8 4.50
245 0.428 572.1 4.47
246 0.428 574.8 4.49
247 0.432 571.2 4.46
248 0.431 574.9 4.49
249 0.437 569.2 4.45
250 0.437 572.6 4.47
251 0.440 570.4 4.46
252 0.439 573.9 4.48
253 0.442 572.5 4.47
254 0.443 573.5 4.48
255 0.446 571.4 4.46
256 0.447 573.2 4.48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment