forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
76 lines (67 loc) · 2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.IO.Ports;
using System.Text;
if (args.Length == 0)
{
Console.WriteLine("arduino-demo <portName> [<baudRate>=9600]");
return;
}
// to get port name you can use SerialPort.GetPortNames()
string portName = args[0];
int baudRate = args.Length >= 2 ? int.Parse(args[1]) : 9600;
using SerialPort sp = new SerialPort(portName);
sp.Encoding = Encoding.UTF8;
sp.BaudRate = baudRate;
sp.ReadTimeout = 1000;
sp.WriteTimeout = 1000;
sp.Open();
bool finished = false;
Console.CancelKeyPress += (a, b) =>
{
finished = true;
// close port to kill pending operations
sp.Close();
};
Console.WriteLine("Type '!q' or Ctrl-C to exit...");
Console.WriteLine("Example commands:");
Console.WriteLine(" DIR");
Console.WriteLine(" change direction of spinning");
Console.WriteLine(" RATE 300");
Console.WriteLine(" change rate to 300ms");
Console.WriteLine(" COL FF0000");
Console.WriteLine(" change LED color to red");
Console.WriteLine(" SPIN");
Console.WriteLine(" start/stop spinning");
while (!finished)
{
string? line = Console.ReadLine();
if (line is object && line == "!q")
break;
try
{
sp.WriteLine(line);
}
catch (TimeoutException)
{
Console.WriteLine("ERROR: Sending command timed out");
}
if (finished)
break;
// if RATE is set to really high Arduino may fail to respond in time
// then on the next command you might get an old message
// ReadExisting will read everything from the internal buffer
string existingData = sp.ReadExisting();
Console.Write(existingData);
if (!existingData.Contains('\n') && !existingData.Contains('\r'))
{
// we didn't get the response yet, let's wait for it then
try
{
Console.WriteLine(sp.ReadLine());
}
catch (TimeoutException)
{
Console.WriteLine($"ERROR: No response in {sp.ReadTimeout}ms.");
}
}
}