Skip to content
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
21 changes: 20 additions & 1 deletion src/Daqifi.Core.Tests/Device/SdCard/SdCardOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 8 additions & 1 deletion src/Daqifi.Core/Device/DaqifiStreamingDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
Loading