Initial commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using RobotNet10.CANOpen.Interfaces;
|
||||
|
||||
namespace RobotNet10.CANOpen;
|
||||
|
||||
/// <summary>
|
||||
/// Service quản lý ICanBus và CanOpenDevice instances
|
||||
/// Đảm bảo chỉ có một ICanBus instance cho mỗi CAN interface
|
||||
/// </summary>
|
||||
public interface ICanOpenManager : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Lấy hoặc tạo ICanBus instance cho interface name
|
||||
/// Nếu đã tồn tại, trả về instance hiện có
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface (ví dụ: "can0", "can1")</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
/// <returns>ICanBus instance (đã được connect)</returns>
|
||||
Task<ICanBus> GetOrCreateCanBusAsync(string interfaceName, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Lấy ICanBus instance đã tồn tại (không tạo mới nếu chưa có)
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface</param>
|
||||
/// <returns>ICanBus instance hoặc null nếu chưa tồn tại</returns>
|
||||
ICanBus? GetCanBus(string interfaceName);
|
||||
|
||||
/// <summary>
|
||||
/// Lấy hoặc tạo CanOpenDevice instance cho (interfaceName, nodeId) pair
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface</param>
|
||||
/// <param name="nodeId">NodeId của device (1-127)</param>
|
||||
/// <param name="cancellationToken">Cancellation token</param>
|
||||
/// <returns>CanOpenDevice instance</returns>
|
||||
Task<CanOpenDevice> GetOrCreateDeviceAsync(string interfaceName, byte nodeId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Lấy CanOpenDevice instance đã tồn tại (không tạo mới nếu chưa có)
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface</param>
|
||||
/// <param name="nodeId">NodeId của device</param>
|
||||
/// <returns>CanOpenDevice instance hoặc null nếu chưa tồn tại</returns>
|
||||
CanOpenDevice? GetDevice(string interfaceName, byte nodeId);
|
||||
|
||||
/// <summary>
|
||||
/// Xóa CanOpenDevice instance khỏi cache và dispose nó
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface</param>
|
||||
/// <param name="nodeId">NodeId của device</param>
|
||||
/// <returns>True nếu đã xóa thành công</returns>
|
||||
bool RemoveDevice(string interfaceName, byte nodeId);
|
||||
|
||||
/// <summary>
|
||||
/// Xóa ICanBus instance khỏi cache và dispose nó
|
||||
/// Lưu ý: Sẽ dispose tất cả CanOpenDevice đang sử dụng ICanBus này
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface</param>
|
||||
/// <returns>True nếu đã xóa thành công</returns>
|
||||
Task<bool> RemoveCanBusAsync(string interfaceName, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Lấy danh sách tất cả interface names đang được quản lý
|
||||
/// </summary>
|
||||
IReadOnlyList<string> GetManagedInterfaces();
|
||||
|
||||
/// <summary>
|
||||
/// Lấy danh sách tất cả nodeIds đang được quản lý cho một interface
|
||||
/// </summary>
|
||||
/// <param name="interfaceName">Tên CAN interface</param>
|
||||
/// <returns>Danh sách nodeIds</returns>
|
||||
IReadOnlyList<byte> GetManagedNodeIds(string interfaceName);
|
||||
|
||||
/// <summary>
|
||||
/// Kiểm tra xem interface có đang được quản lý không
|
||||
/// </summary>
|
||||
bool IsInterfaceManaged(string interfaceName);
|
||||
|
||||
/// <summary>
|
||||
/// Kiểm tra xem device có đang được quản lý không
|
||||
/// </summary>
|
||||
bool IsDeviceManaged(string interfaceName, byte nodeId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user