Created
September 16, 2019 15:49
-
-
Save nanase/14dde0d8c3e14a3713adcf2f92a2ec5f to your computer and use it in GitHub Desktop.
Arduino MKR ZERO Serial Speed Test
This file contains 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
//#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); | |
} |
This file contains 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
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); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result