CanRead/BlazorApp/Models/CanopenDecoder.cs
2026-01-23 11:20:08 +07:00

57 lines
1.6 KiB
C#

using BlazorApp.Models.Canopen;
namespace BlazorApp.Models
{
public class CanopenDecoder
{
public static string GetMeaning(uint id)
{
if (id == 0x080) return "SYNC";
if (id >= 0x081 && id <= 0x0FF)
return $"EMCY (Node {id - 0x080})";
if (id >= 0x700 && id <= 0x77F)
return $"Heartbeat (Node {id - 0x700})";
if (id >= 0x180 && id <= 0x4FF)
{
int pdoIndex = (int)((id - 0x180) / 0x100) + 1;
int nodeId = (int)(id - (0x180 + (pdoIndex - 1) * 0x100));
return $"TPDO{pdoIndex} (Node {nodeId})";
}
return "Unknown";
}
public static string GetValue(uint id, byte[] data, byte length)
{
if (id >= 0x180 && id <= 0x4FF)
{
int pdoIndex = (int)((id - 0x180) / 0x100) + 1;
int nodeId = (int)(id - (0x180 + (pdoIndex - 1) * 0x100));
var decoder = CanopenDeviceRegistry.GetDecoder(nodeId);
if (decoder != null)
return decoder.DecodeTpdo(pdoIndex, data, length);
}
if (id >= 0x700 && id <= 0x77F && length >= 1)
return DecodeNmtState(data[0]);
return "No Error";
}
private static string DecodeNmtState(byte value)
{
return value switch
{
0x00 => "Boot-up",
0x04 => "Stopped",
0x05 => "Operational",
0x7F => "Pre-operational",
_ => $"Unknown (0x{value:X2})"
};
}
}
}