auto-(re)connect work-in-progress

This commit is contained in:
Edward Wright 2019-06-06 16:52:59 -04:00
parent b66e9bb4a7
commit c3c6a5ca7e

View File

@ -30,6 +30,27 @@ namespace SimplySerial
// process all command-line arguments
ProcessArguments(args);
// set up keyboard input for program control and relay to serial port
ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
Console.TreatControlCAsInput = true; // we need to use CTRL-C to activate the REPL in CircuitPython, so it can't be used to exit the application
// this is where data read from the serial port will be temporarily stored
string received = string.Empty;
//main loop - keep this up until instructed otherwise
do
{
// exit the program if CTRL-X was pressed
if (Console.KeyAvailable)
{
keyInfo = Console.ReadKey(intercept: true);
if ((keyInfo.Key == ConsoleKey.X) && (keyInfo.Modifiers == ConsoleModifiers.Control))
{
Output("\n<<< SimplySerial session terminated via CTRL-X >>>");
ExitProgram(silent: true);
}
}
// set up the serial port
serialPort = new SerialPort(port.name, baud, parity, dataBits, stopBits)
{
@ -39,7 +60,6 @@ namespace SimplySerial
DtrEnable = true, // without this we don't ever receive any data
RtsEnable = true // without this we don't ever receive any data
};
string received = string.Empty; // this is where data read from the serial port will be temporarily stored
// attempt to open the serial port, terminate on error
try
@ -48,17 +68,19 @@ namespace SimplySerial
}
catch (System.UnauthorizedAccessException uae)
{
ExitProgram((uae.GetType() + " occurred while attempting to open " + port.name + ". Is this port already in use in another application?"), exitCode: -1);
Output(uae.GetType() + " occurred while attempting to open " + port.name + ". Is this port already in use in another application?");
serialPort.Dispose();
Thread.Sleep(1000);
continue;
}
catch (Exception e)
{
ExitProgram((e.GetType() + " occurred while attempting to open " + port.name + "."), exitCode: -1);
Output(e.GetType() + " occurred while attempting to open " + port.name + ".");
serialPort.Dispose();
Thread.Sleep(1000);
continue;
}
// set up keyboard input for relay to serial port
ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
Console.TreatControlCAsInput = true; // we need to use CTRL-C to activate the REPL in CircuitPython, so it can't be used to exit the application
// if we get this far, clear the screen and send the connection message if not in 'quiet' mode
Console.Clear();
if (!SimplySerial.Quiet)
@ -128,12 +150,16 @@ namespace SimplySerial
}
catch (Exception e)
{
ExitProgram((e.GetType() + " occurred while attempting to read/write to/from " + port.name + "."), exitCode: -1);
Output(e.GetType() + " occurred while attempting to read/write to/from " + port.name + ".");
serialPort.Dispose();
Thread.Sleep(1000);
continue;
}
}
} while (autoConnect > AutoConnect.NONE);
// the program should have ended gracefully before now - there is no good reason for us to be here!
ExitProgram("\nSession terminated unexpectedly.", exitCode: -1);
ExitProgram("<<< SimplySerial session terminated >>>", exitCode: -1);
}