@@ -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}
0 commit comments