private static IPrinterConnector _printSerialMenu() { var portName = ConsoleHelpers.PromptLine("Port name:"); var baudRate = 0; var baudRateIsValid = false; while (!baudRateIsValid) { baudRateIsValid = int.TryParse(ConsoleHelpers.PromptLine("Baud rate:"), out baudRate); if (!baudRateIsValid) { ConsoleHelpers.WriteErrorLine("Baud rate must be an integer number, try again."); } } return(new SerialPortConnector(portName, baudRate)); }
private static IPrinter _printMenu() { while (true) { Console.WriteLine("1. Connect to printer via USB"); Console.WriteLine("2. Connect to printer via serial port"); Console.WriteLine("3. Connect to printer via TCP network"); ConsoleHelpers.WriteErrorLine("0. Exit"); var option = ConsoleHelpers.PromptKey("Select option:"); Console.WriteLine(); IPrinterConnector connector; switch (option.Key) { case ConsoleKey.D1: connector = _printUsbMenu(); break; case ConsoleKey.D2: connector = _printSerialMenu(); break; case ConsoleKey.D3: connector = _printNetworkMenu(); break; case ConsoleKey.D0: Environment.Exit(0); continue; default: ConsoleHelpers.WriteErrorLine("Invalid option, try again."); Console.WriteLine(); continue; } return(new Printer(connector)); } }
private static IPrinterConnector _printNetworkMenu() { var addressIsValid = false; IPAddress ipAddress = null; while (!addressIsValid) { addressIsValid = IPAddress.TryParse(ConsoleHelpers.PromptLine("IP Address:"), out ipAddress); if (!addressIsValid) { ConsoleHelpers.WriteErrorLine("IP Address is not valid, try again."); } } var portIsValid = false; int port = 0; while (!portIsValid) { if (int.TryParse(ConsoleHelpers.PromptLine("TCP Port (ej 9100):"), out port)) { if (port >= 0 && port < 65535) { portIsValid = true; } } if (!portIsValid) { ConsoleHelpers.WriteErrorLine("Port number is not valid, try again."); } } return(new NetworkPrinterConnector(ipAddress, port)); }