using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JSMachine.WMS.PLC.Utils
{
///
/// PLC字解析辅助类
///
public static class WordResolveHelper
{
///
/// 解析PLC的字为枚举
///
///
///
///
public static List ResolveWord(short word) where T : Enum
{
List machineUnitConfigTwoTypes = new();
//注意,枚举类型没有属性,只有字段
int enumCount = typeof(T).GetFields().Length;
int max = 1;
for (int i = 1; i < enumCount; i++)
{
max *= 2;
}
//位与运算定义:只有1和1的位于运算结果为1,其他情况均为0
for (int i = 1; i <= max; i *= 2)
{
short ret = (short)(word & i);
if (ret != 0)
{
machineUnitConfigTwoTypes.Add((T)Enum.Parse(typeof(T), i.ToString()));
}
}
return machineUnitConfigTwoTypes;
}
///
/// 解析PLC的字为枚举
///
///
///
///
public static List ResolveWord(ushort word) where T : Enum
{
List machineUnitConfigTwoTypes = new();
//注意,枚举类型没有属性,只有字段
int enumCount = typeof(T).GetFields().Length;
int max = 1;
for (int i = 1; i < enumCount; i++)
{
max *= 2;
}
//位与运算定义:只有1和1的位于运算结果为1,其他情况均为0
for (int i = 1; i <= max; i *= 2)
{
short ret = (short)(word & i);
if (ret != 0)
{
machineUnitConfigTwoTypes.Add((T)Enum.Parse(typeof(T), i.ToString()));
}
}
return machineUnitConfigTwoTypes;
}
}
}