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

Improvements in serial device detection #218

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2017 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

using nanoFramework.Tools.Debugger.Extensions;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -195,9 +196,16 @@ private void CloseCurrentlyConnectedDevice()
// this is required to be able to actually close devices that get stuck with pending tasks on the in/output streams
var closeTask = Task.Factory.StartNew(() =>
{
// This closes the handle to the device
Device?.Dispose();
Device = null;
try
{
// This closes the handle to the device
Device?.Dispose();
Device = null;
}
catch
{
// catch all required to deal with possible InteropServices.SEHException
}
});

//need to wrap this in try-catch to catch possible AggregateExceptions
Expand Down Expand Up @@ -275,7 +283,7 @@ public void DisconnectDevice()
}
catch
{

// catch all required to deal with possible Exceptions when disconnecting the device
}
}

Expand Down Expand Up @@ -373,11 +381,15 @@ public async Task<uint> SendBufferAsync(byte[] buffer, TimeSpan waiTimeout, Canc
}
catch (TaskCanceledException)
{
// this is expected to happen, don't do anything with this
// this is expected to happen when the timeout occurs, no need to do anything with it
}
catch (Exception ex)
{
Debug.WriteLine($"SendRawBufferAsync-Serial-Exception occurred: {ex.Message}\r\n {ex.StackTrace}");

// something very wrong happened, disconnect immediately
DisconnectDevice();

return 0;
}
finally
Expand Down Expand Up @@ -430,15 +442,19 @@ public async Task<byte[]> ReadBufferAsync(uint bytesToRead, TimeSpan waiTimeout,
}
catch (TaskCanceledException)
{
// this is expected to happen, don't do anything with it
// this is expected to happen when the timeout occurs, no need to do anything with it
}
catch (NullReferenceException)
{
// this is expected to happen when there is anything to read, don't do anything with it
// this is expected to happen when there isn't anything to read, don't bother with it
}
catch (Exception ex)
{
Debug.WriteLine($"ReadBufferAsync-Serial-Exception occurred: {ex.Message}\r\n {ex.StackTrace}");

// something very wrong happened, disconnect immediately
DisconnectDevice();

return new byte[0];
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ private async Task AddDeviceToListAsync(DeviceInformation deviceInformation, Str

if (await newNanoFrameworkDevice.ConnectionPort.ConnectDeviceAsync())
{
// devices powered by the USB cable and that feature a serial converter (like an FTDI chip)
// are still booting when the USB enumeration event raises
// so need to give them enough time for the boot sequence to complete before trying to communicate with them
await Task.Delay(1000);

if (await CheckValidNanoFrameworkSerialDeviceAsync(newNanoFrameworkDevice))
{
//add device to the collection
Expand All @@ -345,23 +350,7 @@ private async Task AddDeviceToListAsync(DeviceInformation deviceInformation, Str
}
else
{
// failing to connect to debugger engine on first attempt occurs frequently on dual USB devices like ESP32 WROVER KIT
// the best workaround for this is to wait a while and retry
await Task.Delay(1000);

if (await CheckValidNanoFrameworkSerialDeviceAsync(newNanoFrameworkDevice))
{
//add device to the collection
NanoFrameworkDevices.Add(newNanoFrameworkDevice);

_serialDevices.Add(serialDevice);

OnLogMessageAvailable(NanoDevicesEventSource.Log.ValidDevice($"{newNanoFrameworkDevice.Description} {newNanoFrameworkDevice.Device.DeviceInformation.DeviceInformation.Id}"));
}
else
{
OnLogMessageAvailable(NanoDevicesEventSource.Log.QuitDevice(deviceInformation.Id));
}
OnLogMessageAvailable(NanoDevicesEventSource.Log.QuitDevice(deviceInformation.Id));
}
}
else
Expand Down Expand Up @@ -535,10 +524,6 @@ private async Task<bool> CheckValidNanoFrameworkSerialDeviceAsync(NanoDevice<Nan
}
}
}
else
{
OnLogMessageAvailable(NanoDevicesEventSource.Log.CheckingValidDevice($" {device.Device.DeviceInformation.DeviceInformation.Id} with invalid DeviceBase"));
}

if (validDevice)
{
Expand All @@ -549,7 +534,6 @@ private async Task<bool> CheckValidNanoFrameworkSerialDeviceAsync(NanoDevice<Nan

// set valid baud rate from device detection
((SerialPort)device.ConnectionPort).BaudRate = serialDevice.BaudRate;

}

Task.Factory.StartNew(() =>
Expand Down
2 changes: 1 addition & 1 deletion source/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://github.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.8.1-preview.{height}",
"version": "1.9.0-preview.{height}",
"buildNumberOffset": 10,
"assemblyVersion": {
"precision": "revision"
Expand Down