mirror of
https://github.com/fasteddy516/SimplySerial.git
synced 2024-11-14 22:14:36 +00:00
auto-(re)connect work-in-progress
This commit is contained in:
parent
b66e9bb4a7
commit
c3c6a5ca7e
@ -30,6 +30,27 @@ namespace SimplySerial
|
|||||||
// process all command-line arguments
|
// process all command-line arguments
|
||||||
ProcessArguments(args);
|
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
|
// set up the serial port
|
||||||
serialPort = new SerialPort(port.name, baud, parity, dataBits, stopBits)
|
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
|
DtrEnable = true, // without this we don't ever receive any data
|
||||||
RtsEnable = 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
|
// attempt to open the serial port, terminate on error
|
||||||
try
|
try
|
||||||
@ -48,17 +68,19 @@ namespace SimplySerial
|
|||||||
}
|
}
|
||||||
catch (System.UnauthorizedAccessException uae)
|
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)
|
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
|
// if we get this far, clear the screen and send the connection message if not in 'quiet' mode
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
if (!SimplySerial.Quiet)
|
if (!SimplySerial.Quiet)
|
||||||
@ -128,12 +150,16 @@ namespace SimplySerial
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
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!
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user