周立功开发过程1
This commit is contained in:
62
CapMachine.Wpf/CanDrive/ZlgCan/ZlgCanIdHelper.cs
Normal file
62
CapMachine.Wpf/CanDrive/ZlgCan/ZlgCanIdHelper.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
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 标识符(标准帧 11bit:0~0x7FF;扩展帧 29bit:0~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 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;
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user