save
This commit is contained in:
50
BlazorApp/Models/Canopen/CanopenDeviceRegistry.cs
Normal file
50
BlazorApp/Models/Canopen/CanopenDeviceRegistry.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using BlazorApp.Models.Canopen.Eds;
|
||||
namespace BlazorApp.Models.Canopen
|
||||
{
|
||||
public static class CanopenDeviceRegistry
|
||||
{
|
||||
private static readonly Dictionary<int, ICanopenDeviceDecoder> _decoders
|
||||
= new();
|
||||
|
||||
static CanopenDeviceRegistry()
|
||||
{
|
||||
// 🔹 Ví dụ cấu hình từ EDS
|
||||
var encoderEds = new EdsDevice
|
||||
{
|
||||
NodeId = 5,
|
||||
DeviceName = "Encoder GXMMW",
|
||||
Tpdos =
|
||||
{
|
||||
[1] = new EdsTpdo
|
||||
{
|
||||
PdoIndex = 1,
|
||||
Objects =
|
||||
{
|
||||
new EdsObject
|
||||
{
|
||||
Index = 0x6004,
|
||||
Name = "Position",
|
||||
BitOffset = 0,
|
||||
BitLength = 32,
|
||||
Factor = 0.094,
|
||||
Unit = "mm"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Register(new EdsDeviceDecoder(encoderEds));
|
||||
}
|
||||
|
||||
public static void Register(ICanopenDeviceDecoder decoder)
|
||||
{
|
||||
_decoders[decoder.NodeId] = decoder;
|
||||
}
|
||||
public static ICanopenDeviceDecoder? GetDecoder(int nodeId)
|
||||
{
|
||||
return _decoders.TryGetValue(nodeId, out var d) ? d : null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
10
BlazorApp/Models/Canopen/Eds/EdsDevice.cs
Normal file
10
BlazorApp/Models/Canopen/Eds/EdsDevice.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace BlazorApp.Models.Canopen.Eds
|
||||
{
|
||||
public class EdsDevice
|
||||
{
|
||||
public int NodeId { get; set; }
|
||||
public string DeviceName { get; set; } = "";
|
||||
|
||||
public Dictionary<int, EdsTpdo> Tpdos { get; set; } = new();
|
||||
}
|
||||
}
|
||||
47
BlazorApp/Models/Canopen/Eds/EdsDeviceDecoder.cs
Normal file
47
BlazorApp/Models/Canopen/Eds/EdsDeviceDecoder.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using BlazorApp.Models.Canopen;
|
||||
using BlazorApp.Models.Canopen.Eds;
|
||||
namespace BlazorApp.Models.Canopen.Eds
|
||||
{
|
||||
public class EdsDeviceDecoder : ICanopenDeviceDecoder
|
||||
{
|
||||
private readonly EdsDevice _device;
|
||||
|
||||
public int NodeId => _device.NodeId;
|
||||
|
||||
public EdsDeviceDecoder(EdsDevice device)
|
||||
{
|
||||
_device = device;
|
||||
}
|
||||
|
||||
public string DecodeTpdo(int pdoIndex, byte[] data, byte length)
|
||||
{
|
||||
if (!_device.Tpdos.TryGetValue(pdoIndex, out var tpdo))
|
||||
return "-";
|
||||
|
||||
var results = new List<string>();
|
||||
|
||||
foreach (var obj in tpdo.Objects)
|
||||
{
|
||||
ulong raw = ReadBits(data, obj.BitOffset, obj.BitLength);
|
||||
double value = raw * obj.Factor;
|
||||
|
||||
results.Add($"{obj.Name}: {value:F2} {obj.Unit}");
|
||||
}
|
||||
|
||||
return string.Join(" | ", results);
|
||||
}
|
||||
|
||||
private static ulong ReadBits(byte[] data, int bitOffset, int bitLength)
|
||||
{
|
||||
ulong value = 0;
|
||||
int byteOffset = bitOffset / 8;
|
||||
|
||||
for (int i = 0; i < bitLength / 8; i++)
|
||||
{
|
||||
value |= (ulong)data[byteOffset + i] << (8 * i);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
BlazorApp/Models/Canopen/Eds/EdsObject.cs
Normal file
13
BlazorApp/Models/Canopen/Eds/EdsObject.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace BlazorApp.Models.Canopen.Eds
|
||||
{
|
||||
public class EdsObject
|
||||
{
|
||||
public ushort Index { get; set; }
|
||||
public byte SubIndex { get; set; }
|
||||
public string Name { get; set; } = "";
|
||||
public double Factor { get; set; } = 1.0;
|
||||
public string Unit { get; set; } = "";
|
||||
public int BitOffset { get; set; }
|
||||
public int BitLength { get; set; }
|
||||
}
|
||||
}
|
||||
8
BlazorApp/Models/Canopen/Eds/EdsTpdo.cs
Normal file
8
BlazorApp/Models/Canopen/Eds/EdsTpdo.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace BlazorApp.Models.Canopen.Eds
|
||||
{
|
||||
public class EdsTpdo
|
||||
{
|
||||
public int PdoIndex { get; set; }
|
||||
public List<EdsObject> Objects { get; set; } = new();
|
||||
}
|
||||
}
|
||||
10
BlazorApp/Models/Canopen/ICanopenDeviceDecoder.cs
Normal file
10
BlazorApp/Models/Canopen/ICanopenDeviceDecoder.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using BlazorApp.Models;
|
||||
namespace BlazorApp.Models.Canopen
|
||||
{
|
||||
public interface ICanopenDeviceDecoder
|
||||
{
|
||||
int NodeId { get; }
|
||||
|
||||
string DecodeTpdo(int pdoIndex, byte[] data, byte length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user