82 lines
4.0 KiB
C#
82 lines
4.0 KiB
C#
using RobotNet10.CANOpen.Enums;
|
|
using RobotNet10.CANOpen.Models;
|
|
using RobotNet10.CANOpen.Services;
|
|
|
|
namespace RobotNet10.CANOpen.Interfaces;
|
|
|
|
public interface ICanOpenDevice
|
|
{
|
|
byte NodeId { get; }
|
|
NmtState State { get; }
|
|
|
|
/// <summary>
|
|
/// PDO Manager để quản lý PDOs
|
|
/// </summary>
|
|
PdoManager Pdo { get; }
|
|
|
|
// Events
|
|
event EventHandler<PdoReceivedEventArgs>? PdoReceived;
|
|
event EventHandler<EmergencyReceivedEventArgs>? EmergencyReceived;
|
|
event EventHandler<HeartbeatReceivedEventArgs>? HeartbeatReceived;
|
|
event EventHandler<HeartbeatTimeoutEventArgs>? HeartbeatTimeout;
|
|
|
|
// SDO Operations
|
|
Task<byte[]> ReadObjectAsync(ushort index, byte subIndex, CancellationToken cancellationToken = default);
|
|
Task WriteObjectAsync(ushort index, byte subIndex, byte[] data, CancellationToken cancellationToken = default);
|
|
|
|
// Helper methods for reading
|
|
Task<byte> ReadUInt8Async(ushort index, byte subIndex, CancellationToken cancellationToken = default);
|
|
Task<ushort> ReadUInt16Async(ushort index, byte subIndex, CancellationToken cancellationToken = default);
|
|
Task<uint> ReadUInt32Async(ushort index, byte subIndex, CancellationToken cancellationToken = default);
|
|
Task<short> ReadInt16Async(ushort index, byte subIndex, CancellationToken cancellationToken = default);
|
|
Task<int> ReadInt32Async(ushort index, byte subIndex, CancellationToken cancellationToken = default);
|
|
|
|
// Helper methods for writing
|
|
Task WriteUInt8Async(ushort index, byte subIndex, byte value, CancellationToken cancellationToken = default);
|
|
Task WriteUInt16Async(ushort index, byte subIndex, ushort value, CancellationToken cancellationToken = default);
|
|
Task WriteUInt32Async(ushort index, byte subIndex, uint value, CancellationToken cancellationToken = default);
|
|
Task WriteInt16Async(ushort index, byte subIndex, short value, CancellationToken cancellationToken = default);
|
|
Task WriteInt32Async(ushort index, byte subIndex, int value, CancellationToken cancellationToken = default);
|
|
|
|
// NMT Operations
|
|
Task SendNmtCommandAsync(NmtCommand command, CancellationToken cancellationToken = default);
|
|
Task StartNodeAsync(CancellationToken cancellationToken = default);
|
|
Task StopNodeAsync(CancellationToken cancellationToken = default);
|
|
Task ResetNodeAsync(CancellationToken cancellationToken = default);
|
|
Task ResetCommunicationAsync(CancellationToken cancellationToken = default);
|
|
|
|
// PDO Configuration Operations
|
|
/// <summary>
|
|
/// Ghi PDO configuration xuống device
|
|
/// Device phải ở PreOperational state trước khi gọi method này
|
|
/// </summary>
|
|
Task WritePdoConfigurationToDeviceAsync(bool isTpdo, byte pdoNumber, CancellationToken cancellationToken = default);
|
|
|
|
/// <summary>
|
|
/// Ghi tất cả PDO configurations xuống device
|
|
/// Device phải ở PreOperational state trước khi gọi method này
|
|
/// </summary>
|
|
/// <param name="edsFilePath">Đường dẫn đến EDS file (optional, để check PDO support trước khi configure)</param>
|
|
/// <param name="cancellationToken">Cancellation token</param>
|
|
Task WriteAllPdoConfigurationsToDeviceAsync(string? edsFilePath = null, CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
public interface ICanBus : IDisposable
|
|
{
|
|
string InterfaceName { get; }
|
|
bool IsConnected { get; }
|
|
|
|
Task ConnectAsync(CancellationToken cancellationToken = default);
|
|
Task DisconnectAsync(CancellationToken cancellationToken = default);
|
|
Task SendFrameAsync(uint canId, byte[] data, CancellationToken cancellationToken = default);
|
|
|
|
event EventHandler<CanFrameReceivedEventArgs>? FrameReceived;
|
|
}
|
|
|
|
public class CanFrameReceivedEventArgs(uint canId, byte[] data, DateTime timestamp) : EventArgs
|
|
{
|
|
public uint CanId { get; init; } = canId;
|
|
public byte[] Data { get; init; } = data;
|
|
public DateTime Timestamp { get; init; } = timestamp;
|
|
}
|