-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
58 lines (53 loc) · 2.09 KB
/
Copy pathProgram.cs
File metadata and controls
58 lines (53 loc) · 2.09 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Runtime.InteropServices;
namespace SerialPlotAndLog
{
static class Program
{
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThread]
static void Main(string[] args)
{
// https://www.csharp411.com/console-output-from-winforms-application/
AttachConsole(ATTACH_PARENT_PROCESS);
string port = args.Length > 0 ? args[0] : null;
string sBaud = args.Length > 1 ? args[1] : null;
int baud = 0;
// works fine with cmd.exe
// a bit wonky with PowerShell.exe (displays but cursor location does not update)
// doesn't work at all with PowerShell_ISE.exe
// -- but this is really just for convenience, so let's not work any harder
if (port != null)
{
var ports = SerialPortInfo.GetSerialPorts().ToArray();
if (!ports.Any(p => p.PortName.Equals(port, StringComparison.Ordinal)))
{
if (ports.Length == 0)
{
Console.Error.WriteLine("No COM ports found.");
}
else
{
Console.Error.WriteLine($"No COM port found matching {port}; available ports:");
foreach (var p in ports)
Console.Error.WriteLine($" {p}");
}
}
}
if (sBaud != null && !int.TryParse(args[1], out baud))
{
Console.Error.WriteLine("Expected an integer as the second argument (for baud rate).");
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain(new SerialPortConfig(port, baud)));
}
}
}