Skip to content

Commit

Permalink
Merge pull request #509 from WildernessLabs/dominique-SurfacePortInUs…
Browse files Browse the repository at this point in the history
…eMessage

Surface port in user message. Also reduce retry time to about 5 seconds.
  • Loading branch information
CartBlanche authored Mar 8, 2024
2 parents 0389c6b + c005250 commit e9b0a95
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Meadow.CLI.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Meadow.CLI.Core
{
public static class Constants
{
public const string CLI_VERSION = "1.9.0.0";
public const string CLI_VERSION = "1.9.2.0";
public const ushort HCOM_PROTOCOL_PREVIOUS_VERSION_NUMBER = 0x0007;
public const ushort HCOM_PROTOCOL_CURRENT_VERSION_NUMBER = 0x0008; // Used for transmission
public const string WILDERNESS_LABS_USB_VID = "2E6A";
Expand Down
24 changes: 19 additions & 5 deletions Meadow.CLI.Core/Devices/MeadowSerialDevice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Meadow.CLI.Core.Exceptions;
using Meadow.CLI.Core.Internals.MeadowCommunication;
using System;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,7 +44,7 @@ private protected override void Dispose(bool disposing)

if (SerialPort != null)
{
lock(lockObject)
lock (lockObject)
{
if (SerialPort.IsOpen)
{
Expand All @@ -52,7 +53,7 @@ private protected override void Dispose(bool disposing)
SerialPort.Dispose();
SerialPort = null;
}
}
}
}
}

Expand Down Expand Up @@ -115,7 +116,7 @@ private static SerialPort OpenSerialPort(string portName)
{
if (string.IsNullOrEmpty(portName))
{
throw new ArgumentException("Serial Port name cannot be empty");
throw new ArgumentException("Serial Port name cannot be empty", nameof(portName));
}

// Create a new SerialPort object with default settings
Expand All @@ -136,7 +137,7 @@ private static SerialPort OpenSerialPort(string portName)
if (port.IsOpen)
port.Close();

int retries = 15;
int retries = 10;

for (int i = 0; i < retries; i++)
{
Expand All @@ -146,10 +147,23 @@ private static SerialPort OpenSerialPort(string portName)
port.BaseStream.ReadTimeout = 0;
break;
}
catch
catch (FileNotFoundException)
{
throw new Exception($"Serial port '{portName}' not found");
}
catch (UnauthorizedAccessException uae)
{
if (i == retries - 1)
{
throw new Exception($"{uae.Message} Another application may have access to '{portName}'. ");
}
Thread.Sleep(500);
}
catch (Exception ex)
{
// We don't know what happened, best to bail and let the user know.
throw new Exception($"Unable to open port '{portName}'. {ex.Message}");
}
}

return port;
Expand Down
2 changes: 1 addition & 1 deletion Meadow.CLI.Core/Meadow.CLI.Core.VS2019.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.9.0.0</Version>
<Version>1.9.2.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
14 changes: 12 additions & 2 deletions Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void ConnectionManagerProc()
try
{
Debug.WriteLine("Opening COM port...");
_port.Open();
Open();
Debug.WriteLine("Opened COM port");
}
catch (Exception ex)
Expand Down Expand Up @@ -156,8 +156,18 @@ private void Open()
{
throw new Exception($"Serial port '{_port.PortName}' not found");
}
catch (UnauthorizedAccessException uae)
{
throw new Exception($"{uae.Message} Another application may have access to '{_port.PortName}'. ");
}
catch (Exception ex)
{
// We don't know what happened, best to bail and let the user know.
throw new Exception($"Unable to open port '{_port.PortName}'. {ex.Message}");
}

State = ConnectionState.Connected;
}
State = ConnectionState.Connected;
}

private void Close()
Expand Down

0 comments on commit e9b0a95

Please sign in to comment.