diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index b489e7e5..a298c6dc 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -255,6 +255,8 @@ protected override async ValueTask ExecuteCommand() await WriteEspFiles(connection, deviceInfo, package); + await connection.WaitForMeadowAttach(); + // reset device if (connection != null && connection.Device != null) { diff --git a/Source/v2/Meadow.Cli/Meadow.CLI.csproj b/Source/v2/Meadow.Cli/Meadow.CLI.csproj index 7fad0a2f..b1d62523 100644 --- a/Source/v2/Meadow.Cli/Meadow.CLI.csproj +++ b/Source/v2/Meadow.Cli/Meadow.CLI.csproj @@ -10,7 +10,7 @@ Wilderness Labs, Inc Wilderness Labs, Inc true - 2.0.60.0 + 2.0.61.0 AnyCPU http://developer.wildernesslabs.co/Meadow/Meadow.CLI/ https://github.com/WildernessLabs/Meadow.CLI diff --git a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs index 873026bf..e84673a6 100644 --- a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs +++ b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs @@ -6,5 +6,5 @@ namespace Meadow.CLI; public static class Constants { - public const string CLI_VERSION = "2.0.60.0"; + public const string CLI_VERSION = "2.0.61.0"; } \ No newline at end of file diff --git a/Source/v2/Meadow.HCom/Connections/SerialConnection.ListenerProc.cs b/Source/v2/Meadow.HCom/Connections/SerialConnection.ListenerProc.cs index e7433e33..0f438efe 100644 --- a/Source/v2/Meadow.HCom/Connections/SerialConnection.ListenerProc.cs +++ b/Source/v2/Meadow.HCom/Connections/SerialConnection.ListenerProc.cs @@ -8,7 +8,7 @@ public partial class SerialConnection public override async Task WaitForMeadowAttach(CancellationToken? cancellationToken) { - var timeout = 500; + var timeout = 50; while (timeout-- > 0) { @@ -26,7 +26,7 @@ public override async Task WaitForMeadowAttach(CancellationToken? cancellationTo return; } - await Task.Delay(20); + await Task.Delay(100); if (!_port.IsOpen) { diff --git a/Source/v2/Meadow.HCom/Connections/SerialConnection.cs b/Source/v2/Meadow.HCom/Connections/SerialConnection.cs index ef37f8b5..ba69186a 100755 --- a/Source/v2/Meadow.HCom/Connections/SerialConnection.cs +++ b/Source/v2/Meadow.HCom/Connections/SerialConnection.cs @@ -1,4 +1,5 @@ using System.Buffers; +using System.Collections.Concurrent; using System.IO.Ports; using System.Security.Cryptography; @@ -21,7 +22,7 @@ public partial class SerialConnection : ConnectionBase, IDisposable private bool _isDisposed; private ConnectionState _state; private readonly List _listeners = new List(); - private readonly Queue _pendingCommands = new Queue(); + private readonly ConcurrentQueue _commandQueue = new ConcurrentQueue(); private readonly AutoResetEvent _commandEvent = new AutoResetEvent(false); private bool _maintainConnection; private Thread? _connectionManager = null; @@ -208,8 +209,7 @@ public override void Detach() // local function so we can unsubscribe var count = _messageCount; - _pendingCommands.Enqueue(command); - _commandEvent.Set(); + EnqueueRequest(command); while (timeout-- > 0) { @@ -248,19 +248,17 @@ private void CommandManager() { _commandEvent.WaitOne(1000); - while (_pendingCommands.Count > 0) + while (_commandQueue.Count > 0) { - Debug.WriteLine($"There are {_pendingCommands.Count} pending commands"); + Debug.WriteLine($"There are {_commandQueue.Count} pending commands"); + + _commandQueue.TryDequeue(out var pendingCommand); - var command = _pendingCommands.Dequeue() as Request; - if (command != null) + if (pendingCommand is Request command) { // if this is a file write, we need to packetize for progress - var payload = command.Serialize(); EncodeAndSendPacket(payload); - - // TODO: re-queue on fail? } } } @@ -297,7 +295,7 @@ public void EnqueueRequest(IRequest command) }; } - _pendingCommands.Enqueue(command); + _commandQueue.Enqueue(command); _commandEvent.Set(); }