mirror of
https://github.com/fasteddy516/SimplySerial.git
synced 2024-11-14 22:14:36 +00:00
More command-line argument processing work
This commit is contained in:
parent
37fb2636c2
commit
92dfc8a431
@ -13,54 +13,23 @@ namespace SimplySerial
|
|||||||
static bool _continue = true;
|
static bool _continue = true;
|
||||||
static SerialPort _serialPort;
|
static SerialPort _serialPort;
|
||||||
|
|
||||||
|
static bool Quiet = false;
|
||||||
|
static bool NoWait = false;
|
||||||
|
|
||||||
|
// default comspec values set here will be overridden by values passed through command-line arguments
|
||||||
|
static string port = string.Empty;
|
||||||
|
static int baud = 9600;
|
||||||
|
static Parity parity = Parity.None;
|
||||||
|
static int dataBits = 8;
|
||||||
|
static StopBits stopBits = StopBits.One;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
ProcessArguments(args);
|
||||||
|
|
||||||
ConsoleKeyInfo cki = new ConsoleKeyInfo();
|
ConsoleKeyInfo cki = new ConsoleKeyInfo();
|
||||||
Console.TreatControlCAsInput = true;
|
Console.TreatControlCAsInput = true;
|
||||||
|
|
||||||
// default comspec values (will be overridden by command-line arguments)
|
|
||||||
string[] availablePorts = SerialPort.GetPortNames();
|
|
||||||
string port = string.Empty;
|
|
||||||
int baud = 9600;
|
|
||||||
Parity parity = Parity.None;
|
|
||||||
int dataBits = 8;
|
|
||||||
StopBits stopBits = StopBits.One;
|
|
||||||
|
|
||||||
if (availablePorts.Count() < 1)
|
|
||||||
ExitProgram("No COM ports detected.", -1);
|
|
||||||
else
|
|
||||||
port = availablePorts[0];
|
|
||||||
|
|
||||||
for(int i = 0; i < args.Count(); i++)
|
|
||||||
{
|
|
||||||
if ((args[i].ToLower()).StartsWith("-c"))
|
|
||||||
{
|
|
||||||
Console.WriteLine("PORT: {0}", args[i]);
|
|
||||||
}
|
|
||||||
if ((args[i].ToLower()).StartsWith("-b"))
|
|
||||||
{
|
|
||||||
Console.WriteLine("BAUD: {0}", args[i]);
|
|
||||||
}
|
|
||||||
if ((args[i].ToLower()).StartsWith("-p"))
|
|
||||||
{
|
|
||||||
Console.WriteLine("PARITY: {0}", args[i]);
|
|
||||||
}
|
|
||||||
if ((args[i].ToLower()).StartsWith("-d"))
|
|
||||||
{
|
|
||||||
Console.WriteLine("DATABITS: {0}", args[i]);
|
|
||||||
}
|
|
||||||
if ((args[i].ToLower()).StartsWith("-s"))
|
|
||||||
{
|
|
||||||
Console.WriteLine("STOPBITS: {0}", args[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ExitProgram("That's All Folks!", 1);
|
|
||||||
|
|
||||||
_serialPort = new SerialPort("COM15", 9600, Parity.None, 8, StopBits.One);
|
_serialPort = new SerialPort("COM15", 9600, Parity.None, 8, StopBits.One);
|
||||||
_serialPort.Handshake = Handshake.None;
|
_serialPort.Handshake = Handshake.None;
|
||||||
@ -107,12 +76,128 @@ namespace SimplySerial
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validates and processes any command-line arguments that were passed in. Invalid arguments will halt program execution.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="args">Command-line parameters</param>
|
||||||
|
static void ProcessArguments(string[] args)
|
||||||
|
{
|
||||||
|
// get a list of all available com ports
|
||||||
|
string[] availablePorts = SerialPort.GetPortNames();
|
||||||
|
|
||||||
|
// iterate through command-line arguments
|
||||||
|
foreach (string arg in args)
|
||||||
|
{
|
||||||
|
// split argument into components based on 'key:value' formatting
|
||||||
|
string[] argument = arg.Split(':');
|
||||||
|
|
||||||
|
// switch to lower case and remove '/', '--' and '-' from beginning of arguments - we can process correctly without them
|
||||||
|
argument[0] = (argument[0].TrimStart('/', '-')).ToLower();
|
||||||
|
|
||||||
|
// help
|
||||||
|
if (argument[0].StartsWith("h") || argument[0].StartsWith("?"))
|
||||||
|
{
|
||||||
|
ExitProgram("Sorry, you're on your own.", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// quiet (no output to console other than comes in via serial)
|
||||||
|
else if (argument[0].StartsWith("q"))
|
||||||
|
{
|
||||||
|
SimplySerial.Quiet = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// nowait (disables the "press any key to exit" function)
|
||||||
|
else if (argument[0].StartsWith("n"))
|
||||||
|
{
|
||||||
|
SimplySerial.NoWait = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the remainder of possible command-line arguments require two parameters, so let's enforce that now
|
||||||
|
else if (argument.Count() < 2)
|
||||||
|
{
|
||||||
|
ExitProgram(("Invalid or incomplete argument <" + arg + ">\nTry 'SimplySerial help' to see a list of valid arguments"), -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// com port
|
||||||
|
else if (argument[0].StartsWith("c"))
|
||||||
|
{
|
||||||
|
string newPort = argument[1].ToUpper();
|
||||||
|
|
||||||
|
if (!argument[1].StartsWith("COM"))
|
||||||
|
newPort = "COM" + argument[1];
|
||||||
|
if (!availablePorts.Contains(newPort))
|
||||||
|
ExitProgram("Cannot find specified port <" + newPort + ">", -1);
|
||||||
|
port = newPort;
|
||||||
|
Output("PORT: " + port);
|
||||||
|
}
|
||||||
|
|
||||||
|
// baud rate
|
||||||
|
else if (argument[0].StartsWith("b"))
|
||||||
|
{
|
||||||
|
//validate requested baud rate
|
||||||
|
Output("BAUD: " + argument[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// parity
|
||||||
|
else if (argument[0].StartsWith("p"))
|
||||||
|
{
|
||||||
|
//validate requested COM port
|
||||||
|
Output("PARITY: " + argument[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// databits
|
||||||
|
else if (argument[0].StartsWith("d"))
|
||||||
|
{
|
||||||
|
//validate requested data bits
|
||||||
|
Output("DATABITS: " + argument[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// stopbits
|
||||||
|
else if (argument[0].StartsWith("s"))
|
||||||
|
{
|
||||||
|
//validate requested stop bits
|
||||||
|
Output("STOPBITS: " + argument[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ExitProgram(("Invalid or incomplete argument <" + arg + ">\nTry 'SimplySerial help' to see a list of valid arguments"), -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// default to the first/only available com port, exit with error if no ports are available
|
||||||
|
if (availablePorts.Count() >= 1)
|
||||||
|
SimplySerial.port = availablePorts[0];
|
||||||
|
else
|
||||||
|
ExitProgram("No COM ports detected.", -1);
|
||||||
|
|
||||||
|
ExitProgram("That's All Folks!", 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes messages using Console.WriteLine() as long as the 'Quiet' option hasn't been enabled
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">Message to output (assuming 'Quiet' is false)</param>
|
||||||
|
static void Output(string message)
|
||||||
|
{
|
||||||
|
if (!SimplySerial.Quiet)
|
||||||
|
Console.WriteLine(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes the specified exit message to the console, then waits for user to press a key before halting program execution.
|
||||||
|
/// </summary>
|
||||||
|
/// <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 <0 if an error occurred, >=0 if program is terminating normally.</param>
|
||||||
static void ExitProgram(string message, int exitCode)
|
static void ExitProgram(string message, int exitCode)
|
||||||
{
|
{
|
||||||
Console.WriteLine(message);
|
Output(message);
|
||||||
Console.WriteLine("\nPress any key to exit...");
|
Console.WriteLine("\nPress any key to exit...");
|
||||||
|
if (!SimplySerial.NoWait)
|
||||||
|
{
|
||||||
while (!Console.KeyAvailable)
|
while (!Console.KeyAvailable)
|
||||||
Thread.Sleep(25);
|
Thread.Sleep(25);
|
||||||
|
}
|
||||||
Environment.Exit(exitCode);
|
Environment.Exit(exitCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user