From 9b6d44f109b055a05d456795130ad6e29ef19332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Mon, 10 May 2021 16:21:32 +0100 Subject: [PATCH 1/3] Rework serial device rescan --- .../PortSerial/PortSerialManager.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/PortSerialManager.cs b/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/PortSerialManager.cs index a60bde89..3e205343 100644 --- a/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/PortSerialManager.cs +++ b/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/PortSerialManager.cs @@ -161,12 +161,7 @@ private void StopDeviceWatchersInternal() NanoFrameworkDevices.Remove(device); device?.DebugEngine?.StopProcessing(); - device?.DebugEngine?.Dispose(); - - device?.Disconnect(); - - // This closes the handle to the device - ((NanoDevice)device)?.Dispose(); + device?.DebugEngine?.Stop(true); } _watchersStarted = false; From 8ec8963bd9dba3413f3c4b5872f4e3f47450b929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Mon, 10 May 2021 16:29:00 +0100 Subject: [PATCH 2/3] Add missing request for capabilities on connect --- USB Test App WPF/MainWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USB Test App WPF/MainWindow.xaml.cs b/USB Test App WPF/MainWindow.xaml.cs index 0ba97f6f..77e49d1a 100644 --- a/USB Test App WPF/MainWindow.xaml.cs +++ b/USB Test App WPF/MainWindow.xaml.cs @@ -116,7 +116,7 @@ private void ConnectDeviceButton_Click(object sender, RoutedEventArgs e) device.CreateDebugEngine(NanoSerialDevice.SafeDefaultTimeout); } - bool connectResult = device.DebugEngine.Connect(5000, true); + bool connectResult = device.DebugEngine.Connect(5000, true, true); if(connectResult) { From bc8b262d50b08847754111a0cf43a0b7ce2cf8e9 Mon Sep 17 00:00:00 2001 From: Laurent Ellerbach Date: Mon, 10 May 2021 18:57:41 +0300 Subject: [PATCH 3/3] adjusting device watcher --- USB Test App WPF/App.xaml.cs | 8 +++++- .../PortSerial/DeviceWatcher.cs | 25 ++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/USB Test App WPF/App.xaml.cs b/USB Test App WPF/App.xaml.cs index 587261d6..4b99a131 100644 --- a/USB Test App WPF/App.xaml.cs +++ b/USB Test App WPF/App.xaml.cs @@ -23,13 +23,19 @@ public App() { Activated += App_Activated; Deactivated += App_Deactivated; - + Exit += App_Exit; vml = new ViewModelLocator(); var serialClient = CreateSerialDebugClient(); ServiceLocator.Current.GetInstance().SerialDebugService = serialClient; } + private void App_Exit(object sender, System.Windows.ExitEventArgs e) + { + var serialClient = ServiceLocator.Current.GetInstance().SerialDebugService; + serialClient.SerialDebugClient.StopDeviceWatchers(); + } + private void App_Deactivated(object sender, EventArgs e) { //throw new NotImplementedException(); diff --git a/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs b/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs index 1b421ed6..790efcef 100644 --- a/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs +++ b/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs @@ -11,10 +11,11 @@ namespace nanoFramework.Tools.Debugger.PortSerial { - public class DeviceWatcher + public class DeviceWatcher : IDisposable { private bool _started = false; private List _ports; + private Thread _threadWatch = null; public delegate void EventDeviceAdded(object sender, string port); @@ -30,14 +31,14 @@ public void Start() { if (!_started) { - var threadWatch = new Thread(() => + _threadWatch = new Thread(() => { _ports = new List(); _started = true; Status = DeviceWatcherStatus.Started; - + while (_started) { var ports = GetPortNames(); @@ -81,7 +82,7 @@ public void Start() { Priority = ThreadPriority.Lowest }; - threadWatch.Start(); + _threadWatch.Start(); } } @@ -109,9 +110,9 @@ private List GetPortNames() // If the device is still plugged, it should appear as valid here, if not present, it means, the device has been disconnected string portDescription = (string)activePorts.GetValue($"{portNameDetails.Groups[2]}"); int numPorts = (int)activePorts.GetValue("Count"); - if((portDescription == null) && (numPorts > 0)) + if ((portDescription == null) && (numPorts > 0)) { - portDescription = (string)activePorts.GetValue($"{numPorts - 1}"); + portDescription = (string)activePorts.GetValue($"{numPorts - 1}"); } if (portDescription != null) @@ -176,5 +177,17 @@ public void Stop() _started = false; Status = DeviceWatcherStatus.Stopping; } + + public void Dispose() + { + Stop(); + + while (Status != DeviceWatcherStatus.Started) + { + Thread.Sleep(50); + } + + _threadWatch = null; + } } }