Change console windows title based on connected port and status

This commit is contained in:
Edward Wright 2023-02-16 11:46:21 -05:00
parent 17250a57b5
commit 185e541b12

View File

@ -1,14 +1,14 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.IO; using System.IO;
using System.IO.Ports; using System.IO.Ports;
using System.Text; using System.Linq;
using System.Threading;
using System.Management; using System.Management;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Newtonsoft.Json; using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace SimplySerial namespace SimplySerial
{ {
@ -257,6 +257,8 @@ namespace SimplySerial
continue; continue;
} }
Console.Title = $"{port.name}: {port.board.make} {port.board.model}";
// if we get this far, clear the screen and send the connection message if not in 'quiet' mode // if we get this far, clear the screen and send the connection message if not in 'quiet' mode
if (clearScreen) if (clearScreen)
{ {
@ -362,6 +364,7 @@ namespace SimplySerial
ExitProgram((e.GetType() + " occurred while attempting to read/write to/from " + port.name + "."), exitCode: -1); ExitProgram((e.GetType() + " occurred while attempting to read/write to/from " + port.name + "."), exitCode: -1);
else else
{ {
Console.Title = $"{port.name}: (disconnected)";
Output("\n<<< Communications Interrupted >>>\n"); Output("\n<<< Communications Interrupted >>>\n");
} }
try try
@ -375,11 +378,13 @@ namespace SimplySerial
Thread.Sleep(2000); // sort-of arbitrary delay - should be long enough to read the "interrupted" message Thread.Sleep(2000); // sort-of arbitrary delay - should be long enough to read the "interrupted" message
if (autoConnect == AutoConnect.ANY) if (autoConnect == AutoConnect.ANY)
{ {
Console.Title = "SimplySerial: Searching...";
port.name = String.Empty; port.name = String.Empty;
Output("<<< Attemping to connect to any available COM port. Use CTRL-X to cancel >>>"); Output("<<< Attemping to connect to any available COM port. Use CTRL-X to cancel >>>");
} }
else if (autoConnect == AutoConnect.ONE) else if (autoConnect == AutoConnect.ONE)
{ {
Console.Title = $"{port.name}: Searching...";
Output("<<< Attempting to re-connect to " + port.name + ". Use CTRL-X to cancel >>>"); Output("<<< Attempting to re-connect to " + port.name + ". Use CTRL-X to cancel >>>");
} }
break; break;
@ -410,7 +415,7 @@ namespace SimplySerial
foreach (string arg in args) foreach (string arg in args)
{ {
// split argument into components based on 'key:value' formatting // split argument into components based on 'key:value' formatting
string[] argument = arg.Split(new [] { ':' }, 2); string[] argument = arg.Split(new[] { ':' }, 2);
argument[0] = argument[0].ToLower(); argument[0] = argument[0].ToLower();
// help // help
@ -622,6 +627,7 @@ namespace SimplySerial
if (autoConnect == AutoConnect.ANY) if (autoConnect == AutoConnect.ANY)
{ {
Console.Title = "SimplySerial: Searching...";
Output("<<< Attemping to connect to any available COM port. Use CTRL-X to cancel >>>"); Output("<<< Attemping to connect to any available COM port. Use CTRL-X to cancel >>>");
} }
else if (autoConnect == AutoConnect.ONE) else if (autoConnect == AutoConnect.ONE)
@ -631,9 +637,15 @@ namespace SimplySerial
Console.Clear(); Console.Clear();
} }
if (port.name == String.Empty) if (port.name == String.Empty)
{
Console.Title = "SimplySerial: Searching...";
Output("<<< Attempting to connect to first available COM port. Use CTRL-X to cancel >>>"); Output("<<< Attempting to connect to first available COM port. Use CTRL-X to cancel >>>");
}
else else
{
Console.Title = $"{port.name}: Searching...";
Output("<<< Attempting to connect to " + port.name + ". Use CTRL-X to cancel >>>"); Output("<<< Attempting to connect to " + port.name + ". Use CTRL-X to cancel >>>");
}
} }
// if we made it this far, everything has been processed and we're ready to proceed! // if we made it this far, everything has been processed and we're ready to proceed!
@ -644,7 +656,7 @@ namespace SimplySerial
/// Writes messages using Console.WriteLine() as long as the 'Quiet' option hasn't been enabled /// Writes messages using Console.WriteLine() as long as the 'Quiet' option hasn't been enabled
/// </summary> /// </summary>
/// <param name="message">Message to output (assuming 'Quiet' is false)</param> /// <param name="message">Message to output (assuming 'Quiet' is false)</param>
static void Output(string message, bool force=false, bool newline=true, bool flush=false) static void Output(string message, bool force = false, bool newline = true, bool flush = false)
{ {
if (!SimplySerial.Quiet || force) if (!SimplySerial.Quiet || force)
{ {
@ -665,7 +677,7 @@ namespace SimplySerial
foreach (byte c in message) foreach (byte c in message)
{ {
if ((c > 31 && c < 128) || (c == 8) || (c == 9) || (c == 10) || (c == 13)) if ((c > 31 && c < 128) || (c == 8) || (c == 9) || (c == 10) || (c == 13))
newMessage += (char) c; newMessage += (char)c;
else else
newMessage += $"[{c:X2}]"; newMessage += $"[{c:X2}]";
} }
@ -761,7 +773,7 @@ namespace SimplySerial
/// <param name="message">Message to display - should indicate the reason why the program is terminating.</param> /// <param name="message">Message to display - should indicate the reason why the program is terminating.</param>
/// <param name="exitCode">Code to return to parent process. Should be &lt;0 if an error occurred, &gt;=0 if program is terminating normally.</param> /// <param name="exitCode">Code to return to parent process. Should be &lt;0 if an error occurred, &gt;=0 if program is terminating normally.</param>
/// <param name="silent">Exits without displaying a message or asking for a key press when set to 'true'</param> /// <param name="silent">Exits without displaying a message or asking for a key press when set to 'true'</param>
static void ExitProgram(string message="", int exitCode=0, bool silent=false) static void ExitProgram(string message = "", int exitCode = 0, bool silent = false)
{ {
// the serial port should be closed before exiting // the serial port should be closed before exiting
if (serialPort != null && serialPort.IsOpen) if (serialPort != null && serialPort.IsOpen)
@ -881,7 +893,7 @@ namespace SimplySerial
if (mBoard == null) if (mBoard == null)
{ {
mBoard = new Board(vid:vid, pid:pid); mBoard = new Board(vid: vid, pid: pid);
Vendor mVendor = null; Vendor mVendor = null;
if (boardData.vendors != null) if (boardData.vendors != null)