Skip to content

Commit dea7cc8

Browse files
josesimoesaromaa
authored andcommitted
Improvements in virtual device startup (#2483)
1 parent e44c97a commit dea7cc8

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

targets/netcore/nanoFramework.nanoCLR.CLI/ExecuteCommandProcessor.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public static int ProcessVerb(
2222
{
2323
Program.ProcessVerbosityOptions(options.Verbosity);
2424

25+
// flag to signal that the intenal serial port has already been configured
26+
bool internalSerialPortConfig = false;
27+
2528
if (options.ExposedSerialPort != null)
2629
{
2730
// a serial port was requested
@@ -55,11 +58,14 @@ public static int ProcessVerb(
5558
}
5659

5760
// need to set debugger serial port to the _other_ port so it shows at the expected end
58-
options.ExposedSerialPort = $"COM{bridge.GetOtherPort(options.ExposedSerialPort)}";
61+
var internalSerialPort = $"COM{bridge.GetOtherPort(options.ExposedSerialPort)}";
5962

6063
hostBuilder.WaitForDebugger = true;
6164
hostBuilder.EnterDebuggerLoopAfterExit = true;
62-
hostBuilder.UseSerialPortWireProtocol(options.ExposedSerialPort);
65+
hostBuilder.UseSerialPortWireProtocol(internalSerialPort);
66+
67+
// set flag
68+
internalSerialPortConfig = true;
6369
}
6470
else
6571
{
@@ -83,7 +89,8 @@ public static int ProcessVerb(
8389
hostBuilder.EnterDebuggerLoopAfterExit = true;
8490
}
8591

86-
if (options.ExposedSerialPort != null)
92+
if (!internalSerialPortConfig
93+
&& options.ExposedSerialPort != null)
8794
{
8895
hostBuilder.UseSerialPortWireProtocol(options.ExposedSerialPort);
8996
}
@@ -121,15 +128,29 @@ public static int ProcessVerb(
121128

122129
Console.CancelKeyPress += (_, _) => host.Shutdown();
123130

124-
host.Run();
131+
try
132+
{
133+
host.Run();
134+
}
135+
catch (UnauthorizedAccessException ex)
136+
{
137+
// can't open port, most likely because there's already another instance running
138+
throw new CLIException(ExitCode.E9007, ex.Message);
139+
}
140+
catch (Exception ex)
141+
{
142+
throw new CLIException(ExitCode.E9008, ex.Message);
143+
}
125144

126145
return 0;
127146
}
128147

129148
private static void ParentProcess_Exited(object sender, EventArgs e)
130149
{
131150
Console.WriteLine("Exiting due to parent process ending");
132-
Environment.Exit(0); // force exit of this process since the parent has exited/died
151+
152+
// force exit of this process since the parent has exited/died
153+
Environment.Exit(0);
133154
}
134155
}
135156
}

targets/netcore/nanoFramework.nanoCLR.CLI/ExitCode.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,17 @@ public enum ExitCode
8888
/// </summary>
8989
[Display(Name = "Error installing Virtual Serial Port Tools.")]
9090
E9006 = 9006,
91+
92+
/// <summary>
93+
/// Can't start a new instance.
94+
/// </summary>
95+
[Display(Name = "Can't start a new virtual device with these settings. Most likely there is already another instance running.")]
96+
E9007 = 9007,
97+
98+
/// <summary>
99+
/// Unknown error when starting the virtual device instance.
100+
/// </summary>
101+
[Display(Name = "Unknown error when starting the virtual device instance.")]
102+
E9008 = 9008,
91103
}
92104
}

0 commit comments

Comments
 (0)