Files
CapMachine/CapMachine.Wpf/CanDrive/ZlgCan/ZlgCanIdHelper.cs
2026-02-02 21:22:01 +08:00

63 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
namespace CapMachine.Wpf.CanDrive.ZlgCan
{
/// <summary>
/// ZLG CAN ID 帮助类。
/// </summary>
public static class ZlgCanIdHelper
{
/// <summary>
/// 生成 ZLG/SocketCAN 风格的 can_id包含扩展帧/远程帧/错误帧标志位)。
/// </summary>
/// <param name="id">CAN 标识符(标准帧 11bit0~0x7FF扩展帧 29bit0~0x1FFFFFFF。</param>
/// <param name="isExtended">是否为扩展帧。</param>
/// <param name="isRemote">是否为远程帧RTR。</param>
/// <param name="isError">是否为错误帧。</param>
/// <returns>包含标志位的 can_id。</returns>
public static uint MakeCanId(uint id, bool isExtended, bool isRemote, bool isError)
{
// 兼容官方样例写法bit31 扩展帧bit30 RTRbit29 ERR
// 注意:该函数不做强约束校验(例如扩展帧 29bit上层可按需要增加校验。
uint canId = id & 0x1FFFFFFF;
if (isExtended)
{
canId |= 1u << 31;
}
if (isRemote)
{
canId |= 1u << 30;
}
if (isError)
{
canId |= 1u << 29;
}
return canId;
}
/// <summary>
/// 从 can_id 中提取 29bit 标识符。
/// </summary>
/// <param name="canId">包含标志位的 can_id。</param>
/// <returns>29bit 标识符。</returns>
public static uint GetArbitrationId(uint canId)
{
return canId & 0x1FFFFFFF;
}
/// <summary>
/// 判断 can_id 是否为扩展帧。
/// </summary>
/// <param name="canId">包含标志位的 can_id。</param>
/// <returns>是否扩展帧。</returns>
public static bool IsExtended(uint canId)
{
return (canId & (1u << 31)) != 0;
}
}
}