Skip to content

Commit 8004447

Browse files
authored
Improvements in virtual device (#2496)
1 parent fd23ce8 commit 8004447

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ public class ExecuteCommandLineOptions
7979
HelpText = "Parent process id to monitor - exit if the parent exits.")]
8080
public int? MonitorParentPid { get; set; }
8181

82+
[Option(
83+
"waitfordebugger",
84+
Required = false,
85+
Default = false,
86+
HelpText = "Option to wait for a debugger connection after nanoCLR is started.")]
87+
public bool WaitForDebugger { get; set; }
88+
89+
[Option(
90+
"loopafterexit",
91+
Required = false,
92+
Default = true,
93+
HelpText = "Option to remain in loop waiting for a debugger connection after the program exits.")]
94+
public bool EnterDebuggerLoopAfterExit { get; set; }
95+
8296
/// <summary>
8397
/// Allowed values:
8498
/// q[uiet]

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public static int ProcessVerb(
6060
// need to set debugger serial port to the _other_ port so it shows at the expected end
6161
var internalSerialPort = $"COM{bridge.GetOtherPort(options.ExposedSerialPort)}";
6262

63-
hostBuilder.WaitForDebugger = true;
64-
hostBuilder.EnterDebuggerLoopAfterExit = true;
6563
hostBuilder.UseSerialPortWireProtocol(internalSerialPort);
6664

6765
// set flag
@@ -83,12 +81,6 @@ public static int ProcessVerb(
8381
hostBuilder.TryResolve();
8482
}
8583

86-
if (options.ExposedSerialPort != null || options.ExposedTcpIpPort != null || options.ExposedNamedPipe != null)
87-
{
88-
hostBuilder.WaitForDebugger = true;
89-
hostBuilder.EnterDebuggerLoopAfterExit = true;
90-
}
91-
9284
if (!internalSerialPortConfig
9385
&& options.ExposedSerialPort != null)
9486
{
@@ -110,6 +102,10 @@ public static int ProcessVerb(
110102
hostBuilder.UsePortTrace();
111103
}
112104

105+
hostBuilder.WaitForDebugger = options.WaitForDebugger;
106+
hostBuilder.EnterDebuggerLoopAfterExit = options.EnterDebuggerLoopAfterExit;
107+
108+
113109
if (options.MonitorParentPid != null)
114110
{
115111
try

targets/netcore/nanoFramework.nanoCLR.CLI/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ This option tries to resolve cross-assembly references between the loaded assemb
116116
nanoclr run --resolve (--assemblies ...)
117117
```
118118

119+
### Options to control debugger connection
120+
121+
There are two options that control how the nanoCLR execution interacts with a debugger. If it's intended that after a program terminates and exits the execution a debugger will be connecting, the option `--loopafterexit` should be included.
122+
In case it's expected that imediatly after nanoCLR is started,a debugger is to connect to it, the option `--waitfordebugger` should be included.
123+
119124
## Maintenance operations with the nanoCLR
120125

121126
The nanoCLR it's, in fact, a wrapper to the nanoCLR instance that is distributed as DLL so it can be easily updated.

targets/netcore/nanoFramework.nanoCLR.Host/Port/NativeWireProtocolPort.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ public void Open()
6868

6969
public void Close()
7070
{
71-
Status = PortStatus.Closed;
71+
lock (_syncRoot)
72+
{
73+
Status = PortStatus.Closed;
7274

73-
Interop.nanoCLR.nanoCLR_WireProtocolClose();
74-
Interop.nanoCLR.nanoCLR_SetWireProtocolReceiveCallback(null);
75-
Interop.nanoCLR.nanoCLR_SetWireProtocolTransmitCallback(null);
75+
Interop.nanoCLR.nanoCLR_WireProtocolClose();
76+
Interop.nanoCLR.nanoCLR_SetWireProtocolReceiveCallback(null);
77+
Interop.nanoCLR.nanoCLR_SetWireProtocolTransmitCallback(null);
78+
}
7679
}
7780

7881
private int WireProtocolTransmitCallback(byte[] data, int length)
@@ -85,13 +88,17 @@ private int WireProtocolTransmitCallback(byte[] data, int length)
8588
}
8689
}
8790

91+
8892
private int WireProtocolReceiveCallback(byte[] data, int length)
8993
{
9094
lock (_syncRoot)
9195
{
92-
if (_receiveBuffer.Count == 0)
96+
if (_receiveBuffer.Count == 0
97+
|| Status == PortStatus.Closed)
9398
{
94-
Monitor.Wait(_syncRoot);
99+
Thread.Yield();
100+
101+
return 0;
95102
}
96103

97104
var size = _receiveBuffer.Count < length ? _receiveBuffer.Count : length;
@@ -108,6 +115,7 @@ private void ProcessMessages()
108115
while (Status == PortStatus.Opened)
109116
{
110117
Interop.nanoCLR.nanoCLR_WireProtocolProcess();
118+
Thread.Yield();
111119
}
112120
}
113121
}

targets/netcore/nanoFramework.nanoCLR.Host/Port/Serial/SerialPort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public SerialPort(string name, int baudRate = MaxBaud)
1818
_serialPort = new System.IO.Ports.SerialPort(name, baudRate);
1919
}
2020

21-
public int BytesAvailable => _serialPort.BytesToRead;
21+
public int BytesAvailable => (_serialPort != null && _serialPort.IsOpen ? _serialPort.BytesToRead : 0);
2222

2323
public byte[] ReceiveData() => _serialPort.ReadAllBytes();
2424

0 commit comments

Comments
 (0)