YD_TrainingCompetition_Api/Protocols/JumpProtocolParser.cs

41 lines
1.1 KiB
C#
Raw Permalink Normal View History

2026-06-10 15:54:30 +08:00
namespace YD_TrainingCompetition_Api.Protocols;
2026-06-02 10:30:05 +08:00
2026-06-10 15:54:30 +08:00
// JumpProtocolParser 负责解析来自 2.4G 终端的 12 字节上行帧。
// 这里只做格式校验和字段提取,不做业务状态判断。
2026-06-02 10:30:05 +08:00
public sealed class JumpProtocolParser : IJumpProtocolParser
{
public bool TryParse(ReadOnlySpan<byte> frame, out JumpFrame? parsed)
{
parsed = null;
if (frame.Length < 12)
{
return false;
}
2026-06-10 15:54:30 +08:00
// 旧版设备上行帧格式固定为 AA ... 55。
2026-06-02 10:30:05 +08:00
if (frame[0] != 0xAA || frame[11] != 0x55)
{
return false;
}
2026-06-10 15:54:30 +08:00
// 帧类型目前仅接受实时成绩帧和设备完成帧。
2026-06-02 10:30:05 +08:00
if (frame[1] != 0x00 && frame[1] != 0xAA)
{
return false;
}
2026-06-10 15:54:30 +08:00
// 关键字段位置与旧版 Form1 的解析规则保持一致。
2026-06-02 10:30:05 +08:00
parsed = new JumpFrame
{
DeviceNo = frame[2],
Counter = frame[3] * 256 + frame[4],
Battery = frame[5],
Mode = frame[6],
RemainingSeconds = frame[7] * 256 + frame[8]
};
return true;
}
}