diff --git a/src/Daqifi.Core.Tests/Device/SdCard/SdCardOperationsTests.cs b/src/Daqifi.Core.Tests/Device/SdCard/SdCardOperationsTests.cs index 14f42f0..75f5ba0 100644 --- a/src/Daqifi.Core.Tests/Device/SdCard/SdCardOperationsTests.cs +++ b/src/Daqifi.Core.Tests/Device/SdCard/SdCardOperationsTests.cs @@ -197,10 +197,29 @@ public async Task StopSdCardLoggingAsync_SendsCorrectCommands() // Assert var sentCommands = device.SentMessages.Select(m => m.Data).ToList(); - Assert.Equal(3, sentCommands.Count); + Assert.Equal(4, sentCommands.Count); Assert.Equal("SYSTem:StopStreamData", sentCommands[0]); Assert.Equal("SYSTem:STORage:SD:ENAble 0", sentCommands[1]); Assert.Equal("SYSTem:STReam:INTerface 0", sentCommands[2]); // Restore USB + Assert.Equal("SYSTem:COMMunicate:LAN:ENAbled 1", sentCommands[3]); // Re-enable LAN + } + + [Fact] + public async Task StopSdCardLoggingAsync_SendsStopCommandEvenWhenIsStreamingIsFalse() + { + // Arrange - simulate stale IsStreaming state (see issue #118) + var device = new TestableSdCardStreamingDevice("TestDevice"); + device.Connect(); + await device.StartSdCardLoggingAsync("test.bin"); + device.StopStreaming(); // Sets IsStreaming = false + device.SentMessages.Clear(); + + // Act + await device.StopSdCardLoggingAsync(); + + // Assert - stop command should still be sent defensively + var sentCommands = device.SentMessages.Select(m => m.Data).ToList(); + Assert.Contains("SYSTem:StopStreamData", sentCommands); } [Fact] diff --git a/src/Daqifi.Core/Device/DaqifiStreamingDevice.cs b/src/Daqifi.Core/Device/DaqifiStreamingDevice.cs index a4cd405..37653f1 100644 --- a/src/Daqifi.Core/Device/DaqifiStreamingDevice.cs +++ b/src/Daqifi.Core/Device/DaqifiStreamingDevice.cs @@ -452,7 +452,10 @@ public Task StopSdCardLoggingAsync(CancellationToken cancellationToken = default cancellationToken.ThrowIfCancellationRequested(); - StopStreaming(); + // Defensive: always send stop command even if IsStreaming is stale (see issue #118) + Send(ScpiMessageProducer.StopStreaming); + IsStreaming = false; + Send(ScpiMessageProducer.DisableStorageSd); // Restore stream interface to USB so subsequent non-SD operations work. @@ -461,6 +464,10 @@ public Task StopSdCardLoggingAsync(CancellationToken cancellationToken = default Send(ScpiMessageProducer.SetStreamInterface(StreamInterface.Usb)); } + // Re-enable LAN interface. StartSdCardLoggingAsync disables LAN because + // the SD card and WiFi/LAN share the SPI bus on the hardware. + Send(ScpiMessageProducer.EnableNetworkLan); + _isLoggingToSdCard = false; return Task.CompletedTask;