public void Run(string[] args) { /* create default settings */ var settings = new Settings(); /* update the settings with the commandline arguments */ var optionSet = ParseArguments(args, settings); /* do what we are told */ switch (settings.Action) { case AppAction.ListPorts: ListPortNames(); break; case AppAction.ShowHelp: optionSet.WriteOptionDescriptions(Console.Out); break; case AppAction.Receive: case AppAction.SendAscii: case AppAction.SendFile: case AppAction.SendText: SerialPort serialPort = null; try { serialPort = GetOpenedPort(settings.PortName, settings.BaudRate, settings.Parity, settings.DataBits, settings.StopBits); } catch (Exception ex) { Console.WriteLine(string.Format("The following error occurred while trying to open SerialPort '{0}':{1}{2}", settings.PortName, Environment.NewLine, ex)); } if (serialPort != null) { switch (settings.Action) { case AppAction.Receive: Receive(serialPort, settings.Echo, settings.FilePath, settings.Count); break; case AppAction.SendAscii: SendAscii(serialPort, settings.Ascii, settings.Count); break; case AppAction.SendText: SendText(serialPort, settings.Text, settings.Count); break; case AppAction.SendFile: SendFile(serialPort, settings.FilePath, settings.Count); break; } } break; } }
private OptionSet ParseArguments(string[] args, Settings settings) { var optionSet = new Mono.Options.OptionSet() { { "h|help", "shows this help", s => settings.Action = AppAction.ShowHelp }, { "p|port=", "sets the name of the serialport (COM1, COM2, etc)", s => settings.PortName = s }, { "l|listports", "lists the name of all available COM ports", s => settings.Action = AppAction.ListPorts }, { "e|echo", "echoes the received byte back", s => settings.Echo = true }, { "b|baudrate=", "sets the baudrate of the serialport", s => settings.BaudRate = TryParseBaudRate(s) }, { "parity=", "sets the parity bit configuration of the serialport", s => settings.Parity = TryParseParity(s) }, { "db|databits=", "sets the databits configuration of the serialport", s => settings.DataBits = TryParseDataBits(s) }, { "sb|stopbits=", "sets the stopbits configuration of the serialport", s => settings.StopBits = TryParseStopBits(s) }, { "f|send-file=", "sends the specified file over the serialport", s => { settings.FilePath = s; settings.Action = AppAction.SendFile; } }, { "a|send-ascii=", "sends the specified ascii value over the serialport", s => { settings.Ascii = TryParseAscii(s); settings.Action = AppAction.SendAscii; } }, { "c|count=", "specifies the number of files or ascii characters that are sent over the serialport", s => settings.Count = TryParseCount(s) }, { "t|text=", "specifies the text that is to be sent over the serialport", s => { settings.Text = s; settings.Action = AppAction.SendText; } }, { "r|receive", "listens on the serialport and writes received data to a file", s => { settings.Action = AppAction.Receive; settings.FilePath = s; } }, { "d|dump-to-file=", "optional file to dump the received data to", s => settings.FilePath = s } }; optionSet.Parse(args); return optionSet; }