Skip to content

Commit

Permalink
Improve serial target detection (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes committed Oct 9, 2019
1 parent 0516fea commit 7c017eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
// See LICENSE file in the project root for full license information.
//
using nanoFramework.Tools.Debugger.Extensions;
using nanoFramework.Tools.Debugger.Serial;
using nanoFramework.Tools.Debugger.WireProtocol;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Foundation;
using Windows.Storage.Streams;

namespace nanoFramework.Tools.Debugger.PortSerial
Expand All @@ -30,6 +25,11 @@ public partial class SerialPort : PortMessageBase, IPort

public SerialDevice Device { get; internal set; }

// valid baud rates
public static readonly List<uint> ValidBaudRates = new List<uint>() { 921600, 460800, 115200 };

public uint BaudRate { get; internal set; }

public NanoDevice<NanoSerialDevice> NanoDevice { get; }

/// <summary>
Expand Down Expand Up @@ -60,6 +60,9 @@ public SerialPort(SerialPortManager portManager, NanoDevice<NanoSerialDevice> se
_portManager = portManager ?? throw new ArgumentNullException(nameof(portManager));
NanoDevice = serialDevice ?? throw new ArgumentNullException(nameof(serialDevice));

// init default baud rate with 1st value
BaudRate = ValidBaudRates[0];

ResetReadCancellationTokenSource();
ResetSendCancellationTokenSource();
}
Expand Down Expand Up @@ -98,7 +101,8 @@ public async Task<bool> OpenDeviceAsync()
successfullyOpenedDevice = true;

// adjust settings for serial port
Device.BaudRate = 921600;
// baud rate is coming from the property
Device.BaudRate = BaudRate;
Device.DataBits = 8;

/////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,35 +490,55 @@ private async Task<bool> CheckValidNanoFrameworkSerialDeviceAsync(NanoDevice<Nan
{
bool validDevice = false;

OnLogMessageAvailable(NanoDevicesEventSource.Log.CheckingValidDevice(device.Device.DeviceInformation.DeviceInformation.Id));

try
{
if (device.DebugEngine == null)
{
device.CreateDebugEngine();
}

if (await device.DebugEngine.ConnectAsync(1000, true))
// get access to Windows.Devices.SerialDevice object
// so we can set it's BaudRate property
var serialDevice = (SerialDevice)device.DeviceBase;

// sanity check for invalid or null device base
if (serialDevice != null)
{
if (device.DebugEngine.ConnectionSource == ConnectionSource.nanoBooter)
// need to go through all the valid baud rates: 921600, 460800 and 115200.
foreach (uint baudRate in SerialPort.ValidBaudRates)
{
var deviceInfo = device.DebugEngine.GetMonitorOemInfo();
if (deviceInfo != null)
serialDevice.BaudRate = baudRate;

OnLogMessageAvailable(NanoDevicesEventSource.Log.CheckingValidDevice($" {device.Device.DeviceInformation.DeviceInformation.Id} @ { baudRate }"));

if (await device.DebugEngine.ConnectAsync(1000, true))
{
device.TargetName = deviceInfo.m_releaseInfo.TargetName;
device.Platform = deviceInfo.m_releaseInfo.PlatformName;
if (device.DebugEngine.ConnectionSource == ConnectionSource.nanoBooter)
{
var deviceInfo = device.DebugEngine.GetMonitorOemInfo();
if (deviceInfo != null)
{
device.TargetName = deviceInfo.m_releaseInfo.TargetName;
device.Platform = deviceInfo.m_releaseInfo.PlatformName;

validDevice = true;
break;
}
}
else
{
device.TargetName = device.DeviceInfo.TargetName;
device.Platform = device.DeviceInfo.Platform;

validDevice = true;
validDevice = true;
break;
}
}
}
else
{
device.TargetName = device.DeviceInfo.TargetName;
device.Platform = device.DeviceInfo.Platform;

validDevice = true;
}
}
else
{
OnLogMessageAvailable(NanoDevicesEventSource.Log.CheckingValidDevice($" {device.Device.DeviceInformation.DeviceInformation.Id} with invalid DeviceBase"));
}

if (validDevice)
Expand All @@ -527,6 +547,10 @@ private async Task<bool> CheckValidNanoFrameworkSerialDeviceAsync(NanoDevice<Nan

// should be a valid nanoFramework device
device.Description = device.TargetName + " @ " + ((SerialPort)device.ConnectionPort).Device.PortName;

// 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://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.4.0-preview.{height}",
"version": "1.5.0-preview.{height}",
"buildNumberOffset": 10,
"assemblyVersion": {
"precision": "revision"
Expand Down

0 comments on commit 7c017eb

Please sign in to comment.