using System;
namespace CapMachine.Wpf.CanDrive.ZlgCan
{
///
/// ZLG CAN ID 帮助类。
///
public static class ZlgCanIdHelper
{
///
/// 生成 ZLG/SocketCAN 风格的 can_id(包含扩展帧/远程帧/错误帧标志位)。
///
/// CAN 标识符(标准帧 11bit:0~0x7FF;扩展帧 29bit:0~0x1FFFFFFF)。
/// 是否为扩展帧。
/// 是否为远程帧(RTR)。
/// 是否为错误帧。
/// 包含标志位的 can_id。
public static uint MakeCanId(uint id, bool isExtended, bool isRemote, bool isError)
{
// 兼容官方样例写法:bit31 扩展帧,bit30 RTR,bit29 ERR
// 注意:该函数不做强约束校验(例如扩展帧 29bit),上层可按需要增加校验。
uint canId = id & 0x1FFFFFFF;
if (isExtended)
{
canId |= 1u << 31;
}
if (isRemote)
{
canId |= 1u << 30;
}
if (isError)
{
canId |= 1u << 29;
}
return canId;
}
///
/// 从 can_id 中提取 29bit 标识符。
///
/// 包含标志位的 can_id。
/// 29bit 标识符。
public static uint GetArbitrationId(uint canId)
{
return canId & 0x1FFFFFFF;
}
///
/// 判断 can_id 是否为扩展帧。
///
/// 包含标志位的 can_id。
/// 是否扩展帧。
public static bool IsExtended(uint canId)
{
return (canId & (1u << 31)) != 0;
}
}
}