Skip to content

Commit 95fa629

Browse files
committed
feat(CanBus): add queued TX bus and extension method
Removed conditional compilation for NET8_0_OR_GREATER around GetFramesAsync in all bus adapters and ICanBus abstraction, making async frame streaming available for all targets. Added QueuedTxCanBus and related options to support queued CAN frame transmission with configurable backoff and queue policies.
1 parent 1d94fab commit 95fa629

File tree

13 files changed

+439
-162
lines changed

13 files changed

+439
-162
lines changed

src/adapters/CanKit.Adapter.ControlCAN/ControlCanBus.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,12 @@ public async Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int
270270
return await _asyncRx.ReceiveBatchAsync(count, timeOut, cancellationToken).ConfigureAwait(false);
271271
}
272272

273-
#if NET8_0_OR_GREATER
274273
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
275274
{
276275
ThrowIfDisposed();
277276
await foreach (var item in _asyncRx.ReadAllAsync(cancellationToken))
278277
yield return item;
279278
}
280-
#endif
281279

282280
public BusState BusState => BusState.Unknown;
283281

src/adapters/CanKit.Adapter.Kvaser/KvaserBus.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ private void HandleBackgroundException(Exception ex)
563563
try { var snap = Volatile.Read(ref BackgroundExceptionOccurred); snap?.Invoke(this, ex); } catch { /*ignored*/ }
564564
}
565565

566-
#if NET8_0_OR_GREATER
567566
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
568567
{
569568
ThrowIfDisposed();
@@ -572,5 +571,4 @@ public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.Com
572571
yield return item;
573572
}
574573
}
575-
#endif
576574
}

src/adapters/CanKit.Adapter.PCAN/PcanBus.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public async Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int
254254
.ConfigureAwait(false);
255255
}
256256

257-
#if NET8_0_OR_GREATER
257+
258258
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
259259
{
260260
ThrowIfDisposed();
@@ -263,7 +263,6 @@ public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.Com
263263
yield return item;
264264
}
265265
}
266-
#endif
267266

268267
public PcanBusRtConfigurator Options { get; }
269268

src/adapters/CanKit.Adapter.SocketCAN/SocketCanBus.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ public async Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int
319319
.ConfigureAwait(false);
320320
}
321321

322-
#if NET8_0_OR_GREATER
323322
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
324323
{
325324
ThrowIfDisposed();
@@ -328,7 +327,6 @@ public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.Com
328327
yield return item;
329328
}
330329
}
331-
#endif
332330

333331
public void ClearBuffer() => throw new NotSupportedException("SocketCAN does not support clear buffer");
334332

src/adapters/CanKit.Adapter.Vector/VectorBus.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ public async Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int
360360
return await _asyncRx.ReceiveBatchAsync(count, timeOut, cancellationToken).ConfigureAwait(false);
361361
}
362362

363-
#if NET8_0_OR_GREATER
364363
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
365364
{
366365
ThrowIfDisposed();
@@ -369,7 +368,6 @@ public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([EnumeratorCancella
369368
yield return frame;
370369
}
371370
}
372-
#endif
373371

374372
public event EventHandler<CanReceiveData> FrameReceived
375373
{

src/adapters/CanKit.Adapter.Virtual/VirtualBus.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public async Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int
140140
.ConfigureAwait(false);
141141
}
142142

143-
#if NET8_0_OR_GREATER
144143
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync(
145144
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
146145
{
@@ -150,7 +149,6 @@ public async IAsyncEnumerable<CanReceiveData> GetFramesAsync(
150149
yield return item;
151150
}
152151
}
153-
#endif
154152

155153
public VirtualBusRtConfigurator Options { get; }
156154

src/adapters/CanKit.Adapter.ZLG/ZlgCanBus.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ public async Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int
629629
return await _asyncRx.ReceiveBatchAsync(count, timeOut, cancellationToken).ConfigureAwait(false);
630630
}
631631

632-
#if NET8_0_OR_GREATER
633632
public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
634633
{
635634
ThrowIfDisposed();
@@ -639,7 +638,6 @@ public async IAsyncEnumerable<CanReceiveData> GetFramesAsync([System.Runtime.Com
639638
}
640639

641640
}
642-
#endif
643641

644642
internal int GetAutoSendIndex(bool add = true)
645643
{

src/core/CanKit.Core/Abstractions/ICanBus.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,13 @@ public interface ICanBus : IDisposable
167167
/// <returns>Received frames (收到的帧集合)。当达到期望数量或超时/取消时返回。</returns>
168168
Task<IReadOnlyList<CanReceiveData>> ReceiveAsync(int count = 1, int timeOut = 0, CancellationToken cancellationToken = default);
169169

170-
171-
#if NET8_0_OR_GREATER
172170
/// <summary>
173171
/// Stream received frames asynchronously (异步流式获取接收帧)
174172
/// </summary>
175173
/// <param name="cancellationToken">Cancellation (取消令牌)</param>
176174
/// <returns>IAsyncEnumerable of frames</returns>
177175
IAsyncEnumerable<CanReceiveData> GetFramesAsync(CancellationToken cancellationToken = default);
178-
#endif
176+
179177

180178
/// <summary>
181179
/// Raised when a new CAN frame is received (接收到新 CAN 帧时触发)。

src/core/CanKit.Core/CanBus.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ public static TBus Open<TBus, TBusOptions, TInitCfg>(DeviceType deviceType,
147147
return Open<TBus, TBusOptions, TInitCfg>(device, typedChOptions, typedInitCfg);
148148
}
149149

150+
151+
public static QueuedCanBus WithQueuedTx(this ICanBus inner, QueuedCanBusOptions? options = null)
152+
{
153+
options ??= new QueuedCanBusOptions();
154+
return new QueuedCanBus(inner, options);
155+
}
156+
150157
private sealed class DeviceOwner(ICanDevice device) : IDisposable
151158
{
152159
private ICanDevice? _device = device;

src/core/CanKit.Core/CanKit.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
2424
<PackageReference Include="System.Memory" Version="4.5.4" />
2525
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
26+
<PackageReference Include="System.Threading.Channels" Version="9.0.10" />
2627
</ItemGroup>
2728

2829

0 commit comments

Comments
 (0)