fgh
This commit is contained in:
@@ -11,7 +11,10 @@ public class CanService : ICanService
|
||||
private readonly object _lock = new();
|
||||
|
||||
public bool IsConnected { get; private set; }
|
||||
public string ChannelName => _channel.ToString();
|
||||
public string ChannelName => IsConnected ? _channel.ToString() : "";
|
||||
public Bitrate CurrentBitrate { get; private set; }
|
||||
|
||||
private (PcanChannel Channel, Bitrate Bitrate)? _pendingReinit;
|
||||
|
||||
public event Action<CanMessageDto>? MessageReceived;
|
||||
|
||||
@@ -103,6 +106,42 @@ public class CanService : ICanService
|
||||
|
||||
// ── PCAN passthrough ──────────────────────────────────────────────────────
|
||||
|
||||
public IReadOnlyList<PcanChannel> GetAvailableChannels()
|
||||
{
|
||||
var available = new List<PcanChannel>();
|
||||
foreach (var ch in Enum.GetValues<PcanChannel>()
|
||||
.Where(c => c.ToString().StartsWith("Usb", StringComparison.OrdinalIgnoreCase))
|
||||
.OrderBy(c => c.ToString()))
|
||||
{
|
||||
var result = Api.GetValue(ch, PcanParameter.ChannelCondition, out uint condition);
|
||||
if (result == PcanStatus.OK && condition != 0)
|
||||
available.Add(ch);
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
public void RequestReinitialize(PcanChannel channel, Bitrate bitrate)
|
||||
{
|
||||
lock (_lock) _pendingReinit = (channel, bitrate);
|
||||
}
|
||||
|
||||
public bool TryConsumePendingReinit(out PcanChannel channel, out Bitrate bitrate)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_pendingReinit.HasValue)
|
||||
{
|
||||
channel = _pendingReinit.Value.Channel;
|
||||
bitrate = _pendingReinit.Value.Bitrate;
|
||||
_pendingReinit = null;
|
||||
return true;
|
||||
}
|
||||
channel = default;
|
||||
bitrate = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Initialize(PcanChannel channel, Bitrate bitrate)
|
||||
{
|
||||
_channel = channel;
|
||||
@@ -110,6 +149,7 @@ public class CanService : ICanService
|
||||
if (result != PcanStatus.OK)
|
||||
throw new InvalidOperationException($"CAN init failed: {GetErrorText(result)}");
|
||||
IsConnected = true;
|
||||
CurrentBitrate = bitrate;
|
||||
}
|
||||
|
||||
public void Uninitialize()
|
||||
|
||||
@@ -7,6 +7,7 @@ public interface ICanService
|
||||
{
|
||||
bool IsConnected { get; }
|
||||
string ChannelName { get; }
|
||||
Bitrate CurrentBitrate { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Raised on the worker thread each time a CAN message passes all filters.
|
||||
@@ -31,6 +32,20 @@ public interface ICanService
|
||||
IReadOnlyDictionary<string, double> ExtractSignals(uint messageId, byte[] data);
|
||||
void PublishMessage(CanMessageDto dto);
|
||||
|
||||
// ── Channel management ────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Returns all PCAN USB channels that are currently available.</summary>
|
||||
IReadOnlyList<PcanChannel> GetAvailableChannels();
|
||||
|
||||
/// <summary>
|
||||
/// Asks the worker to reinitialise on the next loop iteration.
|
||||
/// Non-blocking — the actual reconnect happens asynchronously.
|
||||
/// </summary>
|
||||
void RequestReinitialize(PcanChannel channel, Bitrate bitrate);
|
||||
|
||||
/// <summary>Called by the worker to pick up a pending reinit request.</summary>
|
||||
bool TryConsumePendingReinit(out PcanChannel channel, out Bitrate bitrate);
|
||||
|
||||
// ── PCAN passthrough ──────────────────────────────────────────────────────
|
||||
void Initialize(PcanChannel channel, Bitrate bitrate);
|
||||
void Uninitialize();
|
||||
|
||||
Reference in New Issue
Block a user