Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work on serial device detection #305

Merged
merged 4 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion USB Test App WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ public App()
{
Activated += App_Activated;
Deactivated += App_Deactivated;

Exit += App_Exit;
vml = new ViewModelLocator();

var serialClient = CreateSerialDebugClient();
ServiceLocator.Current.GetInstance<MainViewModel>().SerialDebugService = serialClient;
}

private void App_Exit(object sender, System.Windows.ExitEventArgs e)
{
var serialClient = ServiceLocator.Current.GetInstance<MainViewModel>().SerialDebugService;
serialClient.SerialDebugClient.StopDeviceWatchers();
}

private void App_Deactivated(object sender, EventArgs e)
{
//throw new NotImplementedException();
Expand Down
2 changes: 1 addition & 1 deletion USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

namespace nanoFramework.Tools.Debugger.PortSerial
{
public class DeviceWatcher
public class DeviceWatcher : IDisposable
{
private bool _started = false;
private List<string> _ports;
private Thread _threadWatch = null;

public delegate void EventDeviceAdded(object sender, string port);

Expand All @@ -30,14 +31,14 @@ public void Start()
{
if (!_started)
{
var threadWatch = new Thread(() =>
_threadWatch = new Thread(() =>
{
_ports = new List<string>();

_started = true;

Status = DeviceWatcherStatus.Started;

while (_started)
{
var ports = GetPortNames();
Expand Down Expand Up @@ -81,7 +82,7 @@ public void Start()
{
Priority = ThreadPriority.Lowest
};
threadWatch.Start();
_threadWatch.Start();
}
}

Expand Down Expand Up @@ -109,9 +110,9 @@ private List<string> 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)
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<NanoSerialDevice>)device)?.Dispose();
device?.DebugEngine?.Stop(true);
}

_watchersStarted = false;
Expand Down